Menu-driven program using Switch case to calculate the following:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
#include<stdio.h>
#include<conio.h>
int addition(int, int );
int subtraction(int, int );
int multiplication(int, int );
int division(int, int);
int modulus(int, int);
int main(void)
{
int num1, num2,result, choice;
clrscr();
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulus\n");
printf("Enter your choice");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter two numbers");
scanf("%d%d",&num1,&num2);
result = addition(num1,num2);
printf("Addition=%d",result);
break;
case 2:
int addition(int, int );
int subtraction(int, int );
int multiplication(int, int );
int division(int, int);
int modulus(int, int);
int main(void)
{
int num1, num2,result, choice;
clrscr();
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulus\n");
printf("Enter your choice");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter two numbers");
scanf("%d%d",&num1,&num2);
result = addition(num1,num2);
printf("Addition=%d",result);
break;
case 2:
printf("Enter two numbers");
scanf("%d%d",&num1,&num2);
result = subtraction(num1,num2);
printf("Subtraction=%d",result);
break;
case 3:
printf("Enter two numbers");
scanf("%d%d",&num1,&num2);
result = multiplication(num1,num2);
printf("Multiplication=%d",result);
break;
case 4:
printf("Enter two numbers");
scanf("%d%d",&num1,&num2);
result = division(num1,num2);
printf("division=%d",result);
break;
case 5:
printf("Enter two numbers");
scanf("%d%d",&num1,&num2);
result = modulus(num1,num2);
printf("Modulus=%d",result);
break;
default :
printf("Wrong choice");
}
getch();
return 0;
}
int addition(int n1, int n2 )
{
return(n1+n2);
}
int subtraction(int n1, int n2 )
{
return(n1-n2);
}
}
}
return 0;
}
int addition(int n1, int n2 )
{
return(n1+n2);
}
int subtraction(int n1, int n2 )
{
return(n1-n2);
}
int multiplication(int n1, int n2)
{
return(n1*n2);}
int division(int n1, int n2)
{
return(n1/n2);
}
int modulus(int n1, int n2);
{
return(n1%n2);}
Output :
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice 1
Enter two numbers 10 20
Addition = 30
Function की definition में variable name छोड़ भी सकते है । जैसा कि उपरोक्त प्रोग्राम में है।
About return statement
- Return a value to the place from the function has been called.
- Also return the control from the called function to the calling function.
/* program to calculate the factorial of a number through function*/
#include<stdio.h>
#include<conio.h>
int factorial(int n); /* Function prototype or declaration */
int main(void)
{
int number, result;
clrscr();
printf("Enter the number");
scanf("%d", &number);
result = factorial(number) /* Function call */
printf("Factorial =%d", result);
int factorial(int n); /* Function prototype or declaration */
int main(void)
{
int number, result;
clrscr();
printf("Enter the number");
scanf("%d", &number);
result = factorial(number) /* Function call */
printf("Factorial =%d", result);
getch();
return 0;
}
int factorial(int n) /* Function definition*/
{
int i,fact=1;
for(i=1;i<=n;i++)
}
int factorial(int n) /* Function definition*/
{
int i,fact=1;
for(i=1;i<=n;i++)
{
fact = fact*i;
} return fact;
}
}
Output :
Enter the number 5
Factorial =120
Factorial =120
NEXT : Recursion
PREVIOUS : User defined functions-II
No comments:
Post a Comment