Skip to main content

Posts

Showing posts with the label while loop

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 print reverse number of given number using while loop

//Write a program to print reverse number of given number using while loop #include <stdio.h> #include <conio.h> void main() { int n,a,r=0; clrscr(); printf( "Enter any number:\n" ); scanf( "%d" &n); 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; } printf( "Reverse = %d" ,r); getch(); } /*For example we input a number 123 then a = 123%10 = 3 r = 0*10 + 3 = 3 n = 123/10 = 12 a = 12%10 = 2 r = 3*10 + 2 = 32 n = 12/10 = 1 a = 1%10 = 1 r = 32*10 + 1 = 321 n = 1/10 = 0 while n become zero while loop stop then print r=321 which is reverse of 123.*/

Write a program to print 4,7,10,13,.........less than 50 using while looping

//Write a program to print 4,7,10,13,.........less than 50 using while looping #include <stdio.h> #include <conio.h> void main() { int x=4; //set x= 4 because according to question, we have to start from 4 clrscr(); while (x<50) //run the loop while x is less than 50 { printf( "%d\n" ,x); x=x+3; //because 7-4=3, 10-7=3, 13-10=3,..... } getch(); }

Write a program to print multiplication table of any number using while looping.

//Write a program to print multiplication table of any number using while looping. #include <stdio.h> #include <conio.h> void main() { int x=1,n; clrscr(); printf( "Enter any number:\n" ); scanf( "%d" ,&n); while (x<=10) //Multiple 35 by 2 because because upto 70 there is 35 odd numbers and 35 even numbers { printf( "%dx%d=%d" ,n,x,n*x); //Eg : nx1, nx2, nx3, ..... x++; //increase x by 1 } getch(); }

Write a program to print even square series upto 50 term using while looping

//Write a program to print even square series upto 50 term using while looping. #include <stdio.h> #include <conio.h> void main() { int x=2; clrscr(); while (x<=100) //Multiple 35 by 2 because because upto 70 there is 35 odd numbers and 35 even numbers { printf( "%d" ,x*x); //Eg : 2x2, 3x3, 4x4, ..... x=x+2; //Eg : 2+2=4, 4+2=6, 6+2=8, ...... } getch(); }

Write a program to print 1 to 50 term of natural number & sum by using while loop

//Write a program to print 1 to 50 term of natural number & sum by using while loop #include <stdio.h> #include <conio.h> void main() { int x=1,sum=0; clrscr(); while (x<=50) //While loop run if the condition remains true { printf( "%d" ,x); sum = sum + x; x=x+1; } printf( "Sum = %d" ,sum); //Sum is printed at the last getch(); }