0% found this document useful (0 votes)
2 views

Exp8

The document outlines a programming course experiment focused on nested loops and do-while loops in C programming. It includes various tasks such as identifying outputs of code snippets, writing programs for multiplication tables, patterns, sums of squares, and prime numbers. Additionally, it emphasizes the importance of submitting work in a specified format and contains theoretical explanations of loops.

Uploaded by

aaditya.nikam41
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Exp8

The document outlines a programming course experiment focused on nested loops and do-while loops in C programming. It includes various tasks such as identifying outputs of code snippets, writing programs for multiplication tables, patterns, sums of squares, and prime numbers. Additionally, it emphasizes the importance of submitting work in a specified format and contains theoretical explanations of loops.

Uploaded by

aaditya.nikam41
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

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() {


{ 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); }
}

Task2: Write a C program to print multiplication table for 1 to 10.

Expected Output:

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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 be PPS_batch_rollno_experimentno Example: PPS_B2_B001_Exp1

Roll No.: N033 Name: Aaditya Nikam

Prog/Yr/Sem: MBA Tech CS 1st sem Batch: E2

Date of Experiment: Date of Submission:

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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 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:

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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

Write a C program to print multiplication table for 1 to 10.

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:

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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)

Program:

#include<stdio.h>

int main()

int n,i,j;

printf("Enter no. of lines:");

scanf("%d",&n);

for(i=1;i<=n;i++)

for(j=1;j<=i;j++)

printf("%d",i);

printf("\n");

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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:

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(" ");

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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

for(j=1;j<=i;

j++)

printf("%d"

,k++);

printf("\n");

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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:

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;

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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

while(n>0);

printf("Ans

wer=

%d",sum);

Task 5:

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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

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)

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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

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.

Home Work Questions:

1. Write a C program to print the following pattern till n lines.


123456789

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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

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");
}
}

2.Write a C program to print the Alphabet Triangle.

A
ABA

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


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

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;
}
}

Prepared by: Pankti Doshi, Radhika Chapaneri, Pratidnya Hegdepatil


This document is only meant for Academic Purpose and Internal Circulation only. Copying, xeroxing,
emailing outside MPSTME Campus is strictly not allowed.

You might also like