Skip to main content

Posts

Showing posts with the label if statement

WAP to ask n number from user sort them in assending order and display

//WAP to ask n number from user sort them in assending order and display #include <stdio.h> #include <conio.h> void main() { int i,j,n,num[100],temp; clrscr(); printf( "Enter any value of n:\n" ); scanf( "%d" ,&n); printf( "Entre any value in array:\n" ); scanf( "%d" ,&num[i]); for (i=0;i<n;i++) scanf( "%d" ,&num[i]); for (i=0;i<n;i++) { for (j=i+1;j<n;j++) { if (num[i]>num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } } } printf( "Sorting element of array:" ); for (i=0;i<n;i++) printf( "%d" ,num[i]); getch(); }

WAP to input n number in array & find largest number

//WAP to input n number in array & find largest number #include <stdio.h> #include <conio.h> void main() { int i,num[100],g,n; clrscr(); printf( "Enter any value of n:\n" ); scanf( "%d" ,&n); printf( "Entre any value in array:\n" ); scanf( "%d" ,&num[i]); for (i=0;i<n;i++) scanf( "%d" ,&num[i]); g=num[0]; for (i=0;i<n;i++) { if (num[i]>g) g=num[i]; } printf( "Greater num = %d" ,g); getch(); }

WAP to input 30 number in array & count how many are even and how many are odd.

//WAP to input 30 number in array & count how many are even and how many are odd. #include <stdio.h> #include <conio.h> void main() { int i,num[30],ec=0,oc=0; clrscr(); printf( "Enter value in array:\n" ); for (i=0;i<30;i++) scanf( "%d" ,&num[i]); for (i=0;i<30;i++) { if (num[i]%2==0) ec++; else oc++; } printf( "No of even = %d" ,ec); printf( "No of odd = %d" ,oc); getch(); }

Write a program to check the number is prime or not

//Write a program to check the number is prime or not #include <stdio.h> #include <conio.h> void main() { int i,n,count=0; clrscr(); printf( "Enter any number:\n" ); scanf( "%d" ,&n); for (i=2;i<=n/2;i++) { if (n%i==0) { count++; break ; } } if (coutn==0) printf( "%d is prime number" ,n); else printf( "%d is not prime number" ,n); getch(); }

Write a program to calculate n term of Fibonacci series using for loop 0,1,1,2,3,5,8.....

//Write a program to calculate n term of Fibonacci series using for loop 0,1,1,2,3,5,8,..... #include <stdio.h> #include <conio.h> void main() { int n,i,a,b,c=0; clrscr(); printf( "Enter value of n:" ); scanf( "%d" ,&n); a=0; b=1; if (n=1) printf( "0" ); else if (n>=2) { printf( "0,1" ); for (i=1;i<n-1;i++) { c=a+b; printf( "%d" ,c); a=b; b=c; } } getch(); }

Write a program to print sum of all number between 100 to 350 which is divisible by 9

//Write a program to print sum of all number between 100 to 350 which is divisible by 9 #include <stdio.h> #include <conio.h> void main() { int i,sum=0; clrscr(); for (i=100;i<=350;i=++) //run loop from 100 to 350 { if (i%9==0) //add only if the number is divisible by 9 { sum = sum + i; } } printf( "Sum = %d" ,sum); getch(); }

Write a program to check a number is palindrom or not

//Write a program to check a number is palindrom or not #include <stdio.h> #include <conio.h> void main() { int n,a,r=0,temp; clrscr(); printf( "Enter any number:\n" ); scanf( "%d" &n); temp = n; // copy number to temp while (n) //run loop until n have some value { a=n%10; //from this we get last digit r=r*10+a; n=n/10; } if (temp==r) printf( "%d is Palindrom" ,temp); else printf( "%d is not Palindrom" ,temp); getch(); } /*For example we input a number 121 then a = 121%10 = 1 r = 0*10 + 1 = 1 n = 121/10 = 12 a = 12%10 = 2 r = 1*10 + 2 = 12 n = 12/10 = 1 a = 1%10 = 1 r = 12*10 + 1 = 121 n = 1/10 = 0 temp is equal to r, so that 121 is Palindrom*/

Write a program to check largest number among three number by using Nested if else

//Write a program to check largest number among three number by using Nested if else #include <stdio.h> #include <conio.h> void main() { int a,b,c; clrscr(); printf( "Enter any three number:\n" ); scanf( "%d%d%d,&a&b&c" ); if (a>b) { if (a>c) printf( "Largest=%d" ,a); else printf( "Largest=%d" ,c);} else { if (b>c) printf( "Largest=%d" ,b); else printf( "Largest=%d" ,c);} getch(); }

WAP to enter three numbers and find middle number among them.

// WAP to enter three numbers and find middle number among them. #include<stdio.h> #include<conio.h> void main ( ) { int a , b , c ; clrscr ( ) ; printf ( "Enter three number: \n " ) ; scanf ( "%d%d%d" ,& a ,& b ,& c ) ; if ( ( a > b && a < c ) || ( a > c && a < b ) ) printf ( "%d is middle number" , a ) ; else if ( ( b > a && b < c ) || ( b > c && b < a ) ) printf ( "%d is middle number" , b ) ; else printf ( "%d is middle number" , c ) ; getch ( ) ; } Output:

WAP to enter three numbers and find largest among them.

// WAP to enter three numbers and find largest among them.     #include<stdio.h> #include<conio.h> void main ( ) { int a , b , c ; clrscr ( ) ; printf ( "Enter three number: \n " ) ; scanf ( "%d%d%d" ,& a ,& b ,& c ) ; if ( a > b && a > c ) printf ( "%d is largest" , a ) ; else if ( b > a && b > c ) printf ( "%d is largest" , b ) ; else printf ( "%d is largest" , c ) ; getch ( ) ; }  

WAP to check number is positive or negative.

// WAP to check number is positive or negative.     #include<stdio.h> #include<conio.h> void main ( ) { int x ; clrscr ( ) ; printf ( "Enter any number: \n " ) ; scanf ( "%d" ,& x ) ; if ( x > 0 ) printf ( "%d is positive number" , x ) ; else printf ( "%d is negative number" , x ) ; getch ( ) ; }  

WAP to check number is even or odd.

// WAP to check number is even or odd.     #include<stdio.h> #include<conio.h> void main ( ) { int x ; clrscr ( ) ; printf ( "Enter any number: \n " ) ; scanf ( "%d" ,& x ) ; if ( x % 2 == 0 ) printf ( "%d is even" , x ) ; else printf ( "%d is odd" , x ) ; getch ( ) ; }