SlideShare a Scribd company logo
How to Create Pyramids in C
Learn Creating Pyramids
2 lines
3 lines
4 lines
Analysis-
1.Input – number of lines(n) given by user(4 in
the above case)
2.Output – n number of lines and n number
of stars in each line(above shape has 4 lines
and each line has 4 stars)
4 lines
Design-
1. One loop is needed for n
number of lines
2. And other loop is needed for n
stars in each line
3. Printing element *
4. New line after printing all
elements in line
Coding-
for(lines=1;lines<=n; lines++)
{
for(elements=1;elements<=n; elements++)
{
printf(“*”);
}
printf(“n”);
}
Coding-
for(lines=1;lines<=n; lines++)
{
for(elements=1;elements<=n; elements++)
{
printf(“*”);
}
printf(“n”);
}
1 to n means n times(for n lines)
Coding-
for(lines=1;lines<=n; lines++)
{
for(elements=1;elements<=n; elements++)
{
printf(“*”);
}
printf(“n”);
}
1 to n means n times(for n stars)
Coding-
for(lines=1;lines<=n; lines++)
{
for(elements=1;elements<=n; elements++)
{
printf(“*”);
}
printf(“n”);
}
New line after
printing all stars in
line
Coding-
for(lines=1;lines<=n; lines++)
{
for(elements=1;elements<=n; elements++)
{
printf(“*”);
}
printf(“n”);
} Printing element *
Coding-
for(lines=1;lines<=n; lines++)
{
for(elements=1;elements<=n; elements++)
{
printf(“*”);
}
printf(“n”);
}
1 to n means n times(for n lines)
1 to n means n times(for n stars)
New line after
printing all stars in
line
Printing element *
Complete Program
#include<stdio.h>
main()
{
int n, lines, elements;
printf("enter how many lines ");
scanf("%d", &n);
for(lines=1;lines<=n; lines++)
{
for(elements=1;elements<=n; elements++)
{
printf("*");
}
printf("n");
}
}
How program works?-1
Suppose n =3
1. lines=1, lines<=n means 1<=3(true)
i. elements=1, elements<=n means 1<=3(true)
print *
elements++ = 2
ii. elements=2, elements<=n means 2<=3(true)
print *
elements++ = 3
iii. elements=3, elements<=n means 3<=3(true)
print *
elements++ = 3
iv. elements=4, elements<=n means 4<=3(false)
NEW LINE
lines++= 2
Output
***
_
How program works?-2
2. lines=2, lines<=n means 2<=3(true)
i. elements=1, elements<=n means 1<=3(true)
print *
elements++ = 2
ii. elements=2, elements<=n means 2<=3(true)
print *
elements++ = 3
iii. elements=3, elements<=n means 3<=3(true)
print *
elements++ = 3
iv. elements=4, elements<=n means 4<=3(false)
NEW LINE
lines++= 3
Output
***
_***
_
How program works?-3
3. lines=3, lines<=n means 3<=3(true)
i. elements=1, elements<=n means 1<=3(true)
print *
elements++ = 2
ii. elements=2, elements<=n means 2<=3(true)
print *
elements++ = 3
iii. elements=3, elements<=n means 3<=3(true)
print *
elements++ = 3
iv. elements=4, elements<=n means 4<=3(false)
NEW LINE
lines++= 4
Output
***
_***
_***
_
How program works?-4
1. lines=3, lines<=n means 4<=3(false)
LOOP Finished
Output
***
_***
_***
_
Question
Q. What is ‘n’ in Coding Section?
A. n is the number of lines and number of stars
in each line, given by user.
Q. Can we execute loop from n to 1?
A. Yes, lines and elements both loop can be run
from n to 1. We can also run one loop from 1 to
n and other from n to 1. It will create no effect
on output.
Run lines loop from n to 1
for(lines=n;lines>=1; lines--)
{
for(elements=1;elements<=n; elements++)
{
printf(“*”);
}
printf(“n”);
} It will create no effect on output.
Run elements loop from n to 1
for(lines=1;lines<=1; lines++)
{
for(elements=n;elements>=1; elements--)
{
printf(“*”);
}
printf(“n”);
} It will create no effect on output.
Run both loops from n to 1
for(lines=n;lines>=1; lines--)
{
for(elements=n;elements>=1; elements--)
{
printf(“*”);
}
printf(“n”);
} It will create no effect on output.
Second
Pyramid
4 lines
3 lines
2 lines
Analysis-
1. Input – number of lines(n) given by user(4 in the above case)
2. Output –
a. n number of lines
b. Line 1 -> 1 star
Line 2 -> 2 stars
Line 3 -> 3 stars
Line n -> n stars
4 lines
Design-
1. One loop is needed for n number of
lines
2. And other loop is needed for
printing elements
3. Number of elements = line number
4. Printing element *
5. New line after printing all elements
in line
Coding-
for(lines=1;lines<=n; lines++)
{
for(elements=1;elements<=lines; elements++)
{
printf(“*”);
}
printf(“n”);
}
New Change
Complete Program
#include<stdio.h>
main()
{
int n, lines, elements;
printf("enter how many lines ");
scanf("%d", &n);
for(lines=1;lines<=n; lines++)
{
for(elements=1;elements<=lines; elements++)
{
printf("*");
}
printf("n");
}
}
How program works?-1
Suppose n =4
1. lines=1, lines<=n means 1<=3(true)
i. elements=1, elements<=lines means 1<=1(true)
print *
elements++ = 2
ii. elements=2, elements<=lines means 2<=1(false)
NEW LINE
lines++= 2
Output
*
_
How program works?-2
2. lines=2, lines<=n means 2<=3(true)
i. elements=1, elements<=lines means 1<=2(true)
print *
elements++ = 2
ii. elements=2, elements<=lines means 2<=2(true)
print *
elements++ = 3
iii. elements=3, elements<=lines means 3<=2(false)
NEW LINE
lines++= 3
Output
*
_**
_
How program works?-3
3. lines=3, lines<=n means 3<=3(true)
i. elements=1, elements<=lines means 1<=3(true)
print *
elements++ = 2
ii. elements=2, elements<=lines means 2<=3(true)
print *
elements++ = 3
iii. elements=3, elements<=lines means 3<=3(true)
print *
elements++ = 4
iv. elements=4, elements<=lines means 4<=3(false)
NEW LINE
lines++= 4
Output
*
_**
_***
_
How program works?-4
4. lines=4, lines<=n means 4<=3(false) Output
*
_**
_***
_

