For iterative control structure:
Any iterative control structure has four parts in common
that is initialization, condition, increment/decrement and the body.
The "for" loop is an integrated iterative control
structure where initialization, condition and increment/decrement statements
are placed with in the conditional statement by separating with two semicolons
(;).
The body of loop is placed under the conditional statement.
The body of loop executes repeatedly until condition is
false.
According to the syntax
Initialization section executes at the beginning of loop
execution.
Condition, body and incr/decr sections executes repeatedly
until the condition is false.
Initialization section will not execute again in the life of
loop.
Specification1:
In case
of while i=1; while(i<=n) { printf("%5d",i); i++; }
Print the natural numbers from 1 to n
Program:
#include<stdio.h>
void main()
{
int n,i;
printf("Enter the limit:");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%5d",i);
}
Output:
Enter the limit: 5
1 2 3 4 5
Specification2:
Print the natural numbers from n to 1
Program:
In case
of while i=n; while(i>=1) { printf("%5d",i); i--; }
#include<stdio.h>
void main()
{
int n,i;
printf("Enter the limit:");
scanf("%d",&n);
for(i=n;i>=1;i--)
printf("%5d",i);
}
Output:
Enter the limit: 5
5 4 3 2 1
Specification3:
Print the natural numbers with in the range.
Program:
In case
of while i=ll; while(i<=ul) { printf("%5d",i); i++; }
#include<stdio.h>
void main()
{
int i,ll,ul;
printf("Enter the lower limit:");
scanf("%d",&ll);
printf("Enter the upper limit:");
scanf("%d",&ul);
for(i=ll;i<=ul;i++)
printf("%5d",i);
}
Execution:
Enter the lower limit: 5
Enter the upper limit: 10
5 6 7 8 9 10
Specification4:
Print the even numbers from 1 to n
Program:
#include<stdio.h>
void main()
{
int n,i;
printf("Enter the limit:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
printf("%5d",i);
}
}
Execution:
Enter the limit: 10
2 4 6 8 10
Specification5:
Print the sum of natural numbers from 1 to n
Program:
#include<stdio.h>
void main()
{
int n,i,sum;
printf("Enter the limit:");
scanf("%d",&n);
for(sum=0,i=1;i<=n;i++)
sum=sum+i;
printf("Sum of natural numbers %d",sum);
}
Execution:
Enter the limit: 5
Sum of natural numbers 15
Specification6:
Accept any number and print the its factorial
Program:
#include<stdio.h>
void main()
{
long int fact;
int n,i;
printf("Enter any number:");
scanf("%d",&n);
for(fat=1,i=1;i<=n;i++)
fact=fact*i;
printf("Factorial of the number %ld",fact);
}
Execution:
Enter any number: 5
Factorial of the number 120
Specification7:
Print the sum of even, odd numbers from 1 to n
Program:
#include<stdio.h>
void main()
{
int i,n,sum,osub;
printf("Enter the limit:");
scanf("%d",&n);
for(esum=0,osum=0,i=1;i<=n;i++)
{
if(i%2==0)
esum=esum+i;
else
osum=osum+i;
}
printf("Sum of even numbers %d",esum);
printf("\nSum of odd numbers %d",osum);
}
Execution:
Enter the limit: 10
Sum of even numbers 30
Sum of odd numbers 25
Specification8:
Accept any integer and print its factors.
#include<stdio.h>
void main()
{
int n,i;
printf("Enter any integer:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
printf("%5d",i);
}
}
Execution:
Enter any integer: 10
1 2 5 10
Specification9:
Accept any integer and print the sum of its factors.
Program:
#include<stdio.h>
void main()
{
int n,i,sum;
printf("Enter any integer:");
scanf("%d",&n);
for(sum=0,i=1;i<=n;i++)
{
if(n%i==0)
sum=sum+i;
}
printf("Sum of all the factors %d",sum);
}
Execution:
Enter any integer: 12
Sum of all the factors 28
Specification10:
Accept any integer and print how many factors does the given
number has.
Program:
#include<stdio.h>
void main()
{
int n,i,count;
printf("Enter any integer:");
scanf("%d",&n);
for(count=0,i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
printf("The number has %d number of factors",count);
}
Execution:
Enter any integer: 10
The number has 4 number of factors.
Specification11:
Accept any integer and print whether the number is a prime
number or not.
Logic: if any number has only two factors then the number is
called a prime number.
Program:
#include<stdio.h>
void main()
{
int n,i,count;
printf("Enter any integer:");
scanf("%d",&n);
for(count=0,i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
printf("Prime
number");
else
printf("Not a
prime number");
}
Execution1:
Enter any number: 8
Not a prime number
Execution2:
Enter any number: 7
Prime number
Specification12:
Accept any integer and print whether the number is a perfect
number or not.
Logic: if the sum of all the factors other than the same
number is equal to the given number then the number is called a perfect number.
Program:
#include<stdio.h>
void main()
{
int n,sum,i;
printf("Enter any integer:");
scanf("%d",&n);
for(sum=0,i=1;i<n;i++)
/* up to the last before */
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
printf("Perfect
number");
else
printf("Not a
perfect number");
}
Execution1:
Enter any integer: 24
Perfect number
Execution2:
Enter any integer: 25
Not a perfect number.
Specification13:
Accept any number and print the sum of all digits.
In case
of while: sum=0; while(n!=0) { sum=sum+n%10; n=n/10; }
Program:
#include<stdio.h>
void main()
{
int n,sum;
printf("Enter any number:");
scanf("%d",&n);
for(sum=0;n!=0;n=n/10)
sum=sum+n%10;
printf("Sum of all digits %d",sum);
}
Execution:
Enter any number: 456
Sum of all digits 15
Specification14:
Accept any number and print it reverse.
In case
of while: rev=0; while(n!=0) { rev=rev*10+n%10; n=n/10; }
Program:
#include<stdio.h>
void main()
{
int n,rev;
printf("Enter any number:");
scanf("%d",&n);
for(rev=0;n!=0;n=n/10)
rev=rev*10+n%10;
printf("The reverse number %d",rev);
}
Execution:
Enter any number: 7658
The reverse number 9567
Specification15:
Accept any number and print whether the number is a
palindrome number or not.
Program:
#include<stdio.h>
void main()
{
int n,rev,temp;
printf("Enter any number:");
scanf("%d",&n);
/* storing the accepted number into
"temp"
because "n" would be zero */
temp=n;
for(rev=0;n!=0;n=n/10)
rev=rev*10+n%10;
if(temp==rev)
printf("Palindrome
number");
else
printf("Not a
palindrome number");
}
Execution:
Enter any number: 3445
Not a palindrome number
Execution:
Enter any number: 3443
Palindrome number
Specification16:
Accept any number and print whether the number is an
Armstrong number or not.
Logic: If the sum of cubes of individual digits is equals to
the given number then the number is called Armstrong
Example: 153, 370, 371, 407
Program:
#include<stdio.h>
void main()
{
int n,sum,temp;
printf("Enter any number:");
scanf("%d",&n);
temp=n;
for(sum=0;n!=0;n=n/10)
sum=sum+(n%10)*(n%10)*(n%10);
if(sum==temp)
printf("Armstrong
Number");
else
printf("Not
an Armstrong number");
}
Execution:
Enter any number: 157
Not an Armstrong number
Execution:
Enter any number: 153
Armstrong number
Specification17:
Accept any integer and print the minimum digit.
Logic:
We will take the last digit as the minimum digit.
We will extract individual digits from the given number
using n!=0, n=n/10 and compare with the minimum digit.
If the new digit is less than the minimum, then it will go
into minimum otherwise, we will try the next digit.
At the end of iteration we will get the minimum digit into
the minimum.
min=n%10;
while(n!=0)
{
if(n%10<min)
min=n%10;
n=n/10;
}
Program:
#include<stdio.h>
void main()
{
int n,min;
printf("Enter any integer:");
scanf("%d",&n);
for(min=n%10;n!=0;n=n/10)
{
if(n%10<min)
min=n%10;
n=n/10;
}
printf("The minimum digit is %d",min);
}
Execution1:
Enter any integer:5234
The minimum digit is 2
Specification18:
Accept any integer and print the maximum digit.
Logic:
We will take the last digit as the maximum digit.
We will extract individual digits from the given number using
n!=0, n=n/10 and compare with the maximum digit.
If the new digit is greater than the maximum, then it will
go into maximum otherwise, we will try the next digit.
At the end of iteration we will get the maximum digit into
the maximum.
max=n%10;
while(n!=0)
{
if(n%10>max)
max=n%10;
n=n/10;
}
Program:
#include<stdio.h>
void main()
{
int n,max;
printf("Enter any integer:");
scanf("%d",&n);
for(max=n%10;n!=0;n=n/10)
{
if(n%10>max)
max=n%10;
}
printf("The maximum digit is %d",max);
}
Execution1:
Enter any integer: 5853
The maximum digit is 8
Execution2:
Enter any integer: 16342
The maximum digit is 6
Specification20:
Accept any integer and print the minimum and maximum digits
of the number.
Logic:
min=max=n%10;
while(n!=0)
{
if(n%10<min)
min=n%10;
if(n%10>max)
max=n%10;
}
Program:
#include<stdio.h>
void main()
{
int n,max;
printf("Enter any number:");
scanf("%d",&n);
for(min=max=n%10;n!=0;n=n/10)
{
if(n%10<min)
min=n%10;
if(n%10>max)
max=n%10;
}
printf("The minimum digit %d",min);
printf("\nThe maximum digit %d",max);
}
Execution2:
Enter any integer: 16342
The minimum digit 1
The maximum digit is 6
Speification21:
Print the Fibonacci series up to "n" number of terms.
Logic:
Fibonacci series starts from 0 1
Sum of last two terms will be the next term.
Program:
#include<stdio.h>
void main()
{
int n,i,a,b,c;
printf("How many terms?");
scanf("%d",&n);
/* i and n are used to run the loop for n time */
/* a, b and c are
used to generate the series for n terms */
for(a=0,b=1,c=0,i=1;i<=n;i++)
{
printf("%5d",c);
a=b;
b=c;
c=a+b;
}
}
Execution:
How many terms? 7
0 1 1 2 3 5 8 13
Specification22:
Accept the table number and print the table up to 20 terms.
Logic:
Enter the
table number: 7 n i n*i ----------------------- 7 x
1 =
7 7 x
2 = 14 7 x
3 = 21 7 x
4 = 28 7 x
5 = 35 7 x
6 = 42 ----------------- ----------------- 7 x
20 = 140
i=1;
while(i<=20)
{
printf("\n%dx%d=%d\n",n,i,n*i);
i++;
}
Program:
#include<stdio.h>
void main()
{
int n,i;
printf("Enter the table number:");
scanf("%d",&n);
for(i=1;i<=20;i++)
printf("%d X
%d = %d \n",n,i,n*i);
}
Execution:
Enter the table number: 5
5 X 1
= 5
5 X 2
= 10
5 X 3
= 15
.
.
5 X 20
=100
Specification:
Accept the base, exponent and print its result.
Logic:
If we want find 4 power 6 then we need to multiply 4 for 6
times.
Here we multiply base for exponent number of times to
another variable and it will be the result.
Program:
#include<stdio.h>
void main()
{
int
i,base,exp,res;
printf("Enter
the base:");
scanf("%d",&base);
printf("Enter
the exponent:");
scanf("%d",&exp);
for(res=1,i=1;i<=exp;i++)
res=res*base;
printf("
Result %d",res);
}
Execution:
Enter the base: 3
Enter the exponent: 5
Result: 243
pow():
It is the function defined with in the header file math.h.
It accepts both base, exponent and gives result.
Example:
#include<stdio.h>
#include<math.h>
void main()
{
int base,exp,res;
printf("Enter
the base:");
scanf("%d",&base);
printf("Enter the exponent:");
scanf("%d",&exp);
res=pow(base,exp);
printf("Result
%d",res);
}
Execution:
Enter the base: 3
Enter the exponent: 5
Result: 243
Specification23:
Accept any binary number and print its equal decimal number.
Logic:
We use the logic n!=0, n=n/10, n%10 to extract individual
bits from a binary number.
We use the logic i=0, i++ to calculate 2i
Note:
Here we use pow(b,e) function to calculate "b" to
the power of "e"
math.h need to be included to import pow() function.
i=1;
sum=0;
while(n!=0)
{
sum=sum+pow(n%10,i);
n=n/10;
i++;
}
Program:
#include<stdio.h>
#include<math.h>
void main()
{
int i,sum;
long int n;
printf("Enter any binary number:");
scanf("%ld",&n);
for(i=0,sum=0;n!=0;n=n/10,i++)
sum=sum+pow(n%10,i);
printf("Equal decimal number %d",sum);
}
Execution:
Enter any binary number: 1011011
Equal decimal number 91
Specification24:
Accept any decimal number and print its equal binary number.
Logic:
sum=0;
i=0;
while(n!=0)
{
sum=sum+(n%2)*pow(10,i);
n=n/2;
i++;
}
Program:
#include<stdio.h>
#include<math.h>
void main()
{
int n,i;
long int sum;
printf("Enter any integer:");
scanf("%d",&n);
for(sum=0,i=0;n!=0;n=n/2,i++)
sum=sum+(n%2)*pow(10,i);
printf("Binary equal %ld",sum);
}
Execution:
Enter any integer: 91
Binary equal 1011011
Specification25:
Print the following output
7 1 6 2 5 3 4 4 3 5 2 6 1 7
Logic:
Here we use i=7, i-- to print 7 to 1 and we use j=1, j++ to print 1
to 7
Any condition either i>=1 or j<=7 can be used to
control the loop.
Program:
#include<stdio.h>
void main()
{
int i,j;
for(i=7,j=1;i>=1;i--,j++)
printf("%d\t%d\n",i,j);
}
0 comments:
Post a Comment