Velocity Reviews > what is the solution

# what is the solution

wahid
Guest
Posts: n/a

 01-10-2010
• 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

Ian Collins
Guest
Posts: n/a

 01-10-2010
wahid wrote:
> • Write a program

There's a novel idea, why don't you give it a try?

--
Ian Collins

Antoninus Twink
Guest
Posts: n/a

 01-10-2010
On 10 Jan 2010 at 7:08, wahid wrote:
> â€¢ 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;
}

Michael Foukarakis
Guest
Posts: n/a

 01-11-2010
On Jan 10, 9:22*am, Francis Glassborow
<(E-Mail Removed)> wrote:
> wahid wrote:
> > 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

>
> 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.

Albert
Guest
Posts: n/a

 01-20-2010
wahid wrote:
> • 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;
}