Write a C,C++ program to print the triangle pattern of numbers. In this program we take number as a input from user and print the triangle pattern.
Let's say if user input number 6. Then we print pattern like this.
Program to calculate sum of n numbers.
Triangle of Numbers.
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Steps to Print Triangle Pattern of Numbers
i) Take a number input from user.
ii) Use two for loops.
/* Initialize i=input number, as we printing triangle in reverse order. */ for(i=n;i>=1;i--) { /* For every i, print j from 1 to i.*/ for(j=1;j<=i;j++) { printf("%d ",j); } printf("\n "); }
C Program to Print Triangle Pattern of Numbers
#include<stdio.h> int main() { int i,j,n; /* Taking input from user. */ printf("Enter a number \n "); scanf("%d",&n); for(i=n;i>=1;i--) { for(j=1;j<=i;j++) { printf("%d ",j); } printf("\n "); } return 0; }
Output :
Enter a numer : 7
1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
C++ Program to Print Triangle Pattern of Numbers
#include <iostream>using namespace std; int main() { int i,j,n; /* Taking input from user. */ cout << "Enter a number \n "; cin >> n; for(i=n;i>=1;i--) { for(j=1;j<=i;j++) { cout << j; } cout << "\n "; } return 0; }
Output :
Enter a numer : 8
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Program to reverse an array.
Program to print even numbers from 1 to 100.
C, C++ Programming interview questions.
i do not think that second for loop give the right condition for the desire program it should be like this:- for(j=1;j<=i;j++)
ReplyDeleteWrite a program to print the following pattern based on user input.
ReplyDeleteFor eg: If N=4 the code should print the following pattern.
1*2*3*4*17*18*19*20
--5*6*7*14*15*16
----8*9*12*13
------10*11
Again if N=5 the code should print
1*2*3*4*5*26*27*28*29*30
--6*7*8*9*22*23*24*25
----10*11*12*19*20*21
------13*14*17*18
--------15*16
For N=2 the pattern will be
1*2*5*6
--3*4
Do not use blank spaces instead of dashes.
I could never frame the code.Please help.