close
Write a function that passes an integer n and calculates and prints the
square and cube of n and the sum of 1 to n. Write the main function
to read an integer n and call the function.

A possible session may look like as follows:

Input n: 4
number square cube sum(1..4)
1 1 1 1
2 4 8 3
3 9 27 6
4 16 64 10

Hint: Refer to the following program.

main() {

int i, n, sum = 0;
printf("Input n : ");
scanf("%d", &n);

printf("number sum(1..n)\n");
for (i = 1; i <= n; i++) {
sum += i;
printf("%4d %8d\n", i, sum);
}
============================================================
程式碼:

#include
#include

int square(int i)
{
int s;
s = i * i;
}

int cube(int i)
{
int c;
c = i * i * i;
}

sum(int n, int a[20])
{
int sum =0, i;
for (i = 1; i <= n; i++)
{
sum += i;
a[i] = sum;
}
}

main()
{

int i, n, a[20];
printf("Input n : ");
scanf("%d", &n);
sum(n,a);
printf("number square cube sum(1..n)\n");
for (i = 1; i <= n; i++)
printf("%4d %6d %5d %6d\n", i, square(i), cube(i),a[i]) ;
system("pause");
}
arrow
arrow
    全站熱搜

    小→。毅 發表在 痞客邦 留言(0) 人氣()