You are on page 1of 9

NAME MUHAMMAD KHAN

ROLL 2K20/CSE/77

CLASS MORNING

SUBJECT PROGRAMMING

LAB TASK WEEK11

TOPIC ControlStatements
Week#11

Objectives:InthislabtaskofCProgramming,studentsunderstandcontrolstatements:forloopandwhileloop.

Q1.Practicea Cprogramto printallnaturalnumbersfrom1ton. -usingwhileloopasalreadysolvedbelowandfor loop.

#include<stdio.h>

intmain()
{
inti,end;

/*Inputanumberfromuser
*/
printf("Printallnaturalnumbersfrom1to:");
scanf("%d",&end);

/*
*Printnaturalnumbersfrom1toend
*/
i=1;
while(i<=end)
{
printf("%d\n",i);
i++;
}
return0;
}
Answer
Wefirstweputsomeintandthenweasktoenterthenumberthatyouwanna gotocounting.

Q2.Writea Cprogramtoprintallnaturalnumbersinreverse(fromn to1).- usingwhileloop

Answer;;

#include<stdio.h>

#include<conio.h>

intmain()

intb;

printf("enterthenumberwhichgoesto one");

scanf("%d",&b);
while(b>0)

{ printf("%d\n",b);

b--;

return(0);

Output;

enterthe numberwhichgoestoone5

Processreturned0(0x0) executiontime:8.218s

Pressanykeytocontinue.

Q3.Writea Cprogramtoprintallalphabetsfroma toz.- usingwhileloop

Answer
#include<stdio.h>

#include<conio.h>

intmain()

chara,b;

a='a';b='z';

while(a<=b)

printf("%c\n",a);

a++;

} return(0);}

Q4.Writea Cprogramtoprintallevennumbersbetween1to100.- Usingwhileloop

Answer

#include<stdio.h>

#include<conio.h>

intmain()

inta;

printf("enterthevalue100to seeevento100");

scanf("%d",&a);

while(a<=100)

if(a%2==0)
{

printf("%d\n",a);

a++;

return(0);

Q5.Writea Cprogramtoprintalloddnumberbetween1to100.(Usingforloop)

Answer

#include<stdio.h>

#include<conio.h>

intmain()

inta;

for(a=0;a<=100;a++)

if(a%2!=0)

printf("%d\n",a);

return(0);

OUTPUTIS1 TO100ODDNUMBERS;
Q6.Writea Cprogramtofindsumofallnaturalnumbersbetween1 to n.(Usingforloop) Answer;

#include<stdio.h>

#include<conio.h>

intmain()

inta,b,sum;

sum=0;

printf("enterthe numbertocountnaturalnumbersbetween1 tonumberinputed\t");

scanf("%d",&a);

for(b=0;b<=a;b++){

sum=b+sum;

printf("%d\n",sum);

return(0);

}}

Q7.Writea Cprogramtofindsumofallevennumbersbetween1 ton.(Usingforloop) Answer;;

#include<stdio.h>

#include<conio.h>

intmain()

inta,b,sum;

sum=0;

printf("enterthe numbertocountnaturalnumbersbetween1 tonumberinputed\t");

scanf("%d",&a);
for(b=0;b<=a;b++){

if (b%2==0)

sum=b+sum;

printf("%d\n",sum);

return(0);

Q8.Writea Cprogramtofindsumofalloddnumbersbetween1 ton.(Usingforloop) Answer

#include<stdio.h>

#include<conio.h>

intmain()

inta,b,sum;

sum=0;

printf("enterthe numbertocountnaturalnumbersbetween1 tonumberinputed\t");

scanf("%d",&a);

for(b=0;b<=a;b++){

if (b%2!=0)

sum=b+sum;

printf("%d\n",sum);

return(0);

}
Q9.Writea Cprogramtoprintmultiplicationtableofanynumber.(Usingforloop) Answer

#include<stdio.h>

#include<conio.h>

intmain()

inta,b;

printf("enterthe numberofwhichyouwannaseetable\t");

scanf("%d",&b);

for(a=1;a<=10;++a)

printf("%d*%d=%d\n",a,b,a*b);

return(0);

Q10.Writea Cprogramto printallASCIIcharacterwiththeirvalues.(Usingforloop)

Answer

#include<stdio.h>

#include<conio.h>

int main()

{
inta;

for(a=0;a<256;a++){

printf("%c\t",a);

a=a+1;

return(0);

You might also like