Uploaded by Mohammad Fahim Uddin

Some important C program Code

advertisement
Q1.
i.c program to swap two numbers using third variable
Code:
#include <stdio.h>
int main ()
{
int num1, num2, temp;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
//Swapping process
temp = num1;
num1 = num2;
num2 = temp;
printf("\nAfter swapping,"
"\nFirst number: %d"
"\nSecond number: %d", num1, num2);
return 0;
}
Output :-
ii.c program to swap two numbers without using third variable :#include <stdio.h>
int main()
{
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
//Swapping process
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
printf("\nAfter swapping,"
"\nFirst number: %d"
"\nSecond number: %d", num1, num2);
return 0;
}
Output :-
iii.c program to swap two numbers using function :#include <stdio.h>
void swap(int*, int*); //Swap function declaration
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
//Swap function definition
void swap(int *a, int *b)
{
int t;
t = *b;
*b = *a;
*a = t;
}
Output :-
iv.c program to swap two numbers using pointers :#include <stdio.h>
int main()
{
int x, y, *a, *b, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
Q2:a)c program for palindrome number :#include <stdio.h>
int main()
{
int n, r = 0, t;
printf("Enter a number to check if it's a palindrome or not\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
r = r * 10;
r = r + t%10;
t = t/10;
}
if (n == r)
printf("%d is a palindrome number.\n", n);
else
printf("%d isn't a palindrome number.\n", n);
return 0;
}
Output :-
b)c program for Armstrong number :#include <stdio.h>
int power(int, int);
int main()
{
int n, sum = 0, t, remainder, digits = 0;
printf("Input an integer\n");
scanf("%d", &n);
t = n;
// Count number of digits
while (t != 0) {
digits++;
t = t/10;
}
t = n;
while (t != 0) {
remainder = t%10;
sum = sum + power(remainder, digits);
t = t/10;
}
if (n == sum)
printf("%d is an Armstrong number.\n", n);
else
printf("%d isn't an Armstrong number.\n", n);
return 0;
}
int power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
Output :-
c)c Program for Floyd's Triangle :#include <stdio.h>
int main()
{
int n, i, c, a = 1;
printf("Enter the number of rows of Floyd's triangle to print\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ", a); // Please note space after %d
a++;
}
printf("\n");
}
return 0;
}
Output :-
d)c program for pascal triangle :#include <stdio.h>
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
for (c = 0; c <= (n - i - 2); c++)
printf(" ");
for (c = 0 ; c <= i; c++)
printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}
Output :-
Q3:C Program to find HCF & LCM
#include <stdio.h>
long gcd(long, long);
int main() {
long x, y, hcf, lcm;
printf("Enter two integers\n");
scanf("%ld%ld", &x, &y);
hcf = gcd(x, y);
lcm = (x*y)/hcf;
printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf);
printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);
return 0;
}
long gcd(long x, long y) {
if (x == 0) {
return y;
}
while (y != 0) {
if (x > y)
x = x - y;
else
y = y - x;
}
return x;
}
Output :-
Q.4
C program to convert binary to decimal :#include <stdio.h>
void main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;
printf("Enter a binary number(1s and 0s) \n");
scanf("%d", &num); /* maximum five digits */
binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
num = num / 10 ;
base = base * 2;
}
printf("The Binary number is = %d \n", binary_val);
printf("Its decimal equivalent is = %d \n", decimal_val);
}
Output :-
Q5:C program to calculate gpa :#include <stdio.h>
#include <string.h>
#include <conio.h>
int main(int argc, char *argv[])
{
const double A = 4.0;
const double A_MINUS = 3.67;
const double B_PLUS = 3.33;
const double B = 3.0;
const double B_MINUS = 2.67;
const double C_PLUS = 2.33;
const double C = 2.0;
const double C_MINUS = 1.67;
const double D = 1.00;
const double F = 0.0;
char lettergrade[25];
double credit;
double caltimes = 0;
double totalcal = 0;
double totalcredit = 0;
double finalgpa = 0;
int option;
for (;;)
{
printf("\nEnter letter grade: ");
scanf("%s",lettergrade);
printf("Enter the course credit: ");
scanf("%d", &credit);
if (lettergrade == "a" || lettergrade == "A")
{
caltimes = credit * A;
}
else if (lettergrade == "a-" || lettergrade == "A-")
{
caltimes = credit * A_MINUS;
}
else if (lettergrade == "b+" || lettergrade == "B+")
{
caltimes = credit * B_PLUS;
}
else if (lettergrade == "b" || lettergrade == "B")
{
caltimes = credit * B;
}
else if (lettergrade == "b-" || lettergrade == "B-")
{
caltimes = credit * B_MINUS;
}
else if (lettergrade == "c+" || lettergrade == "C+")
{
caltimes = credit * C_PLUS;
}
else if (lettergrade == "c" || lettergrade == "C")
{
caltimes = credit * C;
}
else if (lettergrade == "c-" || lettergrade == "C-")
{
caltimes = credit * C_MINUS;
}
else if (lettergrade == "d" || lettergrade == "D")
{
caltimes = credit * D;
}
else if (lettergrade == "f" || lettergrade == "F")
{
caltimes = credit * F;
}
else
{
printf("Invaild Input...");
}
totalcredit = totalcredit + credit;
totalcal = totalcal + caltimes;
printf("Do you want to enter another grade (1 - Yes, 2 - no): ");
scanf("%d",&option);
if (option == 1)
{
continue;
}
else
{
break;
}
}
finalgpa = totalcal / totalcredit;
printf("Student's GPA: %f\n", finalgpa);
return 0;
}
C program to calculate CGPA:-
#include<stdio.h>
int main()
{
double English , Hindi ,Maths, Science, SocialStudy, CGPA , CGPAper ;
English = 9.1;
Hindi = 8.5;
Maths = 9.5;
Science =9.6;
SocialStudy = 8.6;
CGPA = (9.1+8.5+9.5+9.6+8.6)/(5.0);
CGPAper = (float)(9.5 * (CGPA));
printf("\n\n CGPA percentage is: %f",CGPAper);
return (0);
}
Q6)
5.1-i) C program to determine whether a given number is odd or Even without else option:#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int x;
clrscr();
printf(“Enter an integer number: “);
scanf(“%d”,&x);
if(x%2==0)
{
printf(“The number entered is even”);
getch();
exit(0);
}
printf(“The number entered is odd”);
getch();
}
ii)C Program to determine whether a given number is odd or even with else option :#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf(“Enter an integer number: “);
scanf(“%d”,&x);
if(x%2==0)
printf(“The number entered is even”);
else
printf(“The number entered is odd”);
getch();
}
5.2)C program to find the number of and sum of all integers greater than 100 and less than
200 that are divisible by 7.:#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int Num,Sum,Count;
clrscr();
Num=100;
Sum=Count=0;
Loop:
if (Num%i==0)
{
Sum=Sum+Num;
Count=Count+1;
}
Num=Num+1;
if(Num<=100)
goto Loop;
printf(“Count:– %d\n”,Count);
printf(“Sum:– %d”,Sum);
}
5.3)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,m,n,Dr;
float x1,x2;
clrscr();
printf(“Enter the value of a, b, c, d, m, n: “);
scanf(“%d%d%d%d%d%d”,&a,&b,&c,&d,&m,&n);
Dr=(a*d-c*b);
if(Dr!=0)
{
x1=(m*d-b*n)/dr;
x2=(n*a-m*c)/dr;
printf(“\n The value of x1= %f \n The value of x2= %f”,x1,x2);
}
else
printf(“The division is not possible and result is an abrupt value “);
getch();
}
5.4)
a) program to compute and print the number of students who have obtained more than 80
marks:-
5.5)C program to process the applications to the eligible candidates.
#include<stdio.h>
#include<conio.h>
void main()
{
int Maths,Phy,Chem,Total,Total_MP;
clrscr();
printf(“Enter the marks of maths :”);
scanf(“%d”,&Maths);
printf(“Enter the marks of phy :”);
scanf(“%d”,&Phy);
printf(“Enter the marks of chem :”);
scanf(“%d”,&Chem);
Total=Maths+Phy+Chem;
Total_MP=Phy+Maths;
if (Maths>=60 && Phy>=50 && Chem>=40 && Total>=200)
printf(“The candidate is eligible for the admission”);
else
{
if(Total_MP>=150)
printf(“The candidate is eligible for the admission”);
else
`
}
getch();
}
printf(“The candidate is not eligible for the admission”);
5.6)C program to print a two-dimensional Sqaure Root Table:#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float sq,sum,i,j;
clrscr();
printf(“Number “);
j=0.1;
loop3:
printf(” %f”,j);
j=j+0.1;
if(j<0.5)
goto loop3;
printf(“\n”);
i=1;
loop1:
printf(“%f”,i);
j=0.1;
loop:
sum=i+j;
sq=sqrt(sum);
printf(” %f”,sq);
j=j+0.1;
if(j<0.5)
goto loop;
i=i+1;
if(i<=4)
{
printf(“\n”);
goto loop1;
}
getch();
}
5.7)C program that will read the value of x and evaluate the following function
Y=
1 for x>0
0 for x=0
-1 for x<0
Using
(a) Nested if statements
#include<stdio.h>
#include<conio.h>
void main()
{
int y;
float x;
clrscr();
printf(“Enter the value of X: “);
scanf(“%f”,&x);
if(x>0)
{
y=1;
printf(“The value of y for the given value of x=%f is %d\n”,x,y);
}
else
{
if(x==0)
{
y=0;
printf(“The value of y for the given value of x=%f is %d\n”,x,y);
}
else
{
y=-1;
printf(“The value of y for the given value of x=%f is %d\n”,x,y);
}
}
getch();
}
(b) Else if statements
#include<stdio.h>
#include<conio.h>
void main()
{
int y;
float x;
clrscr();
printf(“Enter the value of X: “);
scanf(“%f”,&x);
if(x>0)
{
y=1;
printf(“The value of y for the given value of x=%f is %d\n”,x,y);
}
else if(x==0)
{
y=0;
printf(“The value of y for the given value of x=%f is %d\n”,x,y);
}
else
{
y=-1;
printf(“The value of y for the given value of x=%f is %d\n”,x,y);
}
getch();
}
(c)Conditional operators
#include<stdio.h>
#include<conio.h>
void main()
{
int y;
float x;
clrscr();
printf(“Enter the value of X: “);
scanf(“%f”,&x);
(x>0?(y=1):(x==0)?(y=0):(y=-1));
printf(“The value of y for the given value of x=%f is %d\n”,x,y);
getch();
}
5.10)C program to compute the real roots of a quadratic equation
ax2 + bx+c=0:#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d;
float x1,x2,x;
clrscr();
printf(” Enter the value of a: “);
scanf(“%f”,&a);
printf(“\n Enter the value of b: “);
scanf(“%f”,&b);
printf(“\n Enter the value of c: “);
scanf(“%f”,&c);
d=(b*b)-(4*a*c);
if(a==0 && b==0)
printf(” There is no solution of the quadratic equation”);
else if(a==0)
{
x=-c/b;
printf(” There is only one root of the equation, x= %f”,x);
}
else if(d<0)
{
printf(“The roots are imaginary and as follows: \n”);
}
else
{
x1= (-b+sqrt(d))/(2*a);
x2= (-b-sqrt(d))/(2*a);
printf(“The roots are real”);
printf(“x1=%f \n x2=%f”,x1,x2);
}
getch();
}
5.11)C program to read three integer values from the keyboard and
display the output stating that they are the sides of right-angled
triangle.
#include<conio.h>
#include<stdio.h>
void main()
{
float Len,Hei,Hyp;
float Temp1,Temp2;
clrscr();
printf(“Enter Length Height and Hypotenes of Triangle–\n”);
scanf(“%f %f %f”,&Len,&Hei,&Hyp);
Temp1=Hyp*Hyp;
Temp2=Len*Len+Hei*Hei;
if(Temp1==Temp2)
printf(“Triangle is Right Angle Triangle\n”);
else
printf(“Triangle is Not a Right Angle Triangle\n”);
getch();
}
5.12)
Code:#include<conio.h>
#include<stdio.h>
void main()
{
int Units;
char Name[10];
float Charge;
clrscr();
printf(“Enter Name of User:–\n”);
scanf(“%s”,&Name);
printf(“Enetr Total Units Consumed\n”);
scanf(“%d”,&Units);
if(Units>=0&&Units<=200)
Charge=100+(Units*0.80);
else if(Units>200&&Units<=300)
Charge=100+(Units*0.90);
else if(Units>300&&Units<=400)
Charge=100+Units;
else
Charge=(100+units)+(100+Units)*15;
printf(“Name
Units
Charge\n”);
printf(“%s
%d
%.2f”,Name,Units,Charge);
getch();
}
Output :-
Enter Name of User:– Fahim
Enetr Total Units Consumed 600
Name
Units
Charge
Ritesh
600
805.00
5.13:Code:#include<conio.h>
#include<stdio.h>
void main()
{
int Sum,i,Count;
clrscr();
Sum=Count=0;
i=0;
Loop:
if((i%6==0)&&(i%4!=0))
{
printf(“%d \n”,i);
Count=Count+1;
Sum=Sum+i;
}
i=i+1;
if(i<=100)
goto Loop;
printf(“Sum of Numbers is %d\n”,Sum);
printf(“Count of Numbers is %d\n”,Count);
getch();
}
Output:–
6 18 30 42 54 66 78 90
Sum of Numbers is 384
Count of Numbers is 8
5.14)
Code:#include<conio.h>
#include<stdio.h>
void main()
{
int Num,i,Count,Temp;
clrscr();
Count=0;
i=2;
printf(“Enter A Number :–\n”);
scanf(“%d”,&Num);
Loop:
Temp=Num%i;
if(Temp==0)
Cpunt=Count+1;
i=i+1;
if(i<=Num)
goto Loop;
if(Count==1)
printf(“Number %d is Prime”,Num);
else
printf(“Number %d is Not Prime”,Num);
getch();
}
Output:–
Enter A Number :–
6
Number 6 is Prime
Q6-b)
C program for multiplication table using for loop:#define COLMAX 10
#define ROWMAX 12
#include<stdio.h>
int main()
{
int row,col, y;
printf(" MULTIPLICATION TABLE \n\n");
for(row=1;row <= ROWMAX;row++)
{
col = 1;
for(col=1;col<=COLMAX;col++)
{
y = row * col;
printf("%4d", y);
}
printf("\n");
}
return 0;
}
C program for multiplication table using do while loop:#define COLMAX 10
#define ROWMAX 12
#include<stdio.h>
int main()
{
int row,col, y;
row = 1;
printf(" MULTIPLICATION TABLE \n\n");
do
{
col = 1;
do
{
y = row * col;
printf("%4d", y);
col = col + 1;
}
while (col <= COLMAX);
printf("\n");
row = row + 1;
}
while (row <= ROWMAX);
return 0;
}
Q7.6.1.
While loop to reverse the digits of the number.
CODE:
#include<stdio.h>
int main()
{
int a,b,i;
int c=0;
printf("Enter the number : ");
scanf("%d",&a);
while(a!=0)
{
b=a%10;
c=c*10+b;
a=a/10;
}
printf("The reverse number is %d",c);
getch();
}
Output:
Q7.6.2.
Program that computes and prints a table of factorials for any given number.
CODE:
#include<stdio.h>
int main()
{
int a,b,i;
int c=0;
printf("Enter the number : ");
scanf("%d",&a);
b=1;
while(a!=0)
{
b=b*a;
a=a-1;
}
printf("The reverse number is %d",b);
getch();
}
Output:
Q7.6.3.
C program to compute the sum of the digits of a given number.
CODE:
#include<stdio.h>
int main()
{
int a,b,Sum,c;
printf("Enter Number: ");
scanf("%d",&a);
b=a;
Sum=0;
while(b!=0)
{
c=b%10;
Sum=Sum+c;
b=b/10;
}
printf("Sum of Number %d is %d\n",a,Sum);
getch();
}
Output:
Q7. (6.4)
C program using do…..while loop to calculate and print the Fibonacci numbers
CODE:
#include <stdio.h>
int main()
{
int a=-1;
int b=1;
int c;
int d=0;
int i,n;
printf("Enter range : ");
scanf("%d",&n);
do
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
d++;
}
while(d<n);
getch();
}
Output:
Q7.6.7(a).
CODE:
#include <stdio.h>
int main()
{
int row,col,i;
int n=5;
for(row=1;row<=n;row++)
{
for(col=1;col<=row;col++)
{
printf("%2d",row);
}
printf("\n");
}
getch();
}
Output:
Q7.6.7(b).
CODE:
#include <stdio.h>
int main()
{
int row,col,i;
int n=6;
for(row=n;row>=1;row--)
{
for(i=1;i<=n-row;i++)
{
printf(" ");
}
for(col=1;col<=row;col++)
{
printf("* ");
}
printf("\n");
}
getch();
}
Output:
Q7. (6.8)
C program to read the age of 100 persons and count the number of persons in the age
group 50 to 60
CODE:
#include <stdio.h>
int main()
{
int a[1000];
int i;
int n=10; //for showing the code perfectly we took 10 inputs instead of 100
int count=0;
printf("Enter the ages:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]>=50 && a[i]<=60)
count++;
}
printf("The number of people within the range of 50 to 60 are :");
printf("%d people",count);
}
( Q7. (6.18)
C program to print all integers that are not divisible by either 2 or 3 and lie between 1 and
100
Code:#include<stdio.h>
int main()
{
int i,a=0;
printf("All integers that are not divisible by either 2 or 3 and lie between 1 and 100 are:");
for(i=1;i<=100;i++)
{
if(i%2!=0 && i%3!=0)
{
a++;
printf(" %d",i);
}
}
printf("\n The number of such integers are = %d",a);
getch();
}
Output:
Q7.b)
A simple calculator using switch case statements.
CODE:
#include <stdio.h>
int main() {
char operator;
double a,b;
printf("Which operation you want to do (+, -, *,)? \n");
scanf("%c", &operator);
printf("Enter the two numbers: ");
scanf("%lf %lf", &a, &b);
switch (operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf", a, b, a + b);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", a, b, a - b);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", a, b, a * b);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", a, b, a / b);
break;
}
getch();
}
Q8.(a)
C program to compute the following equation using array:
CODE:
#include<stdio.h>
int main()
{
int i,n;
float sum=0;
float avg;
printf("Enter the number of terms : ");
scanf("%d", &n);
float a[n];
printf("Enter the numbers \n");
for(i=1; i<= n;i++)
{
scanf("%f", &a[i]);
}
for(i=1; i<= n; i++)
{
sum = sum + a[i];
}
avg = sum/n;
printf("The result for the following equation is %.2f", avg);
getch();
}
Q8. b) C program to compute the following equation using array
CODE:
#include<stdio.h>
#include <math.h>
int main()
{
int i,n;
float sum=0;
float avg;
n=10;
float a[n];
printf("Enter the numbers \n");
for(i=1;i<= n;i++)
{
scanf("%f", &a[i]);
}
for(i=1; i<= n; i++)
{
sum = sum +pow(a[i],2);
}
printf("The result for the following equation is %.2f", sum);
getch();
}
Output:
Q9. C program to calculate standard deviation, mean and variance
CODE:
#include<stdio.h>
#include <math.h>
int main()
{
int i,n;
float sum1=0,mean,sum2=0,var,sd;
printf("How many numbers are going to be calculated : ");
scanf("%d",&n);
float a[1000];
printf("What are the numbers: \n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
sum1=sum1+a[i];
}
mean=sum1/n;
for(i=0;i<n;i++)
{
sum2=sum2+pow((mean-a[i]),2);
}
var=sum2/n;
sd=sqrt(var);
printf("The Mean value is = %.2f\nThe Variance is =%.2f\nThe Stadard deviation is =
%.2f",mean,var,sd);
}
Output:
Q.10
C program for fitting a straight line through a set of points
Code:#include<stdio.h>
#include<conio.h>
void main()
{
int i,n=10,v1,v2,x[10],y[10];
int Sx=0,Sy=0,Sxy=0,Sx2=0;
float m,c,XY,X2;
printf("Enter the values of x = \n");
for(i=0;i<10;i++)
{
scanf("%d",&x[i]);
}
printf("Enter the values of y = \n");
for(i=0;i<10;i++)
{
scanf("%d",&y[i]);
}
for(i=0;i<10;i++)
{
Sx=Sx+x[i];
Sy=Sy+y[i];
Sxy=Sxy+(x[i]*y[i]);
Sx2=Sx2+(x[i]*x[i]);
}
XY= Sx*Sy;
X2=Sx*Sx;
m=((n*Sxy)-(XY))/((n*Sx2)-X2);
c=((Sy)-(m*Sx))/n;
printf(" \nThe equation of the straight line is : ");
printf(" Y=%fX+%f",m,c);
return 0;
}
Output:
Q11. (9.7) A function of C program that returns 1 if its argument is a prime number and
returns zero otherwise
Code:-
#include<stdio.h>
int prime(int x)
{
int i;
for(i=2;i<=x/2;i++)
{
if(x%i==0)
return 0;
}
return 1;
}
int main()
{
int a,b;
printf("enter a Number ");
scanf("%d",&a);
b=prime(a);
if(b==1)
printf("This is a prime number");
else
printf("This is not a prime number");
}
Output:
Q11. (9.10) A modular interactive program using functions that reads the values of three
sides of a triangle and displays either its area or its perimeter as per the request of the user
Code:#include<stdio.h>
#include<math.h>
int a,b,c;
void Read()
{
printf("Enter three sides of Triangle : \n");
scanf("%d %d %d",&a,&b,&c);
}
void Area()
{
double S,Area,i;
S=(a+b+c)/2;
Area=sqrt((S-a)*(S-b)*(S-c));
printf("Area of Triangle = %lf",Area);
}
void Peri()
{
int P;
P=a+b+c;
printf("Perimeter of Triangle = %d",P);
}
void main()
{
int x;
Read();
while(1)
{
printf("\n 1. Area \n 2. Perimeter \n 3. Exit\n");
printf("Enteryour choice\n");
scanf("%d",&x);
switch(x)
{
case 1:
Area();
break;
case 2:
Peri();
break;
default:
exit(0);
}
}
}
Q12. C program for addition subtraction multiplication and division using the function
CODE:
#include<stdio.h>
int add(int n1, int n2);
int subtract(int n1, int n2);
int multiply(int n1, int n2);
float divide(float n1, float n2);
int add(int n1, int n2) //function for addition
{
int result;
result = n1 + n2;
return result;
}
int subtract(int n1, int n2) //function for substraction
{
int result;
result = n1 - n2;
return result;
}
int multiply(int n1, int n2) //function for multiplication
{
int result;
result = n1 * n2;
return result;
}
float divide(float n1, float n2) //function for division
{
float result;
result = n1 / n2;
return result;
}
int main()
{
int x, y;
printf("Enter first numbers: \n");
scanf("%d", &x);
printf("Enter second number: \n");
scanf("%d", &y);
printf("The addition is %d + %d = %d\n", x, y, add(x, y));
printf("The substraction is %d - %d = %d\n", x, y, subtract(x, y));
printf("The multipication is %d * %d = %d\n", x, y, multiply(x, y));
printf("The division is %d / %d = %.2f\n", x, y, divide(x, y));
return 0;
}
Q13. C program to check even or odd using functions
CODE:
#include<stdio.h>
int even_odd_func(int x)
{
if(x%2==0)
printf("The number is even");
else
printf("The number is odd");
}
int main()
{
int a;
printf("Enter a number you want to check: \n");
scanf("%d",&a);
even_odd_func(a);
}
Output:
Download