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));
}
#include
(This program has four for's.)
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");
}
}
#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;
}
}
#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");
}
There is insufficient information in the question to answer it. You did not specify what "this shape" was. Please restate the question.
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.
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?
There's a nice for Loop at the link below.
You may exit a nested loop in Java using a break with a label for the outer loop.
There is insufficient information in the question to answer it. You did not specify what "this shape" was. Please restate the question.
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
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.
Of course you can, loop is an option for animated gift only.
yes
for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { print what u need } print to go to next line }
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?
There's a nice for Loop at the link below.
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.
You may exit a nested loop in Java using a break with a label for the outer loop.
function pascal($depth){ $row = array(1); while($depth > 0){ $newRow = array(1); for($n = 1; $n < count($row); $n++){ $newRow[] = $row[$n - 1] + $row[$n]; } $newRow[] = 1; $depth --; echo implode(' ', $newRow) . "\n"; $row = $newRow; } }
HTML has no notion of a loop. This cannot be done.