Utopper SkillUtopper Skill
  • Programming
    • Programming Examples
  • Interview Questions
    • DevOps Interview Questions
    • Android Interview Questions
  • How to
  • Tools
  • Top 10
  • Book Summaries
Reading: Print Triangle in C
Share
Utopper SkillUtopper Skill
Search
  • Book Summaries
  • Programming Examples
  • C Programming Example
  • Interview Question
  • How to
  • Top 10
Follow US
Utopper Skill > Programming Examples > C Programming Example > Print Triangle in C
Programming ExamplesC Programming Example

Print Triangle in C

Utopper Skill Team
By Utopper Skill Team Last updated: November 15, 2023
Share
4 Min Read
SHARE

Pseudo code to print triangle

1. Take the number of rows as input from the user
2. For each row from 1 to the number of rows:
   a. Print (number of rows - current row) spaces
   b. Print current row number of asterisks
   c. Move to the next line
3. End

This pseudo-code describes a simple algorithm for printing a triangle shape of asterisks, with one less space on each row than the previous row. The outer loop iterates from 1 to the number of rows and the inner loop iterates from 1 to the current row number which is used to print the number of asterisks. The first inner loop is used to print the spaces before the asterisks, and the second inner loop is used to print the asterisks. Each row has one more asterisk than the previous row, and one less space than the previous row, creating a triangle shape.

Approach to Print Triangle in C

  1. Take the number of rows as input from the user.
  2. Create a nested loop structure, with the outer loop iterating from 1 to the number of rows, and the inner loop iterating from ‘A’ to ‘A’ + current row – 1.
  3. Inside the outer loop, print (number of rows – current row) spaces before the inner loop starts.
  4. Inside the inner loop, print the current column character, which is the current iteration of the loop variable.
  5. Move to the next line after the inner loop finishes.
  6. End the program after the outer loop finishes.

C program to print the triangle

#include <stdio.h>

int main() {
    int i, j, rows;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for(i=1; i<=rows; i++) {
        for(j=1; j<=rows-i; j++) {
            printf(" ");
        }
        for(j=1; j<=i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}

When you run this program, it will prompt the user to enter the number of rows for the triangle and then it will print a triangle shape of asterisks.

For example, if the user enters 5 as the number of rows, the program will output:

    * 
   * * 
  * * * 
 * * * * 
* * * * * 

The outer loop controls the number of rows, and the inner loops control the number of spaces and asterisks in each row. The first inner loop is for printing the spaces before the asterisks, and the second inner loop is for printing the asterisks. Each row has one more asterisk than the previous row, and one less space than the previous row, creating a triangle shape.

TAGGED:C Program Examplesstar printing
Share This Article
Facebook Twitter Whatsapp Whatsapp Telegram Copy Link Print
Previous Article Alphabet Right Triangle in C Print Alphabet Right Triangle in C
Next Article DevOps Interview Questions 30+ Basic DevOps Interview Questions
Most Popular
Learn C Programming (Basic to Advanced)
ASCII Value in C Programming
Utopper Skill Author By Utopper Skill Author
JavaScript Interview Questions and Answers
Top 40 JavaScript Interview Questions and Answers (2024)
Utopper Skill Author By Utopper Skill Author
Learn C Programming (Basic to Advanced)
C Math Functions
Utopper Skill Author By Utopper Skill Author
Book Summary of Surrounded By Idiots By Thomas Erikson
Book Summary of Surrounded by Idiots By Thomas Erikson
Utopper Skill Author By Utopper Skill Author
C Interview Questions and Answers
Top 40 C Interview Questions With Detailed Answers Latest 2024
Utopper Skill Author By Utopper Skill Author

You Might Also Like

C++ Program To Print Prime Numbers From 1 To N
C Programming ExampleProgramming Examples

C++ Program To Print Prime Numbers From 1 To N

11 Min Read
C Program to Print Your Own Name
Programming ExamplesC Programming Example

C Program to Print Your Own Name with Example

2 Min Read
C Program To Convert Fahrenheit To Celsius
C Programming ExampleProgramming Examples

C Program To Convert Fahrenheit To Celsius

5 Min Read
C Program To Find LCM of Two Numbers
Programming ExamplesC Programming Example

C Program To Find LCM of Two Numbers

6 Min Read

Mail : [email protected]

Privacy Policy | DISCLAIMER | Contact Us

Learn

  • learn HTML
  • learn CSS
  • learn JavaScript

Examples

  • C Examples
  • C++ Examples
  • Java Examples

Study Material

  • Interview Questions
  • How to
  • Hosting
  • SEO
  • Blogging

 

© 2024 : UTOPPER.COM | Owned By : GROWTH EDUCATION SOLUTIONS PRIVATE LIMITED
Welcome Back!

Sign in to your account

Lost your password?