Program using While Loop
Unlike most of the computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition in C or C++. The while loop can be thought of as a repeating if statement. Here is some program using while Loop:
-
C Program to return reverse of a three digit number:
Assume that you have input a number 123 to computer and computer return it to 321. we know that if a number divided by 10 then its reminder is always the last digit of inputted number. (123%10 =3, 12%10=2)
#include<stdio.h> void main() { int number,reverse=0,reminder; clrscr(); printf("Enter any three digits value: "); scanf("%d",&number); while(number>0) { reminder=number%10; reverse=(reverse*10)+reminder; number=number/10; } printf("Reverse value is %d",reverse); }
Output:
-
C Program to find Armstrong number between a range :
An Armstrong number is the sum of the cubes of its digits and is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
#include<stdio.h> void main() { int i=0,number, reminder, armstrong=0,term; clrscr(); printf("Enter term to search Armstrong:"); scanf("%d", &term); while(i<=term) { number=i; while(number>0) { reminder=number%10; armstrong =armstrong +(reminder*reminder*reminder); number=number/10; } if(armstrong ==i) printf("Armstrong No is %d",i) i++; } }
Output:
-
C Program to check a given number whether palindrome or not.
A palindrome is a word, phrase, number, or other sequences of characters which read the same backwards or forward. Allowances may be made for adjustments to capital letters, punctuation, and word dividers. Examples in English include “A man, a plan, a canal, Panama!”, “Amor, Roma”, “race car”, “stack cats”, “step on no pets”, “taco cat”, “put it up”, “Was it a car or a cat I saw?” and “No ‘x’ in Nixon” ,”121″ etc.
In case of number we use reverse method:
#include<stdio.h> void main() { int num,value,reminder,tmp; printf("Enter any number to check palindrome"); scanf("%d",&num); tmp=num; while(tmp>0) { reminder=tmp%10; value=value*10+reminder; tmp=tmp/10; } if (num==value) { printf("Entered Number %d is a palidrome number",num); } else { printf("Entered Number %d is not apalindrome number",num); } }
In case of String we use string function substr()
#include<stdio.h> #include<string.h> void main() { char word[100],rev[100]; clrscr(); printf("Enter any word: "); scanf("%s", &word); strcpy(rev,word); if (strcmp(word,strrev(rev))==0) { printf("The word is palindrome"); } else { printf("The word is not a palindrome"); } getch(); }
Subroto Mondal
Latest posts by Subroto Mondal (see all)
- Installing and Configuring OpenShift: A Step-by-Step Guide for Linux Experts with Terminal Code - February 19, 2023
- Sed Command in Linux with Practical Examples - February 19, 2023
- Rsync Command uses with examples - February 19, 2023