c·c++/c 프로그래밍

순환호출과 반복문

바로이순간 2012. 3. 22. 10:32

문제1

1.

#include <stdio.h>

int psum(int a, int n) {

  if(n<=a) return a;

  return n+psum(a, n-1);

}

int main() {

  int a, b;

  int result;

  printf("enter number a, b: ")'

  scanf("%d %d", &a, &b);

  result=psum(a,b);

  printf("result is : %d", result);

  

  return 0;

}


2.

#include <stdio.h>

  int seq(int n) {

  if(n==1) return 1;

  return 5*seq(n-1)+1;

}

int main() {

  int x;

  int result;

  printf("enter number ")'

  scanf("%d", &x);

  result=seq(x);

  printf("result is : %d", result);


  return 0;

}


문제2

1.

#include <stdio.h>

int psum(int a, int b) {

  int i, sum=0;

  for(i=a;i<=b;i++) sum=sum+i;

  return n+sum;

}

int main() {

  int a, b;

  int result;

  printf("enter number a, b: ")'

  scanf("%d %d", &a, &b);

  result=psum(a,b);

  printf("result is : %d", result);


  return 0;

}


2.

#include <stdio.h>

int seq(int n) {

  int i, s=0;

  for(i=0;i<n;i++) s=5*s+1;

  return s;

}

int main() {

  int x;

  int result;

  printf("enter number: ")'

  scanf("%d", &x);

  result=seq(x);

  printf("result is : %d", result);


  return 0;

}