C Programming - Loop(Iteration)

C Programming - Loop(Iteration)


Looping:
The process of executing the same statement repeatedly until a condition is satisfied is called looping. Every loop has three fundamental components: initialization, condition and counter. An initialization statement defines the starting point of the loop. The condition defines the stopping point of the loop and finally the counter counts the number of iterations. The increment and decrement operations are used as counter.
If the task or set of instructions required to be executed “n” number of times, we can use loop statements.
In C- language we have 3 types of looping structures.
1.     For loop
2.     While loop
3.     Do-while loop

1. For Loop
The for loop is applied in the situation when you exactly know how many times you want to execute the statements. It is entry-controlled loop.
Syntax of for loop:
for (initialization; condition; increment/decrement)
{
body of loop
}

                




Example of FOR Loop:
// Program to display “I am a student of class 12.” 10 times using for loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for (i=1;i<=10;i++)
{
printf ("I am a student of class 12.\n");
}
getch();
}

2. While Loop
 The while loop executes a statement or a black of statements as long as a condition evaluates to true. The while loop is mainly used in situations where you don’t know in advance how many times the loop will be executed. The loop terminates when the condition evaluates to false.
Syntax of while loop
Initialization;
while (condition)
{
Statements
……………………….
Increment/decrement
}
The while loop must initialize variable before the loop begin. The condition consists inside while parenthesis. The increment/decrement performed inside the body of loop i.e., within braces.

Example  of While -Loop:
//program to display " I am a student of class 12" 10 times using while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=10)
    {
          printf("I am a student of class 12.\n");
          i++;
    }
getch();
}

3. do-while loop
The working of do-while loop is identical to that the while loop, except that in the do-while loop, a condition is checked at the end of the loops after each iteration. It means that the do-while loop definitely executes at least once even if the condition is false. Unlike the while loop, a semicolon has to be placed after while expression in the do-while loop; otherwise, the loop will not execute.
Syntax of do-while loop:
initialization;
do
{
Statements;
Increment/decrement;
} while condition ( );



# Differences between while and do-while loop:
While loop
do-while loop
1. Condition is evaluated before the statement is executed.
1. Condition is evaluated after the statement is executed.
2. It is an entry control loop.
2. It is an exit control loop.
3. It has a keyword While.
3. It has two keyword do and while.
4. Semicolon is not placed after the while condition.
4. Semicolon is placed after the while condition.
5. It does not execute a statement when the condition is false.
5. It executes statement once even the condition is false.
6. Example:
//program to display " I am a student of class 12" 10 times using while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=10)
    {
printf("I am a student of class 12.");
          i++;
    }
getch();
}
6. Example:
//program to display " I am a student of class 12" 10 times using do-while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
     do
    {
printf("I am a student of class 12.");
          i++;
    }while(i<=10);
getch ();
}



Infinite loop
The loops that do not end are called infinite loop. Generally, infinite loops are used in server code where the servers have to run without any interruption.
//program to show infinite  loop
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
     for(i=1;i>=0;i++)
    {
          printf("%d",i);

    }
getch();
}

Jumping Statement:
Jumping statements are particularly used to jump execution of program statements from one place to another place inside a program. These statements may executes same program statement repeatedly or skip some program statements. Following are the jumping statements used in C.
·        break
·        continue
·        goto

Break and continue statement:

1. Break statement:
The break statement is used to exit from a while, for, do-while or switch structures. It can only be used inside the body of a for, while, do-while, or switch statement.
2. Continue Statement:
 The continuous causes the loop to be conditioned with the next iteration after skipping any statements in between. The continue statements tells the compiler “skip the following statements and continue with the next iteration”.



# Differentiate between Break and Continue Statement

Break Statement
Continue Statement
1. The break statement is used to terminate the control from the switch-case and loop structure.
1. The continue statement is used to by-pass the execution of the further statements.
2. When break statement encountered the entire loop or switch statement is terminated.
2. When continue statement is encountered the entire loop is not terminated; only that particular iteration is skipped.
3. It uses a keyword break.
3. It uses a keyword continue.
4. It can be used in both loop and switch case.
4. It can be only used in loop structure.
5. Example:
#include<stdio.h>
#include<conio.h>

void main( )
{
int i;
for (i=1;i<=10;i++)
{
if(i= =2)
break;
printf("%d",i);
}
getch ( );
}
Output: 1
5. example:
#include<stdio.h>
#include<conio.h>
void main( )
{
int i;
for (i=1;i<=10;i++)
{if(i= =2)
continue;
printf("%d\t",i);
}
getch( );
}

Output:
1 3 4 5 6 7 8 9 10


