INDEX
- Introduction to Loops in C:
- C Programming: Structure of for loop
- C Program: Number series: 1,2,3,...,10
- C Program: Display "C Loops" 10 times
- C Program: Display "C Loops" 10 times with serial No.
- C Program: First 10 multiples of 3
- C Program: Factorial of n
- C Program: Fibonacci Series (i.e. 1,1,2,3,5,8,13,21,....)
- C Program: Factors of n
- C Program: Perfect number
- C Program: HCF and GCD
- C Program: Prime Number
- C Program: Sum of digits of an integer
- C Program: Reverse number
- C Program: Numeric Pallindrome
- C Program: Armstrong Number
- C Program: Duck Number
- C Program: to display
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5 - C Program: to display
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 - C Program: to display
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5 - C Program: to display
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1 - C Program: to display
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5 - C Program: to display
1
22
333
4444
55555 - C Program: to display
55555
4444
333
22
1 - C Program: to display
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15 - C Program: to display
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1 - C Program: to display
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5 - C Program: to display
1
22
333
4444
55555
4444
333
22
1 - C Program: to display
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4 - C Program: to display
1 $ $ $ $
1 2 $ $ $
1 2 3 $ $
1 2 3 4 $
1 2 3 4 5
1 2 3 4 $
1 2 3 $ $
1 2 $ $ $
1 $ $ $ $ - C Program: to display
1 2 3 4 5
2 3 4 5 $
3 4 5 $ $
4 5 $ $ $
5 $ $ $ $
4 5 $ $ $
3 4 5 $ $
2 3 4 5 $
1 2 3 4 5 - C Program: to display
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1 - C Program: to display
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1 - More patterns... (Sign in required...)
Introduction to Loops in C:
Loops are the control-statements, which are used to re-iterate (or repeat) a set of instructions (or
even a single instruction), as long as a specific criteria is met.
The moment the criteria fails, the looping structure ends, and the program-control moves to the first
statement following the loop-struture / loop-block.
There are four(4) kinds of loop control statements in C language.
They are:
- if-goto statement pair
- for loop
- while loop
- do-while loop
The above four loops can be grouped into two kinds of looping-structures. They are;
- Entry control loops
- Exit control loops
Entry control loops check the loop condition at the begining of each iteration. for loop and
while loop falls in this category.
Exit control loops check the loop condition at the end of each iteration. if-goto pair
loop and do-while loop falls in this category.
Note: The use of goto statement carries additional responsibility for the programmer, so as
to ensure that goto is used wisely. Failing which there is likely hood that the
program may get entangled. Thus use of goto is broadly discouraged among programmers. The for loop
for(statement1;statement2;statement3)
{
//the statements that are to be re-iterated
...........
...........
...........
}
The while loop
while(condition)
{
//the statements that are to be re-iterated
...........
...........
...........
}
/////////////////////////////Examples//////////////////////
/*C Program to display 1 to 10
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
short int x;
for(x=1;x<=10;x++)
{
printf("\n%d",x);
}
return 0;
}
/*C Program to display the phrase 'C loops' 10 times
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
short int x;
for(x=1;x<=10;x++)
{
printf("\nC loops");
}
return 0;
}
/*C Program to display the phrase 'C loops' 10 times with serial number
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
short int x;
for(x=1;x<=10;x++)
{
printf("\n%d. C loops",x);
}
return 0;
}
/*C Program to display the first 10 multiples of 3
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
short int x;
printf("\nThe first 10 multiples of 3 are:");
for(x=1;x<=10;x++)
{
printf("\n%d",(x*3));
}
return 0;
}
/*C Program to display factorial of n (i.e. n!)
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
int n,i,fact;
printf("\nEnter any positive integer (n): ");
scanf("%d",&n);
fact=1;
for(i=1;i<=n;i++)
fact*=i;
printf("\nn!=%d",fact);
return 0;
}
/*C Program to display fibonacii series 0,1,1,2,3,5,8,....
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
int i,j,k,ul;
printf("\nEnter the upper-limit of fibonacii series: ");
scanf("%d",&ul);
i=0,j=1,k=0;
for(k=0;k<=ul;k=i+j)
{
if(k!=0)
printf(", ");
printf("%d",k);
i=j;
j=k;
}
return 0;
}
/*C Program to accept a number and display its factors
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
int n,i;
printf("\nEnter any Integer (n): ");
scanf("%d",&n);
printf("The Factors of %d are: ",n);
for(i=1;i<=n;i++)
{
if(n%i==0)
printf("\n%d",i);
}
return 0;
}
/*C Program to accept a number and display whether its a PERFECT-NUMBER or NOT
(definition): if (the sum of factors of the number) = 2 x Number, then the number is a perfect number.
(example): 6, Sum of Factors of 6 = 1+2+3+6 =12 = 2x6, hence 6 is a perfect number.
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
int n,i,sum;
printf("\nEnter any Integer (n): ");
scanf("%d",&n);
//Finding factors of n, and adding them
sum=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
sum+=i;
}
if(sum==2*n)
printf("\n%d is a PERFECT-NUMBER.",n);
else
printf("\n%d is a NOT Perfect-Number.",n);
return 0;
}
/*C Program to accept 2 integers from keyboard and display their h.c.f. or g.c.d.
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
int n1,n2,x,y;
printf("\nEnter integer1 (n1) : ");
scanf("%d",&n1);
printf("\nEnter integer2 (n2) : ");
scanf("%d",&n2);
x=n1,y=n2;
//when n1>n2 we interchange the values of x and y to make x smaller and y larger
if(n1>n2)
x=n2,y=n1;
for(;y%x!=0;x=y%x);//finding hcf(x,y)
printf("\nHCF(%d,%d) = %d", n1,n2,x);
return 0;
}
/*C Program to accept a number and display whether its prime or not
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
int n,FLAG=1,i;
printf("\nEnter any integer (n): ");
scanf("%d",&n);
if(n==1)
FLAG=0;
for(i=2;i<n/2;i++)
{
if(n%i==0)
{
FLAG=0;
break;
}
}
if(FLAG)
printf("\n%d is Prime.",n);
else
printf("\n%d is NOT Prime.",n);
return 0;
}
/*C Program to accept a number and display the sum of its digits.
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
int n,d,sum,n2;
printf("\nEnter any integer (n): ");
scanf("%d",&n);
sum=0;
for(n2=n;n2>0;n2/=10)
{
d=n2%10;
sum+=d;
}
printf("\nSum of digits of %d = %d",n,sum);
return 0;
}
/*C Program to accept a number and display the reverse number.
Example: (i/p)243
(o/p)Reverse Number is 342
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
int n,d,rev,n2;
printf("\nEnter any integer (n): )";
scanf("%d",&n);
rev=0;
for(n2=n;n2>0;n2/=10)
{
d=n2%10;
rev=rev*10+d;
}
printf("\nReverse number of %d = %d",n,rev);
return 0;
}
/*C Program to accept a number and display whether its a Numeric-Pallindrome or Not
(definition): if NUMBER = ITS REVERSE NUMBER, then the NUMBER is a Numeric-Pallindrome.
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
int n,d,rev,n2;
printf("\nEnter any integer (n): ");
scanf("%d",&n);
rev=0;
for(n2=n;n2>0;n2/=10)
{
d=n2%10;
rev=rev*10+d;
}
if(rev==n)
printf("\n%d is a Numeric-Pallindrome.",n);
else
printf("\n%d is NOT a Numeric-Pallindrome.",n);
return 0;
}
/*C Program to accept a number and display whether its an Armstrong number or Not
(definition): if NUMBER = SUM of CUBE of ITS DIGITS, then the number is an ARMSTRONG Number
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
#include<math.h>
int main()
{
int n,d,sum,n2;
printf("\nEnter any integer (n): ");
scanf("%d",&n);
sum=0;
for(n2=n;n2>0;n2/=10)
{
d=n2%10;
sum+=pow(d,3);
}
if(sum==n)
printf("\n%d is an ARMSTRONG-NUMBER.",n);
else
printf("\n%d is a NOT an Armstrong-Number",n);
return 0;
}
/*C Program to accept an integer and display whether its a Duck-Number or not
(definition): if the NUMBER contains atleast one 0(Zero) as a digit, then it is called a duck number.
(example): 10,20,301,520,1200,1701,...
Author: Pradeep Das Gupta
Compiler: GNU gcc compiler.*/
#include<stdio.h>
int main()
{
int d,n,n2,count;
printf("\nEnter any integer (n): ");
scanf("%d",&n);
count=0;
for(n2=n;n2>0;n2/=10)
{
d=n2%10;
if(d==0)
count++;
}
if(count>0)
printf("\n%d is a DUCK-NUMBER.",n);
else
printf("\n%d is a NOT an Duck-Number",n);
return 0;
}