View
220
Download
0
Category
Preview:
DESCRIPTION
Useful programs in C
Citation preview
Q.1. Write a program to perform all the
arithmetic operations i.e.
addition,subtraction,multiplication,division?
.
// Program to perform arithmetic
operations like
addition,subtraction,multiplication
and division.
#include
int add(int,int); /*function prototype declaration*/int sub(int,int);
int product(int,int);
float quotient(int,int);
int main()
{
int a,b;
printf(" Enter the first number :- ");
scanf("%d",&a);
printf("\n Enter the second number :- ");
scanf("%d",&b);
printf("\n The sum of %d and %d is :-
%d",a,b,add(a,b));
printf("\n The difference between %d
and %d is :- %d",a,b,sub(a,b));
printf("\n The product of %d and %d is :-
%d",a,b,product(a,b));
printf("\n The quotient of %d and %d
is :- %f\n",a,b,quotient(a,b));
return 0;
}
int add(int a,int b) /*function for addition*/{
return a+b;
}
int sub(int a,int b) /* function for substraction*/{
if(a>b)
return a-b;
else
return b-a;
}
int product(int a,int b) /* function for multiplication*/{
return a*b;
}
float quotient(int a,int b) /* function for division*/{
if(a>b)
return (float)a/b;
else
return (float)b/a;
}
OUTPUT :-
Enter the first number :- 3
Enter the second number :- 12
The sum of 3 and 12 is :- 15
The difference between 3 and 12 is :- 9
The product of 3 and 12 is :- 36
The quotient of 3 and 12 is :- 4.000000
Q.2. Write a program to calculate the
area and perimeter of trapezoid,
triangle and
circle and area of ellipse?
// Program to calculate Area and
perimeter of trapezoid, triangle and
circle and area of ellipse.
#include
#include
float
area
(float,float,float,float,float,float,float,float,float,float,float);
int main()
{
float a,b,h,c,d,e,f,r,x,y,z;
printf(" Enter the length of one of the
parallel sides of the trapezoid :-
");
scanf("%f",&c);
printf("\n Enter the length of the second
parallel side of the trapezoid :-
");
scanf("%f",&d);
printf("\n Enter the other two sides of
the trapezoid :- ");
scanf("%f%f",&e,&f);
printf("\n Enter the height of the
trapezoid :- ");
scanf("%f",&h);
printf("\n Enter the radius of the circle :-
");
scanf("%f",&r);
printf("\n Enter the length of the major
axis of the ellipse :- ");
scanf("%f",&a);
printf("\n Enter the length of the minor
axis of the ellipse :- ");
scanf("%f",&b);
printf("\n Enter the three sides of the
triangle :- ");
scanf("%f%f%f",&x,&y,&z);
area(a,b,c,d,e,f,h,r,x,y,z);
return 0;
}
float area(float a,float b,float c,float
d,float e,float f,float h,float
r,float x,float y,float z)
{
float a1,a2,a3,p1,p2,p3;
double a4;
a1=(22*a*b)/7;
printf("\n The area of the given ellipse
is :- %f ",a1);
a2=((a+b)*h)/2;
p1=(a+b+c+d);
printf("\n The area of the given
trapezoid is :- %f and perimeter is :- %f
",a2,p1);
a3=(22*r*r)/7;
p2=(2*22*r)/7;
printf("\n The area of the given circle
is :- %f and perimeter is :- %f
",a3,p2);
p3=x+y+z;
a4=sqrt((p3/2)*(p3/2-x)*(p3/2-y)*(p3/2-
z));
printf("\n The area of the given triangle
is :- %f and perimeter is :- %f
\n",a4,p3);
return 0;
}
OUTPUT :-
Enter the length of one of the parallel
sides of the trapezoid :- 5
Enter the length of the second parallel
side of the trapezoid :- 4
Enter the other two sides of the
trapezoid :- 3
4
Enter the height of the trapezoid :- 3
Enter the radius of the circle :- 4
Enter the length of the major axis of the
ellipse :- 5
Enter the length of the minor axis of the
ellipse :- 3
Enter the three sides of the triangle :- 3
4
5
The area of the given ellipse is :-
47.142857
The area of the given trapezoid is :-
12.000000 and perimeter is :-
17.000000
The area of the given circle is :-
50.285713 and perimeter is :- 25.142857
The area of the given triangle is :-
6.000000 and perimeter is :- 12.000000
Q.3. Write a program to find the
solution of a quadratic equation?
Date :- 17-01-2014.
// Program to find the solution of a
quadratic equation.
#include
#include
double quadratic(int,int,int); /* function prototype declaration*/int main()
{
int a,b,c;
printf(" Enter the value of 'a' in the
quadratic equation :- ");
scanf("%d",&a);
printf("\n Enter the value of 'b' in the
quadratic equation :- ");
scanf("%d",&b);
printf("\n Enter the value of 'c' in the
quadratic equation :- ");
scanf("%d",&c);
quadratic(a,b,c);
return 0;
}
double quadratic(int a,int b,int c) /* function for determining roots of quadratic equation*/{
double x1,x2;
if((b*b-4*a*c)b && a>c)?a:((b>c)?b:c);
int s=(a100 && n=50 && n=60 && n=70 && n=80 && n=90 && n100)printf(" Sorry!!! Maximum marks is 100.\n ");else if(n>=90)printf(" Grade : S \n");else if(n>=80)printf(" Grade : A \n");else if(n>=70)printf(" Grade : B \n");else if(n>=60)printf(" Grade : C \n");else if(n>=50)printf(" Grade : D \n");else if(n=10){a=s;s=0;while(a>0){r=a%10;a=a/10;s=s+r;d=d*10+r;}if(b==0){if(d==n)printf("\n The number is a palindrome\n");elseprintf("\n The number is not apalindrome \n");}b++;}return s;}OUTPUT :-Enter an integer :- 25652The number is a palindromeThe sum of digits of the number in onedigit is :- 2Q.11. Write a program to calculatefactorial of a number with and withoutrecursion?Date 01-02-2014.// Program to calculate factorial of anumber with and without usingrecursion#includeint fact(int);int factr(int);int main(){int n;printf(" Enter the value whose factorialhas to be found out :- ");scanf("%d",&n);if(fact(n)==0)printf("\n Factorial not possible \n");elseprintf("\n The factorial of %d withoutrecursion is :- %d \n",n,fact(n));if(factr(n)==0)printf(" Factorial not possible \n");elseprintf(" The factorial of %d usingrecursion is :- %d \n",n,factr(n));return 0;}int fact(int n){int i,f=1;if(nb){while(b>0){temp=a%b;a=b;b=temp;}return a;}else{while(a>0){temp=b%a;b=a;a=temp;}return b;}}int gcdr(int a,int b){int temp;if(a>b){temp=a%b;if(temp==0)return b;elsereturn(gcd(b,temp));}else{temp=b%a;if(temp==0)return a;elsereturn(gcd(a,temp));}}OUTPUT :-Enter the first number :- 35Enter the second number :- 6The Greatest common divisor of 35 and6 without using recursion is :- 1The Greatest common divisor of 35 and6 using recursion is :- 1Q.14. Write a program to print thefibonacci series upto a certain numberentered bythe user?Date 01-02-2014.// Program to print the fibonacci seriesupto a certain number entered bythe user#includeint fibonacci(int);int main(){int n;printf(" Enter the last limit of theseries :- ");scanf("%d",&n);fibonacci(n);return 0;}int fibonacci(int n){int a=0,b=1,s;printf("\n %d,%d",a,b);while((a+b)=1;i--){for(j=n;j>=i;j--)printf("%d ",j);printf("\n");}return 0;}int structure2(int n){int i,j;for(i=1;i=i;j--)printf("%d ",j);printf("\n");}return 0;}int structure3(int n){int i,j;for(i=1;i=1;j--)printf(" ");for(j=1;j=1;j--)printf("%d ",j);for(j=2;jb)return a-b;elsereturn b-a;}int multi(int a,int b){return (a*b);}float div(int a,int b){return (float)a/b;}int power(int a,int b){return pow(a,b);}int mod(int a,int b){return a%b;}OUTPUT :-Enter the first integer :- 6Enter the second integer :- 5Press '1' for addition, '2' forsubtraction , '3' for multiplication '4'for division and '5' to power and '6' tofind remainder :- 1The sum of 6 and 5 is :- 11Press '1' to coninue and '0' to exit!!! 1Enter the first integer :- 4Enter the second integer :- 6Press '1' for addition, '2' forsubtraction , '3' for multiplication '4'for division and '5' to power and '6' tofind remainder :- 4The quotient of 4 and 6 is :- 0.666667Press '1' to coninue and '0' to exit!!! 0Q.20. Write a program to find the gradeof a student using switch case?Date 14-02-2014.// Program to find the grade of a studentusing switch case.#includeint grade(float);int main(){float n;printf(" Enter your marks :- ");scanf("%f",&n);if(n=0)grade(n);elseprintf(" Wrong input !!! Maximum marksis 100 and minimum marks is 0. \n ");return 0;}int grade(float n){int n1=n/10;switch(n1){case 10:case 9: printf(" Grade S \n");break;case 8: printf(" Grade A \n");break;case 7: printf(" Grade B \n");break;case 6: printf(" Grade C \n");break;case 5: printf(" Grade D \n");break;case 4:case 3:case 2:case 1:case 0: printf(" Grade F \n");break;default: printf(" Wrong input !!!Maximum marks is 100 and minimummarks is0. \n ");}return 0;}OUTPUT :-Enter your marks :- 84Grade AQ.21. Write a program to calculate thesum and average of even and oddnumbers inan array?.Date 14-02-2014.// Program to calculate the sum andaverage of even and odd numbers in anarray#includevoid odd(int [],int);void even(int [],int);main(){int i,j=0,k=0,n,a[50],b[50],c[50];printf(" Enter the number ofelements you want to enter(max 50) :-");scanf("%d",&n);if(n0){for(i=0;ip;i--)a[i]=a[i-1];a[p]=n1;n++;printf(" The array after insertion is :- ");show(a,n);printf("\n\n");delete(a,n);}void delete(int a[],int n){int p,i;printf(" Enter the position of elementyou want to delete :- ");scanf("%d",&p);for(i=p;i
Recommended