Understanding WHILE and DO-WHILE loop? Learn syntax for WHILE and DO-WHILE Loop and different programs on WHILE and DO-WHILE loop.
- 025. Write a Program to print numbers 1 to n using while loop in C language
#include<stdio.h> #include<conio.h> main() { int i=1,n; clrscr(); printf("Enter n : "); scanf("%d",&n); while(i<=n) { printf("%d\t",i); i++; } getch(); }
- 026. Write a Program to print numbers n to 1 using Do While loop in C language
#include<stdio.h> #include<conio.h> main() { int i=1,n; clrscr(); printf("Enter n : "); scanf("%d",&n); i=n; do { printf("%d\t",i); i--; }while(i>=1); getch(); }
- 027. Write a Program to print first n even numbers in C language
#include<stdio.h> void main() { int i=1,n,counter=2; printf("Enter n : "); scanf("%d",&n); while(i<=n) { printf("%d\t",counter); counter = counter + 2; i++; } }
- 028. Write a Program to print first n odd numbers in C language
#include<stdio.h> void main() { int i=1,n,counter=1; printf("Enter n : "); scanf("%d",&n); while(i<=n) { printf("%d\t",counter); counter = counter + 2; i++; } }
- 029. Write a Program to accept a number and print the number in reverse order. E.g. if 1324 is the number then the output will be 4231 in C language
#include<stdio.h> #include<conio.h> main() { int rem,n; clrscr(); printf("Enter n : "); scanf("%d",&n); while(n>0) { rem=n%10; printf("%d",rem); n=n/10; } getch(); }
- 030. Write a Program to accept a number and print sum of it’s digits in C language
#include<stdio.h> #include<conio.h> main() { int rem,sum=0,n clrscr(); printf("Enter n : "); scanf("%d",&n); while(n>0) { rem=n%10; sum=sum+rem; n=n/10; } printf("Sum of digits....%d",sum); getch(); }
- 031. Write a program to accept a number from user and check if it is Armstrong number or not i.e. 153 = 1^3 + 5^3 + 3^3 = 153 in C language
#include #include main() { int temp,rem,sum=0,n; clrscr(); printf("Enter n : "); scanf("%d", &n); temp=n; while(n > 0) { rem=n%10; sum=sum+rem*rem*rem; n=n/10; } if(temp==sum) printf("Armstrong Number"); else printf("Not an Armstrong Number"); getch(); }