Welcome to the Triangular Number program! Enter the Triangular Number you\'d lik
ID: 3729319 • Letter: W
Question
Welcome to the Triangular Number program! Enter the Triangular Number you'd like to see:4 How would you 1ike to see your Triangular Number result? a) Represented with asterisks b) Represented with numbers (1, 2,.) c) I just want the final calculation Selection: a In the ET262fun/MiniProject/Phase2 directory, create a phase2.c file that meets the following: 1. Prompt the user to enter a triangular number to calculate o Save the user entry in an integer variable 2. Provide the user with a menu with options for result to display (shown in figure below). program again?Y Would you like to run Enter the Triangular Number you'd like to see: 3 How would you 1ike to see your Triangular Number result? this o Save the user entry in a character variable. 3. Use while loops for triangular number results/representations (follow Phase 1 requirements) 4. Use decision-making statements to handle menu selection entry. 5. Allow user to rerun the program as many times as desired. a) Represented with asterisks b) Represented with numbers (1, 2, ..) c) I just want the final calculation Selection: b o Welcome message should not be repeated o Thank you message should not be repeated 6. Modified/personalized, but complete, sentences are encouraged. 7. Header, in-program comments, and usual requirements must be complete 8. Submit one file per team. Would you like to run this program again? y Enter the Triangular Number you'd like to see: 5 How would you 1ie to see your Triangular Number result? Requirements shown in sample figure below: a) Represented with asterisks b) Represented with numbers (1, 2,.) c) I just want the final calculation Selection: c The Triangular number of 5 is 15 Would you like to run this program again? n Thank you for using this program, come back soonExplanation / Answer
#include <stdio.h>
void patAsterisk(int n)
{
int k = 2*n - 2;
for (int i=0; i<n; i++)
{
for (int spaces=0; spaces<k; spaces++)
printf(" ");
k = k - 1;
for (int j=0; j<=i; j++ )
{
printf("* ");
}
printf(" ");
}
}
void patNumbers(int n)
{
int num = 1;
int k = 2*n - 2;
for (int i=0; i<n; i++)
{
for (int spaces=0; spaces<k; spaces++)
printf(" ");
k = k - 1;
for (int j=0; j<=i; j++ )
{
printf("%d ",num);
num++;
}
printf(" ");
}
}
void calculate(int n)
{
int count = 0;
for(int i=1; i<=n ;i++)
count += i;
printf(" The triangular number of %d is %d ",n,count);
}
int main()
{
int n;
char ch, ch1;
printf("Welcome to the Triangular Number program! ");
while(1)
{
printf(" Enter the Triangular Number you'd like to see : ");
scanf("%d",&n);
printf(" How would you like to see your Triangular Number result? ");
printf(" a) Represented with asterisks * ");
printf(" b) Represented with numbers (1, 2, .. ");
printf(" c) I just want the final calculation ");
printf(" Selection : ");
scanf(" %c",&ch);
switch(ch)
{
case 'a':
patAsterisk(n);
break;
case 'b':
patNumbers(n);
break;
case 'c':
calculate(n);
break;
default:
printf("Invalid choice ");
break;
}
printf(" Would you like to run this program again? ");
scanf(" %c", &ch1);
if(ch1 == 'y' || ch1 == 'Y')
continue;
else if(ch1 == 'N' || ch1 == 'n')
break;
}
printf(" Thank you for using this program, come back soon! ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.