Vous êtes sur la page 1sur 37

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<stdio.h>
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<stdio.h>
#include<math.h>
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/2z));
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<stdio.h>
#include<math.h>
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)<0)
printf("\n Roots are imaginary \n ");
else
{
x1=(-(double)b+sqrt(b*b-4*a*c))/(2*a);
x2=(-(double)b-sqrt(b*b-4*a*c))/(2*a);
printf("\n The roots of the equation
%dx^2+%dx+%d=0 are :- %lf and %lf
\n",a,b,c,x1,x2);
}
return 0;
}
OUTPUT :Enter the value of 'a' in the quadratic
equation :- 1
Enter the value of 'b' in the quadratic
equation :- 3
Enter the value of 'c' in the quadratic
equation :- 2
The roots of the equation 1x^2+3x+2=0
are :- -1.000000 and -2.000000
Q.4. Write a program to calculate largest
and smallest of three integers
using ternary
operator?
// Program to calculate largest and
smallest of three integers using
ternary operator
#include<stdio.h>
int smalllarge(int,int,int);
int main()
{
int a,b,c;
printf(" Enter the first integer :- ");
scanf("%d",&a);
printf("\n Enter the 2nd integer :- ");

scanf("%d",&b);
printf("\n Enter the 3rd integer :- ");
scanf("%d",&c);
smalllarge(a,b,c);
return 0;
}
int smalllarge(int a,int b,int c)
{
int l=(a>b && a>c)?a:((b>c)?b:c);
int s=(a<b && a<c)?a:((b<c)?b:c);
printf("\n Largest number out of %d,%d
and %d is :- %d ",a,b,c,l);
printf("\n Smallest number out of %d,%d
and %d is :- %d \n",a,b,c,s);
return 0;
}
OUTPUT :Enter the first integer :- 4
Enter the 2nd integer :- 3
Enter the 3rd integer :- 6
Largest number out of 4,3 and 6 is :- 6
Smallest number out of 4,3 and 6 is :- 3
Q.5. Write a program to find the
category of a triangle using ternary
operator?
Date :- 17-01-2014.
// Program to find the category of a
triangle whose sides are given using
ternary operator.
#include<stdio.h>
int trianglecategory(float,float,float); /* function prototype declaration*/
int main()
{
float a,b,c;
printf(" Enter the first side of the
triangle :- ");
scanf("%f",&a);
printf("\n Enter the second side of the
triangle :- ");
scanf("%f",&b);
printf("\n Enter the third side of the
triangle :- ");
scanf("%f",&c);
trianglecategory(a,b,c);

return 0;
}
int trianglecategory(float a,float b,float
c)
/* function for nature of trisngle*/
{
int l=(a==b && a==c)?printf("\n The
triangle is equilateral \n"):((a==b
||a==c || b==c)?printf("\n The triangle
is isosceles \n"):printf("\n The
triangle is scalene \n"));
return 0;
}
OUTPUT :Enter the first side of the triangle :- 4
Enter the second side of the triangle :- 3
Enter the third side of the triangle :- 3
The triangle is isosceles
Q.6. Write a program to convert
temperature from Celsius to Fahrenheit
and
vice
versa?
// Program to convert temperature from
celsius to Fahrenheit and vice versa
#include<stdio.h>
float celfahr(float);
float fahrcel(float);
int main()
{
float c,f;
int i;
printf(" Press '1' if you want celsius to
fahrenheit conversion and \n '2'
if you want fahenheit to celsius
conversion :- ");
scanf("%d",&i);
if(i==1)
{
printf("\n Enter the temperature in
celsius :- ");
scanf("%f",&c);
f=celfahr(c);
printf("\n Temperature in fahrenheit

is :- %f\n",f);
}
else if(i==2)
{
printf("\n Enter the temperature in
fahrenheit :- ");
scanf("%f",&f);
c=fahrcel(f);
printf("\n Temperature in
celsius is :- %f\n",c);
}
else
printf("\n Wrong choice \n");
return 0;
}
float celfahr(float c)
{
float f=(1.8*c+32.0);
return f;
}
float fahrcel(float f)
{
float c=(f-32)/1.8;
return c;
}
OUTPUT :Press '1' if you want celsius to fahrenheit
conversion and
'2' if you want fahenheit to celsius
conversion :- 1
Enter the temperature in celsius :- 37
Temperature in fahrenheit is :98.599998
Q.7. Write a program to find out the
category of a triangle using if-else?
Date :- 24-01-2014.
// Program to find out the category of a
triangle using if-else.
#include<stdio.h>
int trianglecategory(float,float,float);
int main()
{
float a,b,c;
printf(" Enter the length of the first

side :- ");
scanf("%f",&a);
printf(" Enter the length of the second
side :- ");
scanf("%f",&b);
printf(" Enter the length of the third
side :- ");
scanf("%f",&c);
trianglecategory(a,b,c);
return 0;
}
int trianglecategory(float a,float b,float
c)
{
if(a==b && b==c)
printf(" The triangle is equilateral \n");
else if(a==b || b==c || a==c)
printf(" The triangle is isosceles \n");
else
printf(" The triangle is scalene \n");
return 0;
}
OUTPUT :Enter the length of the first side :- 4
Enter the length of the second side :- 5
Enter the length of the third side :- 3
The triangle is scalene
Q.8. Write a program to find the grade
of a student using if-else and
logical
operators?
Date-24-01-2014.
// Program to find the grade of a student
using if-else and logical
operators.
#include<stdio.h>
int grade(float);
int main()
{
int n;
printf(" Enter your marks :- ");
scanf("%d",&n);
grade(n);
return 0;

}
int grade(float n)
{
if(n>100 && n<0)
printf(" Sorry!!! Maximum
marks is 100 and minimum marks is
0. \n ");
else if(n<50 && n>=0)
printf(" Grade : F \n");
else if(n>=50 && n<60)
printf(" Grade : E \n");
else if(n>=60 && n<70)
printf(" Grade : C \n");
else if(n>=70 && n<80)
printf(" Grade : B \n");
else if(n>=80 && n<90)
printf(" Grade : A \n");
else if(n>=90 && n<=100)
printf(" Grade : S \n");
return 0;
}
OUTPUT :Enter your marks :- 69
Grade : C
Q.9. Write a program to find the grade
of a student using if-else and
without using
logical operators?
Date-24-01-2014.
// Program to find the grade of a student
using if-else without using
logical operator.
#include<stdio.h>
int grade(float);
int main()
{
int n;
printf(" Enter your marks :- ");
scanf("%d",&n);
grade(n);
return 0;
}
int grade(float n)
{

if(n>100)
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<50)
printf(" Grade : F \n");
return 0;
}
OUTPUT :Enter your marks :- 58
Grade : D
Q.10. Write a program to calculate the
sum of digits and if the sum exceeds
one digit
then the sum of digits of the digits of the
sum and to check whether the
given
number is a palindrome or not?
Date 01-02-2014.
/* Program to calculate the sum of digits
and if the sum exceeds one digit
then the sum of digits of the digits of the
sum
and to check whether the given
number is a palindrome or not
*/
#include<stdio.h>
int sumofdigits(int);
int main()
{
int n,s;
printf(" Enter an integer :- ");
scanf("%d",&n);
s=sumofdigits(n);
printf("\n The sum of digits of the

number in one digit is :- %d \n",s);


return 0;
}
int sumofdigits(int n) /* function for sum of digits of a number*/
{
int a,d=0,r,s=0,b=0;
s=n;
while(s>=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");
else
printf("\n The number is not a
palindrome \n");
}
b++;
}
return s;
}
OUTPUT :Enter an integer :- 25652
The number is a palindrome
The sum of digits of the number in one
digit is :- 2
Q.11. Write a program to calculate
factorial of a number with and without
recursion?
Date 01-02-2014.
// Program to calculate factorial of a
number with and without using
recursion
#include<stdio.h>

int fact(int);
int factr(int);
int main()
{
int n;
printf(" Enter the value whose factorial
has to be found out :- ");
scanf("%d",&n);
if(fact(n)==0)
printf("\n Factorial not possible \n");
else
printf("\n The factorial of %d without
recursion is :- %d \n",n,fact(n));
if(factr(n)==0)
printf(" Factorial not possible \n");
else
printf(" The factorial of %d using
recursion is :- %d \n",n,factr(n));
return 0;
}
int fact(int n)
{
int i,f=1;
if(n<0)
return 0;
else if(n==0 || n==1)
return 1;
else
{
for(i=1;i<=n;i++)
f=f*i;
return f;
}
}
int factr(int n)
{
int f;
if(n==0 || n==1)
return 1;
else if(n<0)
return 0;
else
return(n*fact(n-1));
}

OUTPUT :Enter the value whose factorial has to be


found out :- 7
The factorial of 7 without recursion is :5040
The factorial of 7 using recursion is :5040
Q.12. Write a program to calculate the
sum and average of odd and even
numbers in
a given range?
Date 01-02-2014.
// Program to calculate the sum and
average of odd and even numbers in a
given range.
#include<stdio.h>
int sumofoddeven(int,int);
int main()
{
int u,l;
printf(" Enter the upper limit of the
range :- ");
scanf("%d",&u);
printf("\n Enter the lower limit of the
range :- ");
scanf("%d",&l);
sumofoddeven(u,l);
return 0;
}
int sumofoddeven(int u,int l)
{
int i,o=0,e=0,n1=0,n2=0;
float a1,a2;
for(i=l;i<=u;i++)
{
if(i%2==0)
{
e=e+i;
n2++;
}
else
{
o=o+i;
n1++;

}
}
a1=(float)o/n1;
a2=(float)e/n2;
printf("\n The sum of odd nos in the
range is :- %d and the average of odd
nos in the range is :- %f \n ",o,a1);
printf("\n The sum of even nos in the
range is :- %d and the average of
even nos in the range is :- %f \n",e,a2);
return 0;
}
OUTPUT :Enter the upper limit of the range :- 10
Enter the lower limit of the range :- 2
The sum of odd nos in the range is :- 24
and the average of odd nos in the
range is :- 6.000000
The sum of even nos in the range is :- 30
and the average of even nos in
the range is :- 6.000000
Q.13. Write a program to calculate the
Greatest common divisor using
recursion and
without recursion?
Date 01-02-2014.
// Program to calculate the Greatest
common divisor using recursion and
without recursion
#include<stdio.h>
int gcd(int,int);
int gcdr(int,int);
int main()
{
int a,b,d;
printf(" Enter the first number :- ");
scanf("%d",&a);
printf(" Enter the second number :- ");
scanf("%d",&b);
d= gcd(a,b);
printf("\n The Greatest common divisor
of %d and %d without using recursion
is :- %d \n",a,b,d);
d=gcdr(a,b);

printf(" The Greatest common divisor of


%d and %d using recursion is :- %d
\n",a,b,d);
return 0;
}
int gcd(int a,int b)
{
int temp;
if(a>b)
{
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;
else
return(gcd(b,temp));
}
else
{
temp=b%a;
if(temp==0)

return a;
else
return(gcd(a,temp));
}
}
OUTPUT :Enter the first number :- 35
Enter the second number :- 6
The Greatest common divisor of 35 and
6 without using recursion is :- 1
The Greatest common divisor of 35 and
6 using recursion is :- 1
Q.14. Write a program to print the
fibonacci series upto a certain number
entered by
the user?
Date 01-02-2014.
// Program to print the fibonacci series
upto a certain number entered by
the user
#include<stdio.h>
int fibonacci(int);
int main()
{
int n;
printf(" Enter the last limit of the
series :- ");
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)<=n)
{
s=a+b;
printf(",%d",s);
a=b;
b=s;
}
printf(".\n");
return 0;

}
OUTPUT :Enter the last limit of the series :- 40
0,1,1,2,3,5,8,13,21,34.
Q.15. Write a program to check whether
a number is prime or not?
Date 07-02-2014.
//Program to check whether a number is
prime or not.
#include<stdio.h>
int prime(int);
int main()
{
int n;
printf(" Enter an integer :- ");
scanf("%d",&n);
prime(n);
return 0;
}
int prime(int n)
{
int i,flag=0;
if(n==1)
printf(" The number %d is a special
number.\n ");
else
{
for(i=2;i<n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==1)
printf(" The no %d is a non prime no\n
",n);
else
printf(" The no %d is a prime no \n",n);
}
return 0;
}
OUTPUT :-

Enter an integer :- 41
The no 41 is a prime no
Q.16. Write a program to find the no of
prime numbers in a range?
Date 07-02-2014.
// Program to find the no of prime
numbers in a range.
#include<stdio.h>
int prime(int,int);
int main()
{
int n1,n2;
printf(" Enter the first limit :- ");
scanf("%d",&n1);
printf(" Enter the last limit :- ");
scanf("%d",&n2);
prime(n1,n2);
return 0;
}
int prime(int n1,int n2)
{
int i,j,s1=0,s2=0;
if(n1<n2)
{
for(j=n1;j<=n2;j++)
{
if(j!=1)
{
for(i=2;i<j;i++)
{
if(j%i==0)
{
s2++;
break;
}
}
s1++;
}
}
}
else
{
for(j=n2;j<=n1;j++)
{

if(j!=1)
{
for(i=2;i<j;i++)
{
if(j%i==0)
{
s2++;
break;
}
}
s1++;
}
}
}
printf(" The number of non prime nos :%d \n",s2);
printf(" The number of prime nos :- %d
\n",(s1-s2));
return 0;
}
OUTPUT :Enter the first limit :- 5
Enter the last limit :- 26
The number of non prime nos :- 15
The number of prime nos :- 7
Q.17. Write a program to print the
following pattern upto 'n' which is
entered by the
user :5 5 4 3 2 1 1 where n=5.
54543221
543543321
5432544321
54321554321
Date 07-02-2014.
/* Program to print the following pattern
upto 'n' which is entered by the
user :5543211
54543221
543543321
5432544321
54321554321
*/

#include<stdio.h>
int structure1(int);
int structure2(int);
int structure3(int);
int main()
{
int n;
printf(" Enter the limit of the structure :");
scanf("%d",&n);
structure1(n);
printf("\n\n");
structure2(n);
printf("\n\n");
structure3(n);
return 0;
}
int structure1(int n)
{
int i,j;
for(i=n;i>=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<=n;i++)
{
for(j=n;j>=i;j--)
printf("%d ",j);
printf("\n");
}
return 0;
}
int structure3(int n)
{
int i,j;
for(i=1;i<=n;i++)
{

for(j=n-i;j>=1;j--)
printf(" ");
for(j=1;j<=i;j++)
printf("%d ",j);
printf("\n");
}
return 0;
}
OUTPUT :Enter the limit of the structure :- 6
6
65
654
6543
65432
654321
654321
65432
6543
654
65
6
1
12
123
1234
12345
123456
Q.18. Write a program to print the
following patterns upto 'n' which is
entered by
the user :1 1 1 1 where n=3.
1212121221
12321321231233
21
Date 07-02-2014.
/* Program to print the following
patterns upto 'n' which is entered by the
user :1111
1212121221
1232132123123321
*/

#include<stdio.h>
int structure1(int);
int structure2(int);
int structure3(int);
int main()
{
int n;
printf(" Enter the last limit of the
structure :- ");
scanf("%d",&n);
structure1(n);
printf("\n\n");
structure2(n);
printf("\n\n");
structure3(n);
return 0;
}
int structure1(int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=n-i;j>=1;j--)
printf(" ");
for(j=1;j<=i;j++)
printf("%d ",j);
for(j=i-1;j>=1;j--)
printf("%d ",j);
printf("\n");
}
return 0;
}
int structure2(int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=n-i;j>=1;j--)
printf(" ");
for(j=i;j>=1;j--)
printf("%d ",j);
for(j=2;j<=i;j++)
printf("%d ",j);
printf("\n");

}
return 0;
}
int structure3(int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d ",j);
for(j=2*(n-i);j>=1;j--)
printf(" ");
for(j=i;j>=1;j--)
printf("%d ");
printf("\n");
}
return 0;
}
OUTPUT :Enter the last limit of the structure :- 5
1
121
12321
1234321
123454321
1
212
32123
4321234
543212345
11
1222
123333
12344444
1234555555
Q.19. Write a program to perform
addition, subtraction, multiplication,
division,
power and modulus using switch-case
and do while(to repeatedly take input
unless user wants to exit)?
Date 14-02-2014.
//Program to perform
addition,subtraction,multiplication,division

using
switch-case.
#include<math.h>
#include<stdio.h>
int add(int,int);
int sub(int,int);
int multi(int,int);
float div(int,int);
int power(int,int);
int mod(int,int);
int main()
{
int a,b,n,i=1;
do
{
printf(" Enter the first integer :- ");
scanf("%d",&a);
printf(" Enter the second integer :- ");
scanf("%d",&b);
printf(" Press '1' for addition, '2' for
subtraction , '3' for
multiplication '4' for division and '5' to
power and '6' to find remainder
:- ");
scanf("%d",&n);
switch(n)
{
case 1: printf(" The sum of %d and %d
is :- %d \n",a,b,add(a,b));
break;
case 2: printf(" The difference between
%d and %d is :- %d
\n",a,b,sub(a,b));
break;
case 3: printf(" The product of %d and
%d is :- %d \n",a,b,multi(a,b));
break;
case 4: printf(" The quotient of %d and
%d is :- %f \n",a,b,div(a,b));
break;
case 5: printf(" %d^%d= %d
\n",a,b,power(a,b));
break;
case 6: printf(" Modulus

of %d and %d :%d\n",a,b,mod(a,b));
break;
default: printf(" Wrong input !!! Please
enter the correct input \n");
}
printf(" Press '1' to coninue and '0' to
exit!!! ");
scanf("%d",&i);
}while(i==1);
return 0;
}
int add(int a,int b)
{
return a+b;
}
int sub(int a,int b)
{
if(a>b)
return a-b;
else
return 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 :- 6
Enter the second integer :- 5
Press '1' for addition, '2' for
subtraction , '3' for multiplication '4'

for division and '5' to power and '6' to


find remainder :- 1
The sum of 6 and 5 is :- 11
Press '1' to coninue and '0' to exit!!! 1
Enter the first integer :- 4
Enter the second integer :- 6
Press '1' for addition, '2' for
subtraction , '3' for multiplication '4'
for division and '5' to power and '6' to
find remainder :- 4
The quotient of 4 and 6 is :- 0.666667
Press '1' to coninue and '0' to exit!!! 0
Q.20. Write a program to find the grade
of a student using switch case?
Date 14-02-2014.
// Program to find the grade of a student
using switch case.
#include<stdio.h>
int grade(float);
int main()
{
float n;
printf(" Enter your marks :- ");
scanf("%f",&n);
if(n<=100 && n>=0)
grade(n);
else
printf(" Wrong input !!! Maximum marks
is 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 minimum
marks is
0. \n ");
}
return 0;
}
OUTPUT :Enter your marks :- 84
Grade A
Q.21. Write a program to calculate the
sum and average of even and odd
numbers in
an array?.
Date 14-02-2014.
// Program to calculate the sum and
average of even and odd numbers in an
array
#include<stdio.h>
void 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 of
elements you want to enter(max 50) :-"
);
scanf("%d",&n);
if(n<=50 && n>0)
{
for(i=0;i<n;i++)
{
printf(" Enter the element
no:%d :- ",(i+1));
scanf("%d",&a[i]);

}
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
b[j]=a[i];
j++;
}
else
{
c[k]=a[i];
k++;
}
}
odd(b,j);
even(c,k);
}
else
printf(" No of elements
must be positive and less
than equal to 50 \n");
}
void odd(int b[],int j)
{
int i,s=0;
float avg;
for(i=0;i<j;i++)
s=s+b[i];
avg=(float)s/j;
printf(" Sum of odd numbers :- %d
and average is %f \n",s,avg);
}
void even(int c[],int k)
{
int i,s=0;
float avg;
for(i=0;i<k;i++)
s=s+c[i];
avg=(float)s/k;
printf(" Sum of odd numbers :- %d
and average is %f \n",s,avg);
}
OUTPUT :Enter the number of elements you want

to enter(max 50) :-5


Enter the element no:1 :- 45
Enter the element no:2 :- 43
Enter the element no:3 :- 66
Enter the element no:4 :- 78
Enter the element no:5 :- 93
Sum of odd numbers :- 144 and average
is 72.000000
Sum of odd numbers :- 181 and average
is 60.333332
Q.22. Write a program to perform
insertion,deletion and dupicate deletion
operation on a 1D array?.
Date 08-03-2014.
// Program to perform insertion,deletion
and dupicate deletion operation
on a 1D array.
#include<stdio.h>
void show(int [],int);
void insert(int [],int);
void delete(int [],int);
void duplicatedel(int [],int);
int main()
{
int i,a[20],n;
printf(" Enter the number of elements
you want to enter in the array :- ");
scanf("%d",&n);
printf(" Enter the elements of the
array :- \n");
for(i=0;i<n;i++)
{
printf(" Enter the element no :%d :- ",(i
+1));
scanf("%d",&a[i]);
}
printf(" The array is :- ");
show(a,n);
printf("\n\n");
insert(a,n);
return 0;
}
void show(int a[],int n)
{

int i;
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
void insert(int a[],int n)
{
int i,n1,p;
printf(" Enter the element you want to
enter :- ");
scanf("%d",&n1);
printf(" Enter the position where you
want to insert the value :- ");
scanf("%d",&p);
for(i=n;i>p;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 element
you want to delete :- ");
scanf("%d",&p);
for(i=p;i<=(n-2);i++)
a[i]=a[i+1];
n--;
printf(" The array after deletion is :- ");
show(a,n);
printf("\n\n");
duplicatedel(a,n);
}
void duplicatedel(int a[],int n)
{
int i,j,k;
for(i=0;i<=(n-2);i++)
for(j=i+1;j<n;j++)
if(a[i]==a[j])
{

for(k=j;k<=(n-2);k++)
a[k]=a[k+1];
n--;
}
printf(" The array after duplicate
deletion is :- ");
show(a,n);
}
OUTPUT :Enter the number of elements you want
to enter in the array :- 5
Enter the elements of the array :Enter the element no :1 :- 3
Enter the element no :2 :- 5
Enter the element no :3 :- 6
Enter the element no :4 :- 3
Enter the element no :5 :- 2
The array is :- 3 5 6 3 2
Enter the element you want to enter :- 2
Enter the position where you want to
insert the value :- 3
The array after insertion is :- 3 5 6 2 3
2
Enter the position of element you want
to delete :- 1
The array after deletion is :- 3 6 2 3 2
The array after duplicate deletion is :- 3
62
Q.23. Program to perform the
addition,product and transpose
operation of
two
matrices?.
Date 08-03-2014.
// Program to perform the
addition,product and transpose
operation of two
matrices.
#include<stdio.h>
void addition(int [][10],int []
[10],int,int,int,int);
void product(int [][10],int []
[10],int,int,int,int);
void transpose(int [][10],int,int);

void show(int [][10],int,int);


int main()
{
int a[10][10],b[10][10],i,j,r1,c1,r2,c2;
printf(" Enter the rows and column of
the first matrix :- ");
scanf("%d %d",&r1,&c1);
printf(" Enter the rows and column of
the second matrix :- ");
scanf("%d %d",&r2,&c2);
printf(" Enter the elements of the first
matrix :- \n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
printf(" Enter element of row no-%d and
column no-%d :- ",(i+1),(j+1));
scanf("%d",&a[i][j]);
}
show(a,r1,c1);
printf(" Enter the elements of the second
matrix :- \n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
printf(" Enter element of
row no-%d and column
no-%d :- ",(i+1),(j+1));
scanf("%d",&b[i][j]);
}
show(b,r2,c2);
addition(a,b,r1,c1,r2,c2);
printf("\n\n");
product(a,b,r1,c1,r2,c2);
printf("\n\n Transpose of first matrix
is :- \n");
transpose(a,r1,c1);
printf("\n\n Transpose of second matrix
is :- \n");
transpose(b,r2,c2);
return 0;
}
void show(int a[][10],int r,int c)
{

int i,j;
printf(" The matrix is :- \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
}
void addition(int a[][10],int b[][10],int
r1,int c1,int r2,int c2)
{
int c[10][10],i,j;
if(r1==r2 && c1==c2)
{
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
c[i][j]=a[i][j]+b[i][j];
printf( After addition \n);
show(c,r1,c1);
}
else
printf(" Addition not possible \n");
}
void product(int a[][10],int b[][10],int
r1,int c1,int r2,int c2)
{
int c[10][10],i,j,k;
if(c1==r2)
{
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<r2;k++)
c[i][j]+=a[i][k]*b[k][j];
}
printf( After multiplication\n);
show(c,r1,c2);
}
else
printf(" Product not possible \n");
}
void transpose(int a[][10],int r,int c)

{
int i,j,d[10][10];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
d[j][i]=a[i][j];
show(d,c,r);
}
OUTPUT :Enter the rows and column of the first
matrix :- 2
2
Enter the rows and column of the second
matrix :- 2
3
Enter the elements of the first matrix :Enter element of row no-1 and column
no-1 :- 3
Enter element of row no-1 and column
no-2 :- 4
Enter element of row no-2 and column
no-1 :- 5
Enter element of row no-2 and column
no-2 :- 2
The matrix is :34
52
Enter the elements of the second
matrix :Enter element of row no-1 and column
no-1 :- 6
Enter element of row no-1 and column
no-2 :- 5
Enter element of row no-1 and column
no-3 :- 4
Enter element of row no-2 and column
no-1 :- 3
Enter element of row no-2 and column
no-2 :- 7
Enter element of row no-2 and column
no-3 :- 2
The matrix is :654
372
Addition not possible

After multiplication
The matrix is :30 43 20
36 39 24
Transpose of first matrix is :The matrix is :35
42
Transpose of second matrix is :The matrix is :63
57
42
Q.24. Write a program to sort elements
of an 1D array in ascending order
using
bubble sort?.
Date - 08-03-2014.
// Program to sort elements of an 1D
array in ascending order using bubble
sort.
#include<stdio.h>
void bubblesort(int [],int); /* function prototype declaration*/
int main()
{
int i,a[20],n;
printf(" Enter the no of elements in the
array :- ");
scanf("%d",&n);
printf(" Enter the elements of the
array :- \n");
for(i=0;i<n;i++)
{
printf(" Enter the element-%d :- ",(i+1));
scanf("%d",&a[i]);
}
bubblesort(a,n);
return 0;
}
void bubblesort(int a[],int n) /* function for sorting*/
{
int i,j,t;
for(i=0;i<=(n-1);i++)
for(j=0;j<=(n-i-2);j++)

if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
printf(" Array after sorting in ascending
order :- \n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
OUTPUT :Enter the no of elements in the array :- 5
Enter the elements of the array :Enter the element-1 :- 3
Enter the element-2 :- 4
Enter the element-3 :- 2
Enter the element-4 :- 9
Enter the element-5 :- 4
Array after sorting in ascending order :23449