Source Code
#include
#include
int add(int a, int b)
{
int sum;
sum=a+b;
return sum;
}
void main()
{
int a, b, sum;
clrscr();
printf("\nEnter two numbers:\t");
scanf("%d%d",&a,&b);
sum=add(a,b);
printf("\nThe sum is \t%d",sum);
getch();
}
Programs Output:-
Enter two numbers: 25 26
The sum is 51
2. Create a function subtract () with return type void. Pass the argument from the main () and display the result in subtract().
Source Code
#include
#include
void subtract(int a, int b)
{
int sub;
sub=a-b;
printf("\nThe subtraction is \t%d",sub);
}
void main()
{
int a, b, sub;
clrscr();
printf("\nEnter two numbers:\t");
scanf("%d%d",&a,&b);
subtract(a,b);
getch();
}
Programs Output:-
Enter two numbers: 15 10
The subtraction is 5
3. Create a function multi() with return type as integer. Declare required variable and take user input. Perform multiplication & return in main()
Source Code
#include
#include
int multi(int a, int b)
{
int prod;
prod=a*b;
return prod;
}
void main()
{
int a, b, prod;
clrscr();
printf("\nEnter two numbers:\t");
scanf("%d%d",&a,&b);
prod=multi(a,b);
printf("\nThe Multiplication is \t%d",prod);
getch();
}
Program Output:-
Enter two numbers: 5 5
The Multiplication is 25
4. Create a function division() wit return type as void. Declared required variables and take user input perform division & display result in division() itself.
Program Code
#include
#include
void division(int a, int b)
{
int div;
div=a/b;
printf("\nThe Division is \t%d",div);
}
void main()
{
int a, b;
clrscr();
printf("\nEnter two numbers:\t");
scanf("%d%d",&a,&b);
division(a,b);
getch();
}
Program Output
Enter two numbers: 25 5
The Division is 5
5. Combine above question and use switch case statement in main() to call function.
Ans:
6. Write programs for factorial using recursive function.
Source Code
#include
#include
long int factorial(int n)
{
if(n==1)
return(1);
else
return(n*factorial(n-1));
}
void main()
{
int num;
clrscr();
printf("\nEnter a number:\t");
scanf("%d",&num);
printf("\nThe factorial is \t%ld",factorial(num));
getch();
}
Program Output:
Enter a number: 5
The factorial is 120
Discussion/Conclusion
No comments:
Post a Comment