Algorithm For Greatest of Two Numbers
Algorithm For Greatest of Two Numbers
1.
Get two values A and B from user If A>B, display A is greater than B goto 4 Display B is greater than A end
2. 3. 4.
main() { Int a,b,c; Printf(enter values of a and b:); Scanf(%d%d,&a,&b); c=a>b? a : b ; Printf(%d is greatest,c); }
Using If -else main() { Int a,b; Printf(enter values of a and b:); Scanf(%d%d,&a,&b); If(a>b) { Printf(a is greatest); } Else { Printf(b is greatest); } }
Algorithm for checking odd or even 1. 2. 3. 4. Get values of A from user If A%2 is 0, display A is even goto 4 Display A is ODD end
main() { Int a; Printf(enter value of a:); Scanf(%d,&a); If(a%2==0) { Printf(a is even); } Else { Printf(b is odd); } }
Get values of A and B from user If A/B greater than 0, display B is smallest goto 4 Display A is smallest end
3.
4.
Algorithm to find Greatest of three numbers 1. 2. 3. 4. 5. Get three values A , B and C from user If A>B and A>C, display A is greatest goto 5 if B>A and B>C, display B is greatest goto 5 Display C is greatest end
main() { Int a,b,c; Printf(enter values of a,b and c:); Scanf(%d%d%d,&a,&b,&c); If(a>b && a>c) { Printf(a is greatest); } Else if(b>a && b>c) { Printf(b is greatest); } Else { Printf(c is greatest); }
} Nested If Main() { int a ,b ,c ; printf("n Enter the three numbers "); scanf(" %d %d %d", &a,&b,&c); if (a>b) { if(a>c) { printf(" A=%d is greatest",a); } else { printf(" C= %d is greatest",c); } } else if(b>c) {
#include <stdio.h> main() { int i,j,big; scanf("%d%d",&i,&j); big = i; if(big < j) { big = j; } printf("biggest of two numbers is %d ",big); }
Perform addition, subtraction, multiplication and division based on the choice given by the user
main() {
int a,b,c,d; printf("Enter two numbers "); scanf(" %d %d, &a,&b); first: printf(" Enter the choice 1.add 2.sub 3.mul 4.div "); scanf(" %d &d); if(d==1) { c=a+b; printf("the sum is %d ", c); } Else if(d==2) { c=a-b; printf("the - is %d ", c); } Else if(d==3) {
c=a*b; printf("the * is %d ", c); } Else if(d==4) { c=a/b; printf("the / is %d ", c); } Else { printf("improper choice); goto first; } Getch(); }
Switch Case
Main() { int a,b,c,flag= 0; char sym; printf("enter two no); scanf("%d%d",&a,&b); printf("enter arithmatic symbol"); scanf(" %c",&sym); switch(sym) { case '+' : c= a+b; break; case '-' : c= a-b; break; case '*' : c= a*b; break; case '/' : c= a/b; break;
case '%' : c= a%b; break; default : printf(" not an arithmatic symbol"); flag= 1; } if (flag==0) printf("c= %d",c); getch(); }