SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
Experiment: 8
PART A
(PART A: TO BE REFFERED BY STUDENTS)
Aim:To study nested loops and do-while loop in C programming
Learning Outcomes: Learner would be able to
1. Interpret the scenario to decide on repetitive blocks.
2. Explain using algorithm and flowchart conditional constructs as per scenario.
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);
} }
}
c.void main() d.void main() {
{ inti,j,x=0;
inti,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); }
}
Task2: Write a C program to print multiplication table for 1 to 10.
Expected Output:
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
Task 3: Write a C program to print below patterns.
1 1
12 23
123 456
1234 7 8 9 10
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.
while(condition) do for(initialization; condition;
increment/decrement) {
{ while(condition) { statement(s);
for(initialization; condition;
{ statement(s); do
increment/decrement){
{ statement(s);
} statement(s);
statement(s); }while( condition ); }
} }while( condition ); statement(s);
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
Experiment: 8
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
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
Roll No.: N033 Name: Aaditya Nikam
Prog/Yr/Sem: MBA.Tech, 1st Year, (CS) Batch: E2
Date of Experiment: 28/08/2019 Date of Submission: 11/09/2019
Task 1: Identify output for below blocks of code without using Codeblocks. Give
justification.
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
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);
} }
}
c.void main() d.void main() {
{ inti,j,x=0;
inti,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); }
}
ANSWERS:
(1.1) The output is 1 2 3 4
(1.2) The output is 1 2 4 5 6
(1.3) The output is 0 0
(1.4) The output is 1
22
333
4444
55555
Task 2: Write a C program to print multiplication table for 1 to 10.
#include<stdio.h>
int main()
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
{
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;
}
Task 3: Write a C program to print below patterns.
(3.1) 1
12
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
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(" %d", j);
printf("\n");
printf(" \n ");
(3.2) 1
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
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++)
for (w=0;w<=(4-i); w++)
printf(" ");
for(j=1;j<=i;j++)
printf(" %d", k++);
printf("\n");
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
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
printf("\n enter a number \n");
scanf("%d", &a);
printf("\n");
if(a%3==0)
{continue;}
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
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.
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
#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();
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
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.
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
Home Work Questions:
1. Write a C program to print the following pattern till n lines.
123456789
23456789
3456789
456789
56789
6789
789
89
#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");
}
}
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
2. Write a C program to print the Alphabet Triangle.
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");}}
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
Prepared by: PanktiDoshi, RadhikaChapaneri, PratidnyaHegdepatil
This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.