Sie sind auf Seite 1von 17

Q.1. Write a program to perform all the arithmetic operations i.e.

addition,subtraction,multiplication,division?
Date :- 10-1-2014.
// Program to perform arithmetic operations like addition,subtraction,multiplication and
division.
#include<stdio.h>
int add(int,int);
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)
{
return a+b;
}
int sub(int a,int b)
{
if(a>b)
return a-b;
else
return b-a;
}
int product(int a,int b)
{
return a*b;
}
float quotient(int a,int b)
{
if(a>b)
return (float)a/b;
else
return (float)b/a;
}

Q.2. Write a program to calculate the area and perimeter of trapezoid, triangle and circle
and area of ellipse?
Date :- 17-1-2014.
// 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/2-z));
printf("\n The area of the given triangle is :- %f and perimeter is :- %f \n",a4,p3);
return 0;
}
Q.3. Write a program to find the solution of a quadratic equation?
Date :- 17-1-2014.
// Program to find the solution of a quadratic equation.
#include<stdio.h>
#include<math.h>
double quadratic(int,int,int);
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)
{
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;
}
Q.4. Write a program to calculate largest and smallest of three integers using ternary
operator?
Date :- 17-1-2014.
// 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;
}
Q.5. Write a program to find the category of a triangle using ternary operator?
Date :- 17-1-2014.
// Program to find the category of a triangle whose sides are given using ternary
operator.
#include<stdio.h>
int trianglecategory(float,float,float);
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)
{
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;
}

Q.6. Write a program to convert temperature from Celsius to Fahrenheit and vice versa?
Date :- 17-1-2014.
// 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;
}

Q.7. Write a program to find out the category of a triangle using if-else.
Date :- 24-1-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;
}
// 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;
}

// 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;
}
/* 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)
{
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;
}
// 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));
}

// 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;
}

// 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));

}
}

// Program to print the fibonacci series upto a certain number


#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;
}

//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;
}

//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;
}

// 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;
}

/* Program to print the following pattern upto 'n' which is entered by the user :5
54321
1
54
5432
21
543
543
321
5432
54
4321
54321
5
54321
*/
#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;
}

/* Program to print the following patterns upto 'n' which is entered by the user :1
1
1
1
121
212
12
21
12321
32123
123321
*/
#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;
}

/* Program to display the folowing pattern where 'n' is the last limit taken as input from
the user:123321
12321
where n=3
12
21
12 21
1
1
1
1
1
1
12 21
12
21
12321
123321
*/
#include<stdio.h>
void pattern1(int);
void pattern2(int);
int main()
{
int n;
printf(" Enter the value of 'n'(a one digit integer) :- ");
scanf("%d",&n);
pattern1(n);
printf("\n\n");
pattern2(n);
return 0;
}
void pattern1(int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=(n-i+1);j++)
printf("%d ",j);
for(j=1;j<=2*(i-1);j++)
printf(" ");
for(j=(n-i+1);j>=1;j--)
printf("%d ",j);
printf("\n");
}
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d ",j);
for(j=1;j<=2*(n-i);j++)
printf(" ");
for(j=i;j>=1;j--)
printf("%d ",j);

printf("\n");
}
}
void pattern2(int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=(n-i+1);j++)
printf("%d ",j);
for(j=1;j<2*(i-1);j++)
printf(" ");
if(i==1)
for(j=(n-i);j>=1;j--)
printf("%d ",j);
else
for(j=(n-i+1);j>=1;j--)
printf("%d ",j);
printf("\n");
}
for(i=1;i<=n;i++)
{
if(i==1)
continue;
else
{
for(j=1;j<=i;j++)
printf("%d ",j);
for(j=(2*(n-i)-1);j>=1;j--)
printf(" ");
if(i==n)
for(j=i-1;j>=1;j--)
printf("%d ",j);
else
for(j=i;j>=1;j--)
printf("%d ",j);
printf("\n");
}
}
}

//Program to perform addition,subtraction,multiplication,division using switch-case.


#include<stdio.h>
int add(int,int);
int sub(int,int);
int multi(int,int);
float div(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 and '4'
for division :- ");
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;
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)
{
if(a>b)
return (float)a/b;
else
return (float)b/a;
}

Das könnte Ihnen auch gefallen