Homework Questions
C-1 (Introduction of C)
1.     Define variable and write the rules for variable naming in C programming.
2.     Define simple and compound statements. Describe logical operator with examples.
3.     Define data type. Explain different types of data types used in C programming with examples.
4.     What is an operator? Explain different types of operators used in C programming with examples.
5.     Describe switch case statement with example.
6.     What is variable? Write its types. Define the terms identifier and keywords with example.
7.     Describe “sequence”, “selection” and “Loop” with flowchart and examples.
8.     Differentiate between while and do-while loop with examples.
9.     What is looping? Describe “For”, “while”, “do-while” loops with flowchart and appropriate examples.
Differentiate between break and continue statements with examples.



Fundamentals Program


1. Write a program to find out whether the input number is even or odd?
#include<stdio.h> Output:
#include<conio.h> Enter a number=4
void main() 4 is even number.
{
clrscr ();
int n;
printf(" Enter a number= ");
scanf(" %d ", &n);
if(n%2==0)
printf(" %d is even number. " , n);
else
printf(" %d is odd number. " , n);
getch();
}
2. Write a program to find out whether the input number is positive or
negative?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
printf(" Enter a number= ");
scanf(" %d " , &n);
if(n>0)
printf(" %d is positive number. " , n);
else
printf(" %d is negative number. " , n);
getch();
}
3. Write a program to find the largest number among two numbers?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
printf("Enter any two number=");
scanf("%d%d",&a , &b);
if(a>b)
printf(" %d is greater number. " , a);
else
printf(" %d is greater number. " , b);
getch();
}
4. Write a program to find the smallest number among two numbers?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
printf("Enter any two number=");
scanf("%d%d",&a , &b);
if(a<b)
printf(" %d is smaller number. " , a);
else
printf(" %d is smaller number. " , b);
getch();
}

5. Write a program to find the largest number among three different
numbers?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter any three number=");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
printf("%d is largest number.",a);
else if(b>a&&b>c)
printf("%d is largest numebr.",b);
else
printf("%d is largest number.",c);
getch();
}
6. Write a program to find the greatest number among four different
numbers?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
printf("Enter any four number=");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>b&&a>c&&a>d)
printf("%d is greatst number.",a);
else if(b>a&&b>c&&b>d)
printf("%d is greatest numebr.",b);
else if(c>a&&c>b&&c>d)
printf("%d is greatest number.",c);
else
printf("%d is greatest number.",d);
getch();
}
7. Write a program to check whether the number entered by user is
divisible by 5 but not by 11?
#include<stdio.h>
#include<conio.h>
void main( )
{
int n;
printf("Enter a number =");
scanf("%d",&n);
if(n%5==0&&n%11!=0)
printf(" %d is divisible by 5 but not by 11.",n);
else
printf(" %d is not divisible by 5 but not by 11.",n);
getch( );
}
8. Write a program to accept SP and CP of an item and find whether there is
profit or loss?
#include<stdio.h>
#include<conio.h>
void main( )
{
int sp,cp,p,l;
printf("Enter the sp and cp=");
scanf("%d%d",&sp,&cp);
if(sp>cp)
{
p=sp-cp;
printf("Profit=%d" , p);
}
else
{
l=cp-sp;
printf("Loss=%d " ,l);
}
getch( );
}

9. Write a program to calculate and display the multiplication table of “n”?
#include<stdio.h>
#include<conio.h>
main( )
{
int n,i,ans;
printf("Enter any number=");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
ans=n*i;
printf(" %d * %d = %d \n " , n, i, ans);
}
getch( );
}

10. Write a program to find the factorial of a given number?
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,f=1;
printf("Enter a number=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf(" Factorial of %d =%d " , n , f);
getch( );
}

11. Write a program to display the series upto 10th terms?

1 5 9 13 .............
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr();
int a=1,i;
for(i=1;i<=10;i++)
{
printf("%d\t" , a);
a=a+4;
}
getch( );
}

12. Write a program to display first 10 even numbers?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,a=2;
for(i=1;i<=10;i++)
{
printf(" %d \n " , a );
a=a+2;
}
getch();
}

13. Write a program to display the sum of “n” terms of even numbers?
#include<stdio.h>
#include<conio.h>
void main( )
{
int n , i , sum = 0;
printf("Enter a number=");
scanf("%d",&n);
for(i=1 ; i<=n ; i++)
{
if( i%2 = = 0)
sum=sum+i;
}
printf("Sum of even numbers up to %d = %d", n,sum);
getch( );
}
14. Write a program to enter any number “n” and find the sum of the
numbers upto “n” using loop?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,sum=0;
printf("Enter a number=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("Sum of numbers upto %d =%d " , n , sum);
getch();
}

15. Write a program to check whether the input number is prime or not?
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr();
int i, n;
printf("Enter a number = ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if( n % i = = 0)
{

printf(" %d is not prime ", n);
break;
}
}
if( i = = n)
printf(" %d is prime" , n);
getch( );
}
16. Write a program to display all prime numbers from 1 to 100?
#include<stdio.h>
#include<conio.h>
void main ( )
{
int n,i;
for(i=1;i<=100;i++)
{
for(n=2;n<i;n++)
{
if(i%n==0)
break;
}
if(i==n)
printf("%d\t",n);
}
getch();
}
17. Write a program to display Fibonacci series i.e. 0 1 1 2 3 5 ... upto 10th
terms.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a=0 ,b=1 , c, i;
printf("%d \t %d \t", a , b);
for(i=1; i<=10; i++)
{
c=a+b;
a=b;
b=c;
printf(" %d \t " , c);
}
getch();
}
18. Write a program to display 1 for Sunday, 2 for Monday and so on?

