6.16 Describe the output that will be generated by each of the following c program.(Note the similartion in the programs that are shown from each other)
Solution:
a)
#include<stdio.h>
int main()
{
int i=0,x=0;
while(i<20)
{
if(i%5==0)
{
x+=i;
printf("%d",x);
}
i++;
}
printf(" x=%d",x);
}
ans: Output: 0 5 15 30
x=30
b)
#include<stdio.h>
int main()
{
int i=0,x=0;
do
{
if(i%5==0)
{
x++;
printf("%d",x);
}
++i;
}
while(i<20);
printf(" x=%d",x);
}
ans: Output: 1 2 3 4
x=4
c)
#include<stdio.h>
int main()
{
int i=0,x=0;
for(i=1;i<10; i*=2)
{
x++;
printf("%d",x);
}
printf(" x=%d",x);
}
ans: Output: 1 2 3 4
x=4
d)
#include<stdio.h>
int main()
{
int i=0,x=0;
for(i=1;i<10; ++i)
{
if(i%2==1)
x+=1;
else
x--;
printf("%d ",x);
}
printf(" x=%d",x);
}
ans: Output: 1 0 1 0 1 0 1 0 1
x=1
thistimebd Bangladesh Live online newsportal, education, Lifestyle, Health, Photography, gif image etc.
Make your own name or company name website | contact: thistimebd24@gmail.com
Copyright © 2020-2024 News Portal in Bangladesh - THISTIMEBD.COM. ALL Rights Reserved.