1. Maximum of two numbers :
#include<stdio.h>#include<conio.h>
int main()
{
int a,b;
printf("enter the value of number");
scanf("%d %d",&a,&b);
if(a>b)
{
printf("%d is maximum",a);
}
else
{
printf("%d is maximum",b);
}
getch();
}
Output:
Here we use if else statement to find maximum among numbers.
if(a>b), which is format and when the statement is true ,'a' will be print out as the maximum and if the statement is false the we use else that means if 'a' isnot maximum, b will be maximum automatically.
2.Maximum among three numbers :
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("enter the value of number");
scanf("%d %d %d",&a,&b,&c);
if(a>b&&a>c)
{
printf("%d is maximum",a);
}
else if(b>c)
{
printf("%d is maximum",b);
}
else
{
printf("%d is maximum",c);
}
getch();
}
Output:
Here there are three variables 'a,b,c' so (a>b&&a>c) is format
as a is greater than b and c
and b is greater than c and at last c is greater if a and b are not greater.
Remember we had supposed a is greater than b &c. You can suppose any variable greater .
3.Maximum of four numbers :
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,d;
printf("enter the value of number");
scanf("%d %d %d %d",&a,&b,&c,&d);
if(a>b&&a>c&&a>d)
{
printf("%d is maximum",a);
}
else if(b>c&&b>d)
{
printf("%d is maximum",b);
}
else if(c>d)
{
printf("%d is maximum",c);
}
else
{
printf("%d is maximum",d);
}
getch();
}
Output:
4. Maximum of five numbers:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,d,e;
printf("enter the value of number");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
if(a>b&&a>c&&a>d&&a>e)
{
printf("%d is maximum",a);
}
else if(b>c&&b>d&&b>e)
{
printf("%d is maximum",b);
}
else if(c>d&&c>e)
{
printf("%d is maximum",c);
}
else if(d>e)
{
printf("%d is maximum",d);
}
else
{
printf("%d is maximum",e);
}
getch();
}
Output:
5.To find swap between two number :
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
printf("Enter the number");
scanf("%d %d",&x,&y);
x=x+y;
y=x-y;
x=x-y;
printf("The swap number is %d %d",x,y);
getch();
}
Output:
Here we have to interchange the number and the value of number assign from right to left. To clarify
x=x+y;
Here the outcome value of sum of X+Y is assigned to x
and again same rule continues to
y=x-y;&x=x-y
6.To find the sine and change into radian :
sinx=x-x^3/3! + x^5/5! -x^7/7! +.......+n/n!
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,j,fact,P,n,x;
float sum=0,term;
printf("Enter the value of n");
scanf("%d",&n);
printf("Enter the value of x");
scanf("%d",&x);
x=x*(22/7)/180;
for(i=1; i<=n; i++)
{
fact=1;
P=2*i-1;
for(j=1; j<=P; j++)
{
fact=fact*j;
term=pow(x,P)/(fact) *pow(-1,i+1);
sum=sum+term;
printf("Sine value is %f\n",sum);
}
}
getch();
}
It may look like confusing but I will explain to clear your doubt.
Here we have written float because float is like integer type.
But float gives the output even after .00000000 as we have to find the sine value in radian.
ie. float=3/2=1.500000 ,here the number event after ' .' is resulted as output and after' .' there must be 6 digits ie '.000000'
and in integer(int)=3/2=1 as only 1 is integer.
For better clarification:
float/float=float;
int/int=int;
float/int=float;
x=x*(22/7)/180
It is to covert degree into radian.
We have to find till the 'n' number of terms so we have used n and we have input fact because we have to find the factoria +l number.
fact=1
P=2*i-1;
Here every factorial ends at 1 ie 3!=3*2*1
and P=2*i-1 because P indicates power that means x^3 so we can get power of 3 of x if 2*2-1 ie 3 .
Because i has started from 1 and P=2*1-1=1=x^1(power of x is 1)
& P=2*2-1=3=x^3
&P=2*3-1=5=x^5
and continues to n.
term=pow(x,P)/fact *pow(-1,i+1)
Here pow(x,P) means the power of x and
pow(-1,i+1)because after + there is - so when if i=1, i+1=2 ie -1^2 which means further series is even or positive and if
sinx=x-x^3/3! + x^5/5!
Here the sign is +
7.cosx or cosine in radian :
cosx= 1- x^2/2! + x^4/4! - x^6/6!+......n/n!
here this means
cosx= x^0/0! -x^2/2! + x^4/4! - x^6/6!+...n/n!
We can write 1= x^0/0! as
x^0=1 and 0!=1
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,j,fact,P,n,x;
float sum=0,term;
printf("Enter the value of n");
scanf("%d",&n);
printf("Enter the value of x");
scanf("%d",&x);
x=x*(22/7)/180;
for(i=0;i<=n-1;i++)
{
fact=1;
P=2*i;
for(j=1;j<=P;j++)
{
fact=fact*j;
term=pow(x,P)/(fact) *pow(-1,i);
sum=sum+term;
printf("Cosine value is %f\n",sum);
}
}
getch();
}
Here as our value of factorial starts from "0" so we have i=0 amd to compensate i<=n-1.
No comments :
Post a Comment