[switch case example]

#include<stdio.h>
#include<conio.h>
void main ( )
{
int choice;
printf("Enter the number of the day=");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Wrong Choice");
}
getch( );
}
19. Write a program which finds the sum, difference and product of two
numbers using switch case statement?
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr();
int a,b,ch,s,d,p;
printf(" 1. Sum \n");
printf("2. Difference \n");
printf("3. Product\n ");
printf(" Enter two numbers=");
scanf("%d%d",&a,&b);
printf("Enter your choice = ");
scanf("%d",&ch);
switch(ch)
{
case 1:
s= a+b;
printf("Sum of two numbers=%d",s);
break;
case 2:
d=a-b;
printf(" Difference of two numbers=%d",d);
break;
case 3:
p=a*b;
printf(" Product of two numbers=%d",p);
break;
default:
printf("Wrong Choice");
}
getch( );
}

20.Write a program to reverse a number?
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr();
int n,r,num=0;
printf(" Enter any number=");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
num=num*10+r;
n=n/10;
}
printf("Reverse order=%d",num);
getch( );
}
21. Write a program to display:
1
12
123
1234
12345

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch( );
}
22. WAP to display: 1
22
333
4444
55555 ?

#include<stdio.h>
#include<conio.h>
void main ( )
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i); }
printf("\n");
}
getch( );
}
23. WAP to display:

*
**
***
****
***** ?

#include<stdio.h>
#include<conio.h>
void main() {
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++) {
printf("*"); }
printf("
\n");
}
getch( ); }

24.
WAP to display 4321
321
21
1 ?

#include<stdio.h>
#include<conio.h>
void main ( ) {
int i,j;
for(i=4;i>0;i--)
{
for(j=i;j>0;j--)
{
printf("%d",j); }
printf("
\n");
printf(" "); }
getch( ); }
25. WAP to display 12345
1234
123
12
1 ?

#include<stdio.h>
#include<conio.h>
main() {
int i,j;
for(i=5;i>=1;i--
)

{
for(j=1;j<=i;j++) {
printf("%d",j); }
printf("
\n");
}
getch(); }
26. WAP to display 55555
4444
333
22
1 ?

#include<stdio.h>
#include<conio.h>
main( ) {
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch( );
}
27. WAP to display:
*****
****
***
**
* ?

#include<stdio.h>
#include<conio.h>
main( )
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch( );
}
28. Write a program to check whether the entered number is Armstrong or not [An
Armstrong number is that whose sum of cube of digits is equal to number itself.
Example:-371=33
+73
+13
][1,153,370,371,407]

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,sum,j;
printf("Enter a number=");
scanf("%d",&n);
sum=0;
i=n;
while(i!=0)
{
j=i%10;
sum=sum+j*j*j;
i/=10;
}
if(n==sum)
printf("%d is Armstrong",n);
else
printf("%d is not Armstrong",n);
getch();
}
29. Write a program to to enter a string and check whether the entered string is palindrome
or not? [A string is said to be palindrome if it remains same if we reverse it. Eg:-
ADA,LIRIL,MALAYALAM,MADAM]
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100],temp[100];
printf("Enter a string=");
scanf("%s",str);
strcpy(temp,str);
strrev(temp);
if(strcmp(temp,str)==0)
printf("%s is palindrome",str);
else
printf("%s is not palindrome",str);
getch();
}
30. Write a program to make a report card of exam?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char name[30];
int cl,eng,nep,acc,eco,cmp,total;
float per;
printf("\nEnter the name of a student=");
scanf("%s",name);
printf("\n Enter the class=");
scanf("%d",&cl);
printf("\n Enter the marks in English, Nepali,Account,Economics, Computer=");
scanf("%d%d%d%d%d",&eng,&nep,&acc,&eco,&cmp);
total=eng+nep+acc+eco+cmp;
per=total/5;
printf("NAME=%s\n" , name);
printf("CLASS=%d\n" , cl);
printf("TOTAL=%d\n" , total);
printf("Percentage=%0.2f\n" , per);
{
if(eng>=35&&nep>=35&&acc>=35&&eco>=35&&cmp>=35)
printf("Result=Passed\n");
else
printf("Result=Failed\n");
if(per>=75)
printf("Division=Distinction");
else if(per>=60&&per<75)
printf("Division=First");
else if(per>=45&&per<60)
printf("Division=Second");
else if(per>=35&&per<45)
printf("Division=Third");
else
printf("Division=Fail");
}
getch();
}