Homework1 2
Homework1 2
Homework1 2
#include <stdio.h>
int main(void)
{
int i;
for(i = 0;i < 5; i++)
{
int temp = 1;
printf("temp = %d\n", temp);
temp++;
}
return 0;
}
11
#include <stdio.h>
int main(void)
{
int temp;
printf("temp = %d\n", temp);
return 0;
}
13
#include <stdio.h>
void inc(int counter);
int main(void)
{
int i;
i = 10;
printf(" i=%d\n", i);
inc(i);
printf(" i=%d\n", i);
return 0;
}
void inc(int counter)
{
counter++;
}
15
#include<stdio.h>
int A;
int B;
int add()
{
return A + B;
}
int main(void)
{
int answer;
A = 5;
B = 7;
answer = add();
printf(“ % d + % d = % d\n”, A, B, answer);
return 0;
}
16
#include <stdio.h>
int counter;
int main(void)
{
printf("counter = % d\n", counter);
return 0;
}
18
#include <stdio.h>
int x;
void sub();
int main(void)
{
for (x = 0; x < 10; x++) sub();
}
void sub()
{
for (x = 0; x < 10; x++) printf("");
}
19
#include <stdio.h>
int x;
void sub();
int main(void)
{
for (x = 0; x < 10; x++) sub();
}
void sub()
{
int x;
for (x = 0; x < 10; x++) printf("");
printf(“\n”);
}
21
#include <stdio.h>
int sum = 1;
int main(void)
{
int sum = 0;
printf("sum = %d\n", sum);
return 0;
}
22
#include <stdio.h>
int sum = 1;
int main(void)
{
// int sum = 0;
printf("sum = %d\n", sum);
return 0;
}
28
#include <stdio.h>
void sub() {
static int scount = 0;
int acount = 0;
printf("scount = %d\t", scount);
printf("acount = %d\n", acount);
scount++;
acount++; }
int main(void) {
sub();
sub();
sub();
return 0; }
33
#include <stdio.h>
void save(int amount) {
static long balance = 0;
if (amount >= 0) printf("%d \t\t", amount);
else printf("\t %d \t", -amount);
balance += amount;
printf("%d \n", balance); }
int main(void) {
printf("==============================\n");
printf(" \t \t \n");
printf("==============================\n");
save(10000);
save(50000);
save(-10000);
save(30000);
printf("==============================\n");
return 0; }
38
#include <stdio.h>
//extern void f1();
extern void f2();
int main(void)
{
f2();
return 0;
}
#include <stdio.h>
static void f1()
{
printf("f1() .\n");
}
void f2()
{
printf("f2() .\n");
}
37
#include <stdio.h>
int all_files;
static int this_file;
extern void sub();
int main(void)
{
sub();
printf("%d\n", all_files);
return 0;
}
extern int all_files;
//extern int this_file;
void sub(void)
{
all_files = 10;
}
39
#include <stdio.h>
int x = 50;
int main(void)
{
int x = 100;
{
extern int x;
printf("x= %d\n", x);
}
return 0;
}
40
#include <stdio.h>
int x = 50;
int main(void)
{
int x = 100;
{
// extern int x;
printf("x= %d\n", x);
}
return 0;
}
47
#include <stdio.h>
int factorial(int n)
{
printf("factorial(%d)\n", n);
if (n <= 1) return 1;
else return n * factorial(n - 1);
}
int main(void)
{
int n ;
printf(" : ");
scanf(“ %d”, &n);
printf(“ %d! %d . \n”, n, factorial(n));
return 0;
}
49
#include <stdio.h>
int gcd(int x, int y);
int main(void)
{
printf("%d\n", gcd(30, 20));
}
int gcd(int x, int y);
{
if (y == 0) return x;
else return gcd(y, x % y);
}