what is the solution

W

wahid

• Write a program, that would take a number n
from user, and then output the square and
cube of first n natural numbers.

Sample Output
• Enter a number: 5
• Output for 1: square 1, cube 1
• Output for 2: square 4, cube 8
• Output for 3: square 9, cube 27
• Output for 4: square 16, cube 64
• Output for 5: square 25, cube 125
 
A

Antoninus Twink

• Write a program, that would take a number n from user, and then
output the square and cube of first n natural numbers.

#include <stdio.h>
#include <limits.h>

int main(void)
{
int i, d, rv = 0;
fputs("Enter a Number: ", stdout);
fflush(stdout);
if(scanf("%d", &d) == 1 && d >= 1) {
if(1. * d * d * d > ULLONG_MAX) {
fputs("Integer overflow\n", stderr);
rv = 1;
} else
for(i = 1; i <= d; i++)
printf("Output for %d: square %llu, cube %llu\n", i, 1ULL * i * i,
1ULL * i * i * i);
} else {
fputs("Invalid input: needed a positive integer\n", stderr);
rv = 1;
}
return rv;
}
 
M

Michael Foukarakis

This is the fourth problem you have posted here without any added
material from you.

Either you are an unbelievably lazy student, or you are too stupid to be
studying programming or you are an extremely bad troll.

He's obviously Twink's alter ego. ;-)
 
A

Albert

wahid said:
• Write a program, that would take a number n
from user, and then output the square and
cube of first n natural numbers.

Sample Output
• Enter a number: 5
• Output for 1: square 1, cube 1
• Output for 2: square 4, cube 8
• Output for 3: square 9, cube 27
• Output for 4: square 16, cube 64
• Output for 5: square 25, cube 125

#include <stdio.h>

int main()
{
int i, n;

printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
printf("Output for %d: square %d, cube %d\n", i, i * i, i * i * i);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top