Problem 2: Develop a C program that lets a user enter a number and display the f
ID: 671005 • Letter: P
Question
Problem 2: Develop a C program that lets a user enter a number and display the following output (on the command window) and write the following output into the text file named “pattern.txt”.
Each line must be printed out by calling each of the two functions:
// PrintLineFile prints out a single line to the file void PrintLineFile(int i, FILE *outp);
// PrintLine prints out a single line to the command window void PrintLine(int i);
Sample Code execution Sample Code execution Enter a number:5 Enter a number: 8 0 1 012 0123 01234 01 2345 01234 0123 012 0 1 012 0123 01234 012345 0 123456 01234567 012345678 01234567 0 123456 012345 01234 0123 012Explanation / Answer
or
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter loop repeat number(rows): ");
scanf("%d",&num);
for(r=0; r<=num; r++)
{
for(c=0; c<=r; c++)
printf("%d",c);
printf(" ");
}
for(; num>=0; num--)
{
for(c=0; c<=num; c++)
printf("%d",c);
printf(" ");
}
getch();
return 0;
}
or
#include<stdio.h>
#include<conio.h>
int main()
{
int num,n,r=0,c;
printf("Enter triangle number : ");
scanf("%d",&num);
while(num >= r)
{
c=1;
while(c <= r)
{
printf("%d",r);
c++;
}
printf(" ");
r++;
}
n=num-1;
while(n >= 0)
{
c=0;
while(c <= n)
{
printf("%d",n);
c++;
}
printf(" ");
n--;
}
getch();
return 0;
}
or
#include<stdio.h>
#include<conio.h>
int main()
{
int num,n,r,c;
printf("Enter triangle number : ");
scanf("%d",&num);
for(r=0; r<=num; r++)
{
for(c=0; c<=r; c++)
printf("%d",r);
printf(" ");
}
for(n=num-1; n>=0; n--)
{
for(c=0; c<=n; c++)
printf("%d",n);
printf(" ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.