Programming fundamentals:
Problem sheet 1:
Exercise 1:
(i) Write a menu based c program to perform the following
arithmetic operations
Addition
Subtraction
Multiplication
Division
The user should be given an option for performing the above
mentioned operations as much number of times as he wishes to
and he should be able to quit the program only when he enters his
choice as 0.
Aim:
To Write a program to the arithmetic operations
Algorithm:
step 1:start the program
step 2:print all the arithmetic operations with exit conditions
step 3:set a loop
step 4:print to enter the numbers
step 5:print to enter the choice
step 6:print do u want to continue
step 7:if 0 is given the program should quit
step 8:stop
Progam:
#include<stdio.h>
void main()
int choice,a,b,c;
do
{
printf(“Enter the choice\n”);
printf(“1.Addition\n”);
printf(“2.Subtraction\n”);
printf(“3.Multiplication\n”);
printf(“4.Division\n”);
printf(“0.Exit\n”);
scanf(“%d”,&choice);
switch(choice)
case 1:
printf(“1.Addition\n”);
printf(“Enter the value of a and b:”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“The Addition is %d \n”,c);
break;
case 2:
printf(“2.Subtraction\n”);
printf(“Enter the value of a and b:”);
scanf(“%d%d”,&a,&b);
if (a>b)
c=a-b;
else
c=b-a
}
printf(“The subtraction is %d \n”,c);
break;
case 3:
printf(“3.Multiplication\n”);
printf(“Enter the value of a and b:”);
scanf(“%d%d”,&a,&b);
c=a*b;
printf(“The Multiplication is %d \n”,c);
break;
case 4:
printf(“4.Division\n”);
printf(“Enter the value of a and b:”);
scanf(“%d%d”,&a,&b);
if (a>b)
c=a/b;
else
c=b/a
printf(“The Division is %d \n”,c);
break;
while(choice!=0);
}
Sample INPUT and OUTPUT:
Enter the choice
1.Addition
2.Subtraction
3.Multiplication
4.diivision
0.Exit 1
1.Addition
Enter the value of a and b:15
The Addition is 20
Enter the choice
1.Addition
2.Subtraction
3.Multiplication
4.diivision
0.Exit 2
2.Subtraction
Enter the value of a and b:15
The Subtraction is 10
Enter the choice
1.Addition
2.Subtraction
3.Multiplication
4.diivision
0.Exit 3
3.Multiplication
Enter the value of a and b:15
The Multiplication is 45
Enter the choice
1.Addition
2.Subtraction
3.Multiplication
4.diivision
0.Exit 4
4.Division
Enter the value of a and b:15
The Division is 3
Enter the choice
1.Addition
2.Subtraction
3.Multiplication
4.diivision
0.Exit
Result:
The program has been executed for the four arithmetic operations and the given
program has been verified
(ii) Write a c program that read a value for the variable ‘n’. Until the ‘n’ value prints out all the
numbers that is divisible of 5.
Aim:
To write a program that n value print all the value of n until which was divisible by 5
Algorithm:
Step 1:start the program
Step 2:enter the limit
Step 3:set a loop
Step 4:print the numbers which was divisible by 5 to the given limit
Step 5:stop
Program:
#include<stdio.h>
void main()
{
int n,I;
printf(“Enter the limit”);
scanf(“%d”,&n);
do
{
if(i%5==0)
printf(“%d\t”,i);
i++;
}
while(i<=n);
}
Sample input and output:
Enter the limit 15
5 10 15
Result:
The program has been executed for the number which is divisible by 5 upto the
value of n.