1
2 3
4 5 6
7 8 9 10
Write a c code to display the following pattern?
Let’s index lines from 1 to N. In the n-th line, there are n elements ending to n(n+1)/2.
We can write a function PrintLine(int n) which prints the n-th line like:
void PrintLine(int n)
{
int last = n * (n + 1) / 2;
for (int k = 0; k %26lt; n; k++)
printf (“%d\t”, last – k);
printf (“\n”);
}
Now we can use PrintLine () in our main:
main()
{
int N;
printf (“Enter N: “);
scanf(“%d”, %26amp;N);
int n;
for (n = 1; n %26lt;= N; n++)
PrintLine (n);
}
--------------------------------------...
Oops! Correction: the for loop in PrintLine():
for (int k = 1; k %26lt;= n; k++)
printf (“%d\t”, last – n + k);
Reply:for(i=0;i%26lt;4;i++)
{
for(j=0;j%26lt;=i;j++)
print j+1
}
For getting project assignment there are better websites like http://getafreelnacer.com/
Reply:The absence of ellipsis (…) makes it too easy! Just use four printf (C) or cout %26lt;%26lt; … (C++) lines!
Reply:hint: use a loop
Reply:#include %26lt;stdio.h%26gt;
void main()
{
int temp=3;
for(int i = 1, j = 1; i %26lt;= 10; i++)
{
printf("%d ", i);
if(i==j)
{
printf("\n");
j = temp;
}
temp = i+j+1;
}
}
Reply:printf ("1\n2 3\n4 5 6\n7 8 9 10\n");
But thats not the answer you want.
online florists
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment