0% found this document useful (0 votes)
5 views4 pages

Lab 5

The document contains three separate C programs. The first program implements basic arithmetic operations, the second program prints integers from 1 to a user-defined number, and the third program calculates a mathematical function, factorial, and a recursive sum. Each program includes a loop to allow the user to perform multiple operations until they choose to exit.

Uploaded by

An Tran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Lab 5

The document contains three separate C programs. The first program implements basic arithmetic operations, the second program prints integers from 1 to a user-defined number, and the third program calculates a mathematical function, factorial, and a recursive sum. Each program includes a loop to allow the user to perform multiple operations until they choose to exit.

Uploaded by

An Tran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Ex 1 :

#include <stdio.h>
float func_plus(float a,float b){
float sum;
sum = a + b ;
return sum;
}
float func_sub (float a,float b){
float sub;
sub = a - b ;
return sub;
}
float func_mul (float a,float b){
float mul;
mul = a * b ;
return mul;
}
float func_div (float a,float b){
float div;
div = a / b ;
return div;
}
int main(void){
char op,cho;

do{
float a,b;
printf("Choose the option : ");
scanf(" %c",&op);
switch (op){
case '+':{
printf("Enter number 1 and 2 : ");
scanf("%f %f",&a ,&b);
func_plus(a,b);
printf("The result is : %f", func_plus(a,b));
break;
}
case '-':{
printf("Enter number 1 and 2 : ");
scanf("%f %f",&a ,&b);
func_sub(a,b);
printf("The result is : %f", func_sub(a,b));
break;
}
case '*':{
printf("Enter number 1 and 2 : ");
scanf("%f %f",&a ,&b);
func_mul(a,b);
printf("The result is : %f", func_mul(a,b));
break;
}
case '/':{
printf("Enter number 1 and 2 : ");
scanf("%f %f",&a ,&b);
func_div(a,b);
printf("The result is : %f", func_div(a,b));
break;
}
default:{
break;
}}
printf("\nDo you want to continue?(y\n): ");
scanf(" %c",&cho);

} while(cho == 'y');
printf("Exit program");
return 0;
}

Ex 2 :
#include <stdio.h>
int func2(int n ,int x){
if(n <= x){
printf("%d" , x - (x - n));
func2(n+1,x);
}
return 0;
}
int main(void){
int a;
printf("Enter your integer : ");
scanf("%d", &a);
func2(1,a);
return 0;
}

Ex 3:
#include <stdio.h>
#include <math.h>
float func1 (float x , float y){
float result = 2*sin(2*x)*pow(cos(y),3) - 3*cos(2*x)*pow(sin(y),2);
return result;
}
int func2 (int x){
int mul;
if(x > 1){
mul = x * func2(x-1);

}
else {
return 1;
}
return mul;
}
int func3 (int y){
int result;
if (y > 1){
return func2(y+1) + func3(y-1) ;
}
else {
return 1;}

}
int main(void){
float a,b,output1;
int n,m,r,output2,output3;
char op;
do{
printf("Please enter your option : ");
scanf("%d",&r);
switch (r){
case 1 :
printf("Please enter two float (in degree) : ");
scanf("%f %f",&a,&b);
output1 = func1(a,b);
printf("f(%f,%f) = %f",a,b,output1 );
break;
case 2:
printf("Please enter a integer : ");
scanf("%d",&n);
output2 = func2(n);
printf("The factorial of %d is %d ", n,output2);
break;
case 3 :
printf("Please enter a integer : ");
scanf("%d",&m);
output3 = func3(m);
printf("The result is : %d",output3);
break;
}
printf("\n");
printf("Do you want to continue (y/n): ");
scanf(" %s", &op);
}while(op == 'y');
printf("Exit program");
}

You might also like