186k views
5 votes
How can I write a program in c language that calculates and produces these 2 columns sequence numbers using looping statement?

Enter a number: 5
Sequence: 1, 2, 3, 4, 5
Squared: 1, 4, 9, 16, 25

Do you want to continue? Y/N

N


User Immortal
by
6.7k points

1 Answer

5 votes

Answer:

#include <stdio.h>

int main(void) {

// Declare variables to store the number and the loop counter

int num, i;

char cont;

do {

// Prompt the user to enter a number

printf("Enter a number: ");

scanf("%d", &num);

// Print the sequence of numbers from 1 to the user-specified number

printf("Sequence: ");

for (i = 1; i <= num; i++) {

printf("%d, ", i);

}

printf("\\");

// Print the squares of the numbers from 1 to the user-specified number

printf("Squared: ");

for (i = 1; i <= num; i++) {

printf("%d, ", i * i);

}

printf("\\");

// Ask the user if they want to continue

printf("Do you want to continue? Y/N: ");

scanf(" %c", &cont);

} while (cont == 'Y' || cont == 'y');

return 0;

}

User Paul Alexander
by
7.0k points