Write a Program which Generates One Hundred Random Integers in the Range of 1 To
100, store them in an array and then prints the average. write three versions of the
program using Different Loop Constructs (e.g for, while and do. while).
Using For Loop
#include
#include
#include
void main()
{
int a[100],i;
float sum=0,avg;
clrscr();
randomize();
for(i=0;i<100;i++)
{
a[i]=random(100);
sum=sum+a[i];
}
avg=sum/100;
for(i=0;i<100;i++)
printf("%d\t",a[i]);
printf("\naverage=%f",avg);
getch();
}
Using While Loop
#include
#include
#include
void main()
{
int a[100],i;
float sum=0,avg;
clrscr();
randomize();
while(i<100)
{
a[i]=random(100);
sum=sum+a[i];
i++;
}
i=0;
avg=sum/100;
while(i<100)
{
printf("%d\t",a[i]);
i++;
}
printf("\naverage=%f",avg);
getch();
}
Using do-While Loop
#include
#include
#include
void main()
{
int a[100],i;
float sum=0,avg;
clrscr();
randomize();
do
{
a[i]=random(100);
sum=sum+a[i];
printf("%d\t",a[i]);
i++;
}
avg=sum/100;
printf("\naverage=%f",avg);
getch();
}
100, store them in an array and then prints the average. write three versions of the
program using Different Loop Constructs (e.g for, while and do. while).
Using For Loop
#include
#include
#include
void main()
{
int a[100],i;
float sum=0,avg;
clrscr();
randomize();
for(i=0;i<100;i++)
{
a[i]=random(100);
sum=sum+a[i];
}
avg=sum/100;
for(i=0;i<100;i++)
printf("%d\t",a[i]);
printf("\naverage=%f",avg);
getch();
}
Using While Loop
#include
#include
#include
void main()
{
int a[100],i;
float sum=0,avg;
clrscr();
randomize();
while(i<100)
{
a[i]=random(100);
sum=sum+a[i];
i++;
}
i=0;
avg=sum/100;
while(i<100)
{
printf("%d\t",a[i]);
i++;
}
printf("\naverage=%f",avg);
getch();
}
Using do-While Loop
#include
#include
#include
void main()
{
int a[100],i;
float sum=0,avg;
clrscr();
randomize();
do
{
a[i]=random(100);
sum=sum+a[i];
printf("%d\t",a[i]);
i++;
}
avg=sum/100;
printf("\naverage=%f",avg);
getch();
}
1 comments:
Thanks man!
Post a Comment