submission 8
submission 8
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.
Experiment: 8
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 bePPS_batch_rollno_experimentno Example: PPS_B2_B001_Exp1
Task 1: Identify output for below blocks of code without using Codeblocks. Give
justification.
ANSWERS:
#include<stdio.h>
int main()
{
inti=1,j=1,m=0;
for(i=1; i<=10; i++)
{
for(j=1; j<=10; j++)
{
m=j*i;
printf("%d \t" , m);
}
printf("\n");
}
return 0;
}
(3.1) 1
12
123
1234
ANSWERS
(3.1)
#include<stdio.h>
int main()
inti,j;
for(i=1;i<5;i++)
for (j=1;j<=i;j++)
printf("\n");
printf(" \n ");
(3.2) 1
23
456
7 8 9 10
ANSWER
(3.2)
#include<stdio.h>
int main()
int i,j,k,w;
k=1;
for(i=1;i<=4;i++)
printf(" ");
for(j=1;j<=i;j++)
printf("\n");
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)
#include <stdio.h>
int main()
{ int a,sum=0;
do
scanf("%d", &a);
printf("\n");
if(a%3==0)
{continue;}
else if(a<0)
{ continue;}
else if (a>=0)
{ sum=sum + (a*a);}
} while (a>0);
printf("\n");
printf("\n the sum of the squares of the numbers that are not divisible by 3 = %d ", sum );
printf("\n");}
Task 5: Write a C program to print all the prime numbers from 1 to 100.
#include<stdio.h>
int main()
{ int i,j,k;
for(i=1;i<=100;i++)
{k=0;
for(j=1;j<=100;j++)
if(i%j==0)
k++;
if(k==2)
printf("%d ",i);
} getch();
Conclusion (Learning Outcomes): Understood the use of for loops to make patterns which
look aesthetically pleasing. Nested for loops are used to make patterns easier. Tap space (\t)
gives 8 spaces between the outputs. We came to know the output by using for loop logic.
Learned to use continue statement for a given for loop.
#include <stdio.h>
int main()
{ int i,j,k,w,n;
printf("enter till what number of lines you want the pattern to be printed :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ for(j=i; j<=n;j++)
printf(" %d",j);
printf("\n");
}
}
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
#include<stdio.h>
int main()
{ int a,b,c,d,row,e=1,f;
printf("\n enter the number of rows = ");
scanf("%d", &row);
for (a=1;a<=row;a++)
{d=65;
for (b=1;b<=(row-a); b++)
{printf(" ");}
for(c=1;c<=e;c++)
{
if(c<=a)
{
printf ("%c", d);
d++;
f=d-1;
}
else
{
f--;
printf("%c",f);
}}
e=e+2;
printf("\n");}}