The programs that we have discussed so far used either a sequential or a decision control instruction. In the first one, the calculations were carried out in a fixed order; while in the second one, an appropriate set of instructions were executed depending on the outcome of the condition being tested.
These programs have a limited scope, because when executed they always perform the same series of actions, in the same way exactly once. But in real life if something is worth doing, it has to be done more than once.
Loops:
The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied.This repetitive operation is done through a loop control instruction.
Syntax of while loop:
while(condition)
{
set of statements;
increment statement;
}
//simple programs using while loop
//calculation of simple interest for 3 sets of p, n and r
#include<stdio.h>
#include<conio.h>
int main()
{
int p, n, count;
float r, si;
count=1;
while(count<=3)
{
printf("enter values of p, n and r\n");
scanf("%d%d%f",&p,&n,&r);
si=p*n*r/100;
printf("simple interest =rs %f\n",si);
count=count+1;
}
return 0;
}
output:
enter values of p, n and r
1000 5 13.5
simple interest =rs 675.000000
enter values of p, n and r
2000 5 13.5
simple interest =rs 1350.000000
enter values of p, n and r
3500 5 3.5
simple interest =rs 612.500000
//To print numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0,n;
printf("how many numbers do you want to print\n");
scanf("%d",&n);
while(i<=n)
{
printf("%d\n",i);
i++;
}
getch();
return 0;
}
output:
how many numbers do you want to print
5
0
1
2
3
4
5
//To print even numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int i=1,n;
printf("enter the number\n");
scanf("%d",&n);
printf("0\n");
while(i<=n)
{
if(i%2==0)
printf("%d\n",i);
i++;
}
getch();
return 0;
}
These programs have a limited scope, because when executed they always perform the same series of actions, in the same way exactly once. But in real life if something is worth doing, it has to be done more than once.
Loops:
The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied.This repetitive operation is done through a loop control instruction.
Syntax of while loop:
while(condition)
{
set of statements;
increment statement;
}
//simple programs using while loop
//calculation of simple interest for 3 sets of p, n and r
#include<stdio.h>
#include<conio.h>
int main()
{
int p, n, count;
float r, si;
count=1;
while(count<=3)
{
printf("enter values of p, n and r\n");
scanf("%d%d%f",&p,&n,&r);
si=p*n*r/100;
printf("simple interest =rs %f\n",si);
count=count+1;
}
return 0;
}
output:
enter values of p, n and r
1000 5 13.5
simple interest =rs 675.000000
enter values of p, n and r
2000 5 13.5
simple interest =rs 1350.000000
enter values of p, n and r
3500 5 3.5
simple interest =rs 612.500000
//To print numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0,n;
printf("how many numbers do you want to print\n");
scanf("%d",&n);
while(i<=n)
{
printf("%d\n",i);
i++;
}
getch();
return 0;
}
output:
how many numbers do you want to print
5
0
1
2
3
4
5
//To print even numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int i=1,n;
printf("enter the number\n");
scanf("%d",&n);
printf("0\n");
while(i<=n)
{
if(i%2==0)
printf("%d\n",i);
i++;
}
getch();
return 0;
}
output:
enter the number
12
0
2
4
6
8
10
12
No comments:
Post a Comment