More Related Content

PDF
Magic Methods (Python meetup)
PPTX
Java Foundations: Arrays
PPTX
Stack - Data Structure - Notes
PPTX
CSE240 Pointers
DOCX
Functions reading passage
PPT
Data structure lecture7
PPTX
Abir ppt3
PPTX
Dev Concepts: Data Structures and Algorithms
Magic Methods (Python meetup)
Java Foundations: Arrays
Stack - Data Structure - Notes
CSE240 Pointers
Functions reading passage
Data structure lecture7
Abir ppt3
Dev Concepts: Data Structures and Algorithms

What's hot (19)

PDF
Algorithms Lecture 4: Sorting Algorithms I
PPTX
16. Arrays Lists Stacks Queues
PDF
Assignment
PPTX
Java Foundations: Data Types and Type Conversion
PPTX
Learn Function The Hard Way
DOC
Exception Example in Python
PPT
Data Structures- Part3 arrays and searching algorithms
PPTX
Ex1 d sketchlinfuncs
PPT
Data Structures by Maneesh Boddu
PPT
Data Structure and Algorithms Stacks
PDF
paython practical
PPTX
Queue ppt
PPTX
1.2 matlab numerical data
PDF
Exercise2 java
PPT
358 33 powerpoint-slides_8-linked-lists_chapter-8
PPTX
Assignment method
PPTX
Unit II - LINEAR DATA STRUCTURES
PPTX
Queue Data Structure
Algorithms Lecture 4: Sorting Algorithms I
16. Arrays Lists Stacks Queues
Assignment
Java Foundations: Data Types and Type Conversion
Learn Function The Hard Way
Exception Example in Python
Data Structures- Part3 arrays and searching algorithms
Ex1 d sketchlinfuncs
Data Structures by Maneesh Boddu
Data Structure and Algorithms Stacks
paython practical
Queue ppt
1.2 matlab numerical data
Exercise2 java
358 33 powerpoint-slides_8-linked-lists_chapter-8
Assignment method
Unit II - LINEAR DATA STRUCTURES
Queue Data Structure
Ad

Viewers also liked (8)

PDF
12th ip CBSE chapter 4 oop in java notes complete
DOCX
Difference between switch and ladder if
PPTX
Basics of file handling
PPTX
Simple class and object examples in java
DOCX
Sql like operator examples
DOCX
12th information practices mysql practice questions
PPTX
Inline functions in c++
PDF
100 images for visual brainstorming
12th ip CBSE chapter 4 oop in java notes complete
Difference between switch and ladder if
Basics of file handling
Simple class and object examples in java
Sql like operator examples
12th information practices mysql practice questions
Inline functions in c++
100 images for visual brainstorming
Ad

Similar to Learn How to create pyramids in c (20)

PPTX
R1-Intro (2udsjhfkjdshfkjsdkfhsdkfsfsffs
PPTX
Two dimensional arrays
PPT
Lecture#9 Arrays in c++
PDF
CP PPT_Unit IV computer programming in c.pdf
PDF
C programs
DOCX
DS LAB RECORD.docx
PPT
Arrays in C++
PPTX
07. Arrays
PPTX
unit-2-dsa.pptx
PPTX
Module_3_Arrays - Updated.pptx............
DOCX
object oriented programming lab manual .docx
PDF
05 queues
PPTX
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
PDF
Data structure and algorithm.(dsa)
PDF
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
PPTX
arrays in c programming - example programs
DOCX
Array
PDF
Arrays and function basic c programming notes
PDF
2D array
R1-Intro (2udsjhfkjdshfkjsdkfhsdkfsfsffs
Two dimensional arrays
Lecture#9 Arrays in c++
CP PPT_Unit IV computer programming in c.pdf
C programs
DS LAB RECORD.docx
Arrays in C++
07. Arrays
unit-2-dsa.pptx
Module_3_Arrays - Updated.pptx............
object oriented programming lab manual .docx
05 queues
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
Data structure and algorithm.(dsa)
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
arrays in c programming - example programs
Array
Arrays and function basic c programming notes
2D array

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Complications of Minimal Access Surgery at WLH
2.FourierTransform-ShortQuestionswithAnswers.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
Microbial diseases, their pathogenesis and prophylaxis
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Chinmaya Tiranga quiz Grand Finale.pdf
01-Introduction-to-Information-Management.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
RMMM.pdf make it easy to upload and study
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

Learn How to create pyramids in c