Pascal's traingle in C Programming

Q. WAP to ask a number and print a pascal's triangle upto that number. Also prepare the algorithm & flowchart. 

For pascal's triangle, first of all we had developed algorithm like this:
Step1: Start
Step2: Declare & initialize i=0,j=0,num,cal,space=0
Step3: Read a number
Step4: Display "<space>" till space<=num-i
Step5: Initialize cal=1
Step6: Display cal in .2d format
           cal=cal*(i-j)/(j+1) until j<=i
Step7: Display new line
Step8: Repeat step 4 to 7 till i<num
Step9: Stop

After algorithm development we did Flowcharting for the same






































After flow charting we did coding which goes like this :
/*Program to print pascal's triangle */
#include<stdio.h>
int main()
{
    int i,j,num,cal,space;
    printf("\n Enter a number: ");
    scanf("%d",&num);
    for(i=0;i<num;i++)
        {
        for(space=0;space<=num-i;space++)
            {
            printf(" ");
            cal=1;   
            }
        for(j=0;j<=i;j++)
            {
            printf("%2d",cal);
            cal=cal*(i-j)/(j+1);
            }
        printf("\n");
        }   
    return 0;           
}

Output:
 












I have used Dev CPP to compile this program.
If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment