answersLogoWhite

0


Best Answer

int factorial (int n)

{

if (n==0) return 1;

else return (n*factorial(n-1));

}

int combo (int n, int r)

{

return (factorial (n))/(factorial (r) * (factorial (n-r)));

}

int width(int n, int i, int j)

{

return(j==0?(((n+1)*2)-((i==0)?i:(i-j))):(j<=3)?j+1:j-1);

}

void print (int n)

{

int i, j;

for (i = 0; i<=n; ++i)

{

for (j = 0; j<=i; ++j)

{

printf ("%*d", width(n,i,j) ,combo (i, j));

if (i==j)

{

printf("\n");

}

}

}

}

int main ()

{

int row,col;

int maxrow;

printf("\nPlease enter the num of rows for pascal triangle : ");

scanf("%d",&maxrow);

printf("The Pascal Triangle for %d rows is :\n",maxrow);

print(maxrow);

printf("To get the value at any row, col please enter \nRow:");

scanf("%d",&row);

printf("Col:");

scanf("%d",&col);

printf("\nThe value at row %d & col %d is : %d\n",row,col,combo(row,col));

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

#include long factorial(int); main() { int i, n, c; printf("Enter the number of rows you wish to see in pascal triangle\n"); scanf("%d",&n); for ( i = 0 ; i < n ; i++ ) { for ( c = 0 ; c <= ( n - i - 2 ) ; c++ ) printf(" "); for( c = 0 ; c <= i ; c++ ) printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c))); printf("\n"); } return 0; } long factorial(int n) { int c; long result = 1; for( c = 1 ; c <= n ; c++ ) result = result*c; return ( result );

(This program has four for's.)

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

the logic i have used is nCr => n!/(r!*(n-r)!)

for example the outer loop counter is 'n' and the inner loop counter is 'r' , then the corresponding element of the pascal's triangle will be nCr.

keep in mind that both the loops will have to start from zero.

#include

main()
{
int num,i,j,k,space;
int difffact=1,diff,n,r,x,y,comb=0,nfact=1,rfact=1;
printf("please enter the number of lines\n");
scanf("%d",&num);

k=num-1;
space=num-1;
for(i=0;i{
k=space--;
for(;k>0;k--)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
comb=0;
nfact=1;
rfact=1;
difffact=1;

for(n=i;n>=1;n--)
nfact=nfact*n;

for(r=j;r>=1;r--)
rfact=rfact*r;

diff=i-j;
for(;diff>=1;diff--)
difffact=difffact*diff;

comb=(nfact/(rfact*difffact));
printf("%d ",comb);
}
printf("\n");
}
}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

#include<iomanip>

#include<vector>

#include<sstream>

using namespace std;

std::vector<unsigned> create_pascals_triangle (const unsigned rows)

{

std::vector<unsigned> v;

for(unsigned i=0; i<rows; ++i)

{

unsigned coef=1;

for(unsigned j=0; j<=i; ++j)

{

if (!ji==j)

coef=1;

else

coef=coef*(i-j+1)/j;

v.push_back (coef);

}

}

return v;

}

unsigned input_rows()

{

while (true)

{

cout<<"Enter number of rows (2 to 25): ";

string input;

cin>>input;

stringstream ss;

ss<<input;

unsigned rows;

if (ss>>rows && rows && rows<=25)

return rows;

cerr<<"Invalid input!\n";

}

}

int main()

{

unsigned rows=input_rows();

vector<unsigned> v = create_pascals_triangle (rows);

unsigned width=rows<14?4:rows<21?6:8;

unsigned element=0;

for (unsigned i=0; i<rows; ++i)

{

for(unsigned space=0;space<rows-i;space++)

cout<<setw(width/2)<<' ';

for(unsigned j=0; j<=i; ++j)

cout<<setw(width)<<v[element++];

cout<<endl;

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include <stdio.h>

#include <cstdlib>

int main()

{

int n;

scanf("%d", &n);

for (int b=1; b<=n; ++b)

{

for (int a=1; a<=b; ++a)

{

printf("*");

}

printf("\n");

}

system("PAUSE");

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you create a Pascal triangle using only one 'for' loop using C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you create this shape using the for loop in c?

There is insufficient information in the question to answer it. You did not specify what "this shape" was. Please restate the question.


How do you create a loop (for loop, while loop) in Python?

In Python, you can create loops using different constructs, such as the for loop and the while loop. These loops allow you to repeatedly execute a block of code until a specific condition is met. Here's how you can create loops in Python: for loop: A for loop is used when you want to iterate over a sequence of elements such as a list, tuple, or string. It executes a block of code for each item in the sequence. for item in sequence: # Code block to be executed Here's an example that prints the numbers from 1 to 5 using a for loop: for num in range(1, 6): print(num) 2 while loop: A while loop is used when you want to repeat a block of code as long as a certain condition is true. It keeps executing the code block until the condition becomes false. while condition: # Code block to be executed Here's an example that prints the numbers from 1 to 5 using a while loop: num = 1 while num


How does loop work?

In programming, a loop works by conditionally jumping to the start of the loop and repeating the instructions. If the condition evaluates false, execution continues to the next instruction, thus breaking out of the loop. We can also break out of a loop from within the body of the loop itself using another conditional jump which jumps out of the loop. If we jump backwards out of a loop we effectively create an intertwined loop, known as spaghetti code which is difficult to read and maintain. Structured loops help make it easier to digest the logic. In C, a jump is achieved using a goto and a label. However, structured loops using for, while and do-while statements make loops much easier to read and maintain.


Can you create a gif image which will not loop?

Of course you can, loop is an option for animated gift only.


For loop program turbo c making a right triangle?

yes


How do you prepare a right angled triangle of using for loop?

for(int i=1;i&lt;=n;i++) { for(int j=1;j&lt;=i;j++) { print what u need } print to go to next line }


Write a program that input a positive integer and prints a triangle using for loop?

write a program that reads in the size of the side of square and then pints a hollow square of that size out of asterisks and blanks?


Flow chart using for loop?

There's a nice for Loop at the link below.


What is a mobius loop?

A mobius loop is the symbol often reffered to as the recycling symbol. It consits of three arrows pointing at eachother, forming a type of triangle.


How do you exit in nested loop in java?

You may exit a nested loop in Java using a break with a label for the outer loop.


How do you write pascal tringle in php using while loop?

function pascal($depth){ $row = array(1); while($depth &gt; 0){ $newRow = array(1); for($n = 1; $n &lt; count($row); $n++){ $newRow[] = $row[$n - 1] + $row[$n]; } $newRow[] = 1; $depth --; echo implode(' ', $newRow) . "\n"; $row = $newRow; } }


Fibonacci series using for loop in HTML?

HTML has no notion of a loop. This cannot be done.