C PROGRAMMING LOOPS
While writing programs, we frequently come across situations where a particular task is to be repeated a number of times. In such a cases the use of loops helps save time and effort. Loop is also a kind of control structure. A loop is also used to execute a particular set of statements a fixed number of times or until a specific condition has been met. Loops cause program to execute the certain block of code repeatedly until test condition is false. Loops are used in performing repetitive task in programming.
There are 3 types of loops in C programming:
- for loop
- while loop
- do...while loop
For loop
In for loop control statement, loop is executed until condition becomes false. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
For ( initialization statement ; test expression ; update statement )
The initialization statement is executed only once at the beginning of the for loop. Then the test expression is checked by the program. If the test expression is false, for loop is terminated. But if test expression is true then the codes inside body of for
loop is executed and then update expression is updated. This process repeats until test expression is false.
1. Write a program to print 1 to 10 numbers.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num ;
clrscr ( ) ;
For ( num = 1 ; num < = 10 ; num + + )
{
Printf ( “ % d \n ”, num ) ;
}
Getch ( ) ;
}
2. Write a program to find the sum of first n natural numbers where n is entered by user.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int n , i , sum = 0 ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, &n );
For ( i = 1 ; i < = n ; + + i )
{
sum = sum + i ;
}
Printf ( “ sum = % d \ n ” , sum ) ;
Getch ( ) ;
}
3. Write a program to generate multiplication table.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num , i ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, &num);
For ( i = 1 ; i < = 10 ; + + i )
{
Printf ( “ % d * % d = % d \n ”, num , i , num * i ) ;
}
Getch ( ) ;
}
4. Write a program to display factors of a number.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int n , i ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, &n ) ;
Printf ( “ factrors are \ n ” ) ;
For ( i = 1 ; i < = n ; + + i )
{
if ( n % i = = 0)
Printf ( “ % d ” , i ) ;
}
Getch ( ) ;
}
5. Write a program to find HCF of two numbers.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num1 , num2 , hcf , i ;
clrscr ( ) ;
Printf ( “ enter two numbers \ n ” ) ;
Scanf ( ” % d % d ”, & num1 , & num2 ) ;
Printf ( “ factrors are \ n ” ) ;
For ( i = 1 ; i < = num1 || i < = num2 ; + + i )
{
if ( num1 % i = = 0 && num2 % i = = 0 )
hcf = i ;
}
Printf ( “ hcf of two numbers is %d ” , hcf ) ;
Getch ( ) ;
}
6. Write a program to display ASCII values.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num ;
clrscr ( ) ;
For ( num = 0 ; num < = 255 ; num + + )
Printf ( “ ASCII value of character %c : % d \n ”, num , num ) ;
Getch ( ) ;
}
7. Write a programto display character from A to Z.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
char c ;
clrscr ( ) ;
For ( c = ' A ' ; c < = ' Z ' ; + +c )
{
Printf ( “ % c \ t ”, c ) ;
}
Getch ( ) ;
}
While loop
The while
loop checks whether the test expression is true or not. If it is true, codes inside the body of while loop is executed, That is, codes inside the braces { } are executed. Then again the test expression is checked whether test expression is true or not. This process continues until the test expression becomes false.
Example program
1. Write a C program to find the factorial of a number, where the number is entered by user.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num , factorial = 1 ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, & num ) ;
Printf ( “ factrors are \ n ” ) ;
while ( num > 0 ) /* while loop continues until test condition num > 0 is true*/
{
factorial = factorial * num ;
- - num ;
}
Printf ( “ factorial is %d ” , factorial ) ;
Getch ( ) ;
}
2. Write a program to add digits of given number.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num , sum = 0 , remainder ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, & num ) ;
while ( num ! = 0 )
{
remainder = num % 10 ;
sum = sum + remainder ;
num = num / 10 ;
}
Printf ( “ sum of digits of entered number is %d ” , sum ) ;
Getch ( ) ;
}
Explanation
For example if the input is 98, sum(variable) is 0 initially
98 % 10 = 8 ( % is modulus operator which gives us remainder when 98 is divided by 10 ).
sum = sum + remainder
so sum = 8 now.
98 / 10 = 9 because in c whenever we divide integer by another integer we get an integer.
9 % 10 = 9
sum = 8 ( previous value ) + 9
sum = 17
9 / 10 = 0.
So finally n = 0, loop ends we get the required sum.
3. Write a Program to Count Number of Digits of an Integer.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num , count = 0 ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, & num ) ;
while ( num ! = 0 )
{
num = num / 10 ;
+ + count ;
}
Printf ( “ number of digits of entered number is %d ” , count ) ;
Getch ( ) ;
}
4. Write a Program to Reverse a Number.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num , reverse = 0 , remainder ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, & num ) ;
while ( num ! = 0 )
{
remainder = num % 10 ;
reverse = reverse * 10 + remainder ;
num = num / 10 ;
}
Printf ( “ reversed number is %d ” , reverse ) ;
Getch ( ) ;
}
5. Write a program to print 1 to 10 numbers.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num = 1 ;
clrscr ( ) ;
while ( num < = 10 )
{
Printf ( “ % d \ n ” , num ) ;
+ + num ;
}
Getch ( ) ;
}
6. Write a program to Check Armstrong Number.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num , sum = 0 , remainder , temp ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, & num ) ;
temp = num ;
while ( temp ! = 0 )
{
remainder = temp % 10 ;
sum = sum + remainder * remainder * remainder ;
temp = temp / 10 ;
}
if ( num = = sum )
printf ( " armstrong number \ n " ) ;
else
printf ( " not an armstrong number\ n " ) ;
Getch ( ) ;
}
Definition according to c programming point of view:
Those numbers which sum of the cube of its digits is equal to that number are known as Armstrong numbers. For example 153 since 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153.
Armstrong numbers from 0 to 999 are 0 ,1, 153, 370, 371, 407 etc.
7. Write a program to Check Whether a Number is Palindrome or Not.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num , reverse = 0 , temp ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, & num ) ;
temp = num ;
while ( temp ! = 0 )
{
reverse = reverse * 10 ;
reverse = reverse + temp % 10 ;
temp = temp / 10 ;
}
if ( num = = reverse )
printf ( " palindrum number \ n " ) ;
else
printf ( " not a palindrum number\ n " ) ;
Getch ( ) ;
}
Palindrome number in c
A palindrome number is a number such that if we reverse it, it will not change. For example some palindrome numbers examples are 121, 212, 12321, -454. To check whether a number is palindrome or not first we reverse it and then compare the number obtained with the original, if both are same then number is palindrome otherwise not.
8. Write a program to count number of evens and odds in a given number.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num , remainder, even, odd ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, & num ) ;
even = 0 ;
odd = 0 ;
while ( num > 0 )
{
remainder = num % 10 ;
if ( remainder % 2 = = 0 )
even ++;
else
odd ++;
num = num / 10 ;
}
printf ( " total evens are : %d \ n " , even) ;
printf ( " total odds are : %d \ n ", odd ) ;
Getch ( ) ;
}
9. Write a Program to display Factors of a Number.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num , i = 1 ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, & num ) ;
printf ( " factors of given number are\ n " ) ;
while ( i < = num )
{
if ( num % i = = 0 ) ;
printf ( " % d \t" , i ) ;
i++;
}
Getch ( ) ;
}
10. Write a program to Calculate the Power of a Number.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int base , exponent ;
long long int value = 1 ;
clrscr ( ) ;
Printf ( “ enter base number and exponent \ n ” ) ;
Scanf ( ” % d % d ”, & base, & exponent ) ;
while ( exponent ! = 0 )
{
value = value * base ;
- - exp ;
}
printf ( " answer is : %d \ n " , value ) ;
Getch ( ) ;
}
11. Write a Program to Check Whether a Number is Prime or Not.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int i,n,count=0; ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, & n ) ;
for ( i = 1 ; i < = n ; i ++ )
{
if ( n % i = = 0 )
count = count + 1 ;
}
if ( count = = 2 )
printf ( " prime number \ n") ;
else
printf ( " not prime number \ n " ) ;
Getch ( ) ;
}
12. Write a Program to Find Sum of Natural Numbers.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num , count = 1 , sum = 0 ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, & num ) ;
while ( count < = num )
{
sum = sum + count ;
+ + count ;
}
Printf ( “ sum is %d ” , sum ) ;
Getch ( ) ;
}
13. Write a Program to Display Fibonacci Series.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int n,f 1, f 2, f 3 ;
clrscr ( ) ;
Printf ( “ enter number \ n ” ) ;
Scanf ( ” % d ”, & n ) ;
f 1 = 0 ;
f 2 = 1 ;
printf ( " % d \ t % d ", f 1 , f 2 ) ;
f 3 = f 1 + f 2 ;
while ( f 3 < = n )
{
printf ( " \ n %d ", f 3 ) ;
f 1 = f 2 ;
f 2 = f 3;
f 3 = f 1 + f 2;
}
Getch ( ) ;
}
Do While loop
The body of the loop will always be executed whether the condition is initially true or not. This is because do while loop is always executed once before the specified condition is tested.
Do...while loop is very similar to while loop. Only difference between these two loops is that, in while loops, test expression is checked at first but, in do...while loop code is executed at first then the condition is checked. So, the code are executed at least once in do...while loops.it tests the condition at the end of the loop body.
At first codes inside body of do is executed. Then, the test expression is checked. If it is true, codes inside body of do are executed again and the process continues until test expression becomes false (zero).
Notice: there is semicolon in the end of while ( ) ; in do...while loop.
1. Write a C program to find the sum of 10 natural numbers.
/******************************************************************************/
//*****************************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
Int num, sum;
clrscr ();
Sum=0;
num=1;
Do
{
sum= sum + num;
num++;
}
While ( num < = 10 ) ;
Printf ( “ sum of %d natural numbers is %d \ n ” , num, sum ) ;
Getch ( ) ;
}
2. Write a C program to print taruni computers ten times.
/******************************************************************************/
//*****************************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
Int i = 10 ;
clrscr ( ) ;
Do
{
Printf ( “ taruni computers \ n ” ) ;
i = i - 1 ;
}
While ( i > 0 ) ;
Getch ( ) ;
}
3. Write a C program to print 10 to 20 numbers .
/******************************************************************************/
//*****************************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
Int num = 10 ;
clrscr ( ) ;
Do
{
Printf ( “ num is %d\ n ” , num) ;
num = num + 1 ;
}
While ( num < 20 ) ;
Getch ( ) ;
}
4. Write a C program to add all the numbers entered by a user until user enters zero .
/******************************************************************************/
//*****************************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
Int num , sum ;
clrscr ( ) ;
Sum = 0 ;
Do
{
Printf ( “ enter a number \ n ” ) ;
Scanf ( “ %d ” , & num ) ;
sum= sum + num;
}
While ( num ! = 0 ) ;
Printf ( “ sum is %d ” , sum ) ;
Getch ( ) ;
}
5. Write a C program to print first ten multiple of 5 .
/******************************************************************************/
//*****************************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
Int num, i ;
clrscr ( ) ;
i = 1 ;
num = 5;
Do
{
Printf ( “ %d \ t ” , num * i ) ;
i + + ;
}
While ( i < = 10 ) ;
Getch ( ) ;
}