Exp8
Exp8
Experiment: 8
PART A
Task 1: Identify output for below blocks of code without using Codeblocks. Give
justification.
a. #include<stdio.h> b. #include<stdio.h> void
void main() main()
{ int { int
c=1; c=0;
while(1) while(c<=5)
{ if(c==5) { c++;
break; printf(“\t if(c==3)
%d”,c); c++; continue;
} printf(“\t %d”,c);
} }
}
Expected Output:
Task 4: Write a C program to find sum of squares of numbers entered by the user till user
enters a negative number. The program will not find sum of square of a number, if it is
divisible by 3. (using do-while loop)
Task 5: Write a C program to print all the prime numbers from 1 to 100.
Theory:
A loop inside another loop is known as nested loop. We can write any loop inside any loop in
c i.e. we can write for loop inside the loop or while loop or do while loop etc.
PART B
Students must submit the soft copy as per following segments within two
hours of the practical. The soft copy must be uploaded on the portal at the
end of the practical.
The filename should be PPS_batch_rollno_experimentno Example: PPS_B2_B001_Exp1
Task 1:
a. #include<stdio.h> b. #include<stdio.h>
void main() void main()
{ int { int
c=1; c=0;
while(1) while(c<=5)
{ if(c==5) { c++;
break; printf(“\t if(c==3)
%d”,c); c++; continue;
} printf(“\t %d”,c);
} }
Output: }
1 2 3 4 Output:
The program will run till c=4 and at c=5 the 1 2 4 5 6
if condition satisfies and the program The program will break at c=3 therefore the
breaks. output wont include 3.
c. void main() d. void main() {
{ int i,j,x=0;
int i,j,k;
for(i=0;i<5;++i)
{ for (i = 1; i <= 5; i++) {
for(j=0;j<i;++j)
for (j = 1; j <= (5 - i) ;j++) printf(" ");
x+=(i+j-1);
printf("%d ",x); for (k = 1; k <= i; k++) printf(“%d”,i);
break;
printf(“\n”);}
}
printf("%d",x); }
}
Output:
Output:
1
01366
22
333
4444
Task 2:
Program:
#include<stdio.h>
int main()
int i,j,k;
for(i=1;i<=10;i++)
for(j=1;j<=10;j++)
k=i*j;
printf("%d\t",k);
printf("\n");
Task 3:
A)
Program:
#include<stdio.h>
int main()
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
Task 3:
B)
PROGRAM:
#include<std
io.h>
int main()
int i,j,k=1;
for(i=1;i<=4;
i++)
for(j=1;j<=4
-i;j++)
printf(" ");
for(j=1;j<=i;
j++)
printf("%d"
,k++);
printf("\n");
Task 4:
Program:
#include<std
io.h>
int main()
int
n,sum=0;
do
printf("Ente
r a No.");
scanf("%d",
&n);
if(n%3!
=0&&n>0)
sum=sum+n
*n;
while(n>0);
printf("Ans
wer=
%d",sum);
Task 5:
PROGRAM:
#include<std
io.h>
int main()
int
i,j,count;
for(i=1;i<=1
00;i++)
count=0;
for(j=2;j<=i/
2;j++)
if(i
%j==0)
count++;
break;
if(count==0
&&i!=1)
printf("
%d",i);
Conclusion (Learning Outcomes): Reflect on the questions answered by you jot down your
learnings about the Topic: Loops.
23456789
3456789
456789
56789
6789
789
89
PROGRAM:
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=8;i++)
{
for(j=i;j<=9;j++)
{
printf("%d",j);
}
printf("\n");
}
}
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
PROGRAM:
#include<stdio.h>
int main()
{
int ch=65;
int i,j,k,l;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("%c",ch++);
}
ch--;
for(l=1;l<i;l++)
{
printf("%c",--ch);
}
printf("\n");
ch=65;
}
}