6. Write a program to find the value of y for a particular value of n. The a, x, b, n is input by user if n=1 y=ax%b if n=2 y=ax2+b2 if n=3 y=a-bx if n=4 y=a+x/b
SOURCE CODE:
Using nested if else:
#include <stdio.h>
int main()
{ int a,x,b,n;
float y;
printf("enter the value of a,x,b,n\n");
scanf("%d %d %d %d",&a,&x,&b,&n);
if(n==1)
{ y=(a*x)%b; }
else if(n==2){
y=a*x*x+b*b;
}
else if(n==3)
{ y=a-b*x; }
else if(n==4){
y=a+(x/b);
}
printf("value of y is %f",y);
return 0;
}
Using switch case:
#include <stdio.h>
int main()
{ int a,x,b,n;
float y;
printf("enter the value of a,x,b,n\n");
scanf("%d %d %d %d",&a,&x,&b,&n);
switch (n)
{ case 1:
y=(a*x)%b;
printf("y=%f",y);
break;
case 2:
y=(a*x*x+b*b);
printf("y=%f",y);
break;
case 3:
y=a-b*x;
printf("y=%f",y);
break;
case 4:
y=a+(x/b);
printf("y=%f",y);
break;
}
return 0;
}
Comments
Post a Comment