본문 바로가기
학부공부/C_Basic

1. Input and Output[C Basic]

by sonpang 2021. 11. 6.
반응형

1.1.

Write a program that reads an integer and determines and prints whether it is odd or even. [Hint: Use the remainder operator. An even number is a multiple of two. Any multiple of two leaves a remainder of zero when divided by 2.]

#include<stdio.h>
int main()
{
	int a;
	printf("Input an integer : ");
	scanf("%d", &a);
	if (a % 2 == 0)
		printf("%d is an even integer", a);
	if(a % 2 == 1)
		printf("%d is an odd integer", a);

	return 0;
}

 

1.2.

Write a program that asks the user to enter two numbers, obtains them from the user and prints their (1) sum, (2) product, (3) difference, (4) quotient, and (5) remainder.

#include<stdio.h>
int main()
{
	int n1, n2;
	printf("Enter two numbers : ");
	scanf("%d %d", &n1, &n2);
	printf("The sum is %d\n", n1 + n2);
	printf("The product is %d\n", n1 * n2);
	printf("The difference is %d\n", n1 - n2);
	printf("The quotient is %d\n", n1 / n2);
	printf("The remainder is %d\n", n1 % n2);
	
	return 0;
}

 

1.3.

Write a program that reads in five integers and then determines and prints the largest and the smallest integers in the group.

#include<stdio.h>
int main()
{
	int n1, n2, n3, n4, n5, l, s;
	printf("Input 5 integers : ");
	scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5);
	l = n1;
	s = n2;
	if (n1 < n2)
	{
		l = n2;
		s = n1;
	}
	if (l < n3)
		l = n3;
	if (s > n3)
		s = n3;
	if (l < n4)
		l = n4;
	if (s > n4)
		s = n4;
	if (l < n5)
		l = n5;
	if (s > n5)
		s = n5;
	printf("The largest value is %d\n", l);
	printf("The smallest value is %d", s);

	return 0;
}

 

1.4.

Using only the techniques you learned in this chapter, write a program that calculates the squares and cubes of the numbers from 0 to 10 and uses tabs to print the following table of values:

#include<stdio.h>
int main()
{
	printf("number\tsquare\tcube\n");
	int cnt = 0;
	printf("%d\t%d\t%d\n", cnt, cnt*cnt, cnt*cnt*cnt);
	cnt++;
	printf("%d\t%d\t%d\n", cnt, cnt*cnt, cnt*cnt*cnt);
	cnt++;
	printf("%d\t%d\t%d\n", cnt, cnt*cnt, cnt*cnt*cnt);
	cnt++;
	printf("%d\t%d\t%d\n", cnt, cnt*cnt, cnt*cnt*cnt);
	cnt++;
	printf("%d\t%d\t%d\n", cnt, cnt*cnt, cnt*cnt*cnt);
	cnt++;
	printf("%d\t%d\t%d\n", cnt, cnt*cnt, cnt*cnt*cnt);
	cnt++;
	printf("%d\t%d\t%d\n", cnt, cnt*cnt, cnt*cnt*cnt);
	cnt++;
	printf("%d\t%d\t%d\n", cnt, cnt*cnt, cnt*cnt*cnt);
	cnt++;
	printf("%d\t%d\t%d\n", cnt, cnt*cnt, cnt*cnt*cnt);
	cnt++;
	printf("%d\t%d\t%d\n", cnt, cnt*cnt, cnt*cnt*cnt);
	cnt++;
	printf("%d\t%d\t%d\n", cnt, cnt*cnt, cnt*cnt*cnt);

	return 0;
}

 

1.5.

Write a program taking a five-digit positive integer as an input and printing following output.

1.Print out each digit in reverse order with 3-space distance.

2.Print the sum of all digits which are odd numbers.

 

[Hint: Use combinations of integer division and the remainder operation.]

#include<stdio.h>
int main()
{
	int n1, n2;
	printf("Input Two Integers : ");
	scanf("%d %d", &n1, &n2);
	printf("%d = %d * %d + %d\n", n1, n2, n1 / n2, n1%n2);

	if ((n1 / n2) % 2 == 0)
		printf("quotient is even\n");
	if ((n1 / n2) % 2 == 1)
		printf("quotient is odd\n");
	if ((n1%n2) % 2 == 0)
		printf("remainder is even");
	if ((n1%n2) % 2 == 1)
		printf("remainder is odd");

	return 0;
}

 

 

C Basic 문제의 출처는 모두 2019년도 1학기 COSE101 컴퓨터프로그래밍1 과목의 차성덕 교수님 수업입니다.

반응형

'학부공부 > C_Basic' 카테고리의 다른 글

6. Pointer[C Basic]  (0) 2021.11.07
5. Array and Functions[C Basic]  (0) 2021.11.07
4. Function[C Basic]  (0) 2021.11.07
3. Loop[C Basic]  (0) 2021.11.07
2. Loop[C Basic]  (0) 2021.11.06

댓글