There are many ways of solving a programming problem , and every one has his own logic to solve a problem. Here I also implemented a new logic using C language’s printf() function.
printf() function is more than just writing output to the screen i.e. it returns the number of characters it writes on the output screen.
One functionality of printf() is as follows.
printf(“Hello”+1); = ello
printf(“ABCDE”+2); = CDE
It skips the first n characters if we are using printf(“message”+n);
Another functionality of printf method is :
If we write => int num = printf(“Hello”); . Then num will have value 5 (Hello has 5 letters).
Similarly if we write,
int num = printf(“2017”);. Then num will have value 4 (2017 has 4 characters).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include<conio.h> #include<stdio.h> int main(){ char star[80]; char space[80]; int line,i; clrscr(); printf("Enter number of lines in the pyramid = "); scanf("%d",&line); for(i=0;i < line;i++){ star[i]='*'; space[i]=' '; } space[line-1]='\0'; star[line]='\0'; for(i=1;i < =line;i++){ printf(space+(i-1)); printf(star+(line-i)); printf(star+(line+1-i)); printf("\n"); } getch(); return 0; } |