Triangle in C
Q. Write a C program number triangle ?
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i, j, c = 1;
clrscr();
printf("Enter the no of rows for Floyd's triangle: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d\t",c);
c++;
}
printf("\n");
}
getch();
}
Q. Write an algorithm to number triangle ?
1. Start
2. Declare integer variables `n`, `i`, `j`, and `c`.
3. Initialize `c` to 1. (`c` keeps track of the current number to print in Floyd's triangle)
4. Prompt the user to enter the number of rows for Floyd's triangle.
5. Read the value of `n`.
6. Loop `i` from 1 to `n` (inclusive):
a. Loop `j` from 1 to `i` (inclusive):
i. Print the value of `c` followed by a tab space.
ii. Increment `c` by 1.
b. After the inner loop ends, print a newline to move to the next row.
7. End the program.
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com