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

3. Loop[C Basic]

by sonpang 2021. 11. 7.
반응형

3.1.

Write a program that inputs a series of 5 numbers and determines and prints the largest of the numbers

Use ‘do-while’ statement

#include<stdio.h>
int main()
{
	int i=0, num, largest;
	do {
		printf("Enter the number:");
		scanf("%d", &num);
		if (i == 0)
			largest = num;
		if (largest < num)
			largest = num;
	} while (++i < 5);
	printf("Largest is %d", largest);
	
	return 0;
}

 

3.2.

Write a program that prints a table of the binary equivalents of the decimal numbers in the range 0 through 15 

Use conditional operator ( ? : )

#include<stdio.h>
int main()
{
	int i, num;
	printf("Decimal\t\tBinary\n");
	for (i = 0;i <= 15;i++)
	{
		printf("%d\t\t", i);
		num = i;
		printf("%d", num<16 && num >= 8 ? 1 : 0);
		num %= 8;
		printf("%d", num<8 && num >= 4 ? 1 : 0);
		num %= 4;
		printf("%d", num<4 && num >= 2 ? 1 : 0);
		num %= 2;
		printf("%d", num<2 && num >= 1 ? 1 : 0);
		printf("\n");
	}
	
	return 0;
}

 

3.3.

Write a program that reads two positive integers ‘base’ and ‘exponent’. computes base to the power of exponent (𝑏𝑎𝑠𝑒^𝑒𝑥𝑝𝑜𝑛𝑒𝑛𝑡) and prints (For example, 4 3= 4x4x4 = 64)

#include<stdio.h>
int main()
{
	int n1, n2, rn=1, i;
	do {
		printf("Enter the base number: ");
		scanf("%d", &n1);
	} while (n1 < 0);
	do {
		printf("Enter the exponent number: ");
		scanf("%d", &n2);
	} while (n2 < 0);
	for (i = n2;i > 0;i--)//pow() math.h
	{
		rn = rn * n1;
	}
	printf("%d^%d is %d", n1, n2, rn);
	
	return 0;
}

 

3.4.

Write a program that prints the following diamond shape 

You may use printf statements that print either a single asterisk (*) or a single blank

Maximize your use of repetition and minimize the number of printf statements (Hint: Use nested for loops to generate the patterns)

#include<stdio.h>
int main()
{
	int i,j;
	for (i = 0;i < 5;i++) {
		for (j = 0;j <= 4 - i;j++)
			printf(" ");
		for (j = 0;j < i * 2 + 1;j++)
			printf("*");
		printf("\n");
	}
	for (i = 3;i >= 0;i--) {
		for (j = 0;j <= 4 - i;j++)
			printf(" ");
		for (j = 0;j < i * 2 + 1;j++)
			printf("*");
		printf("\n");
	}
	
	return 0;
}

 

3.5.

An online retailer sells five different products whose retail prices are shown in the following table:

Write a program that reads a series of number: (1) Product number and (2) Quantity sold for one day, and display the total retail value of all products sold last week (Use ‘switch’ statement)

#include<stdio.h>
int main()
{
	int item, quantities;
	double retail_price = 0.0;
	printf("Enter pairs of item numbers and quantities.\n");
	printf("Enter -1 for the item number to end input.\n");
	scanf("%d", &item);
	while(item!=-1) {
		scanf("%d", &quantities);
		switch (item) {
			case 1:{
				retail_price += 2.98*quantities;
				break;
			}
			case 2:{
				retail_price += 4.50*quantities;
				break;
			}
			case 3:{
				retail_price += 9.98*quantities;
				break;
			}
			case 4:{
				retail_price += 4.49*quantities;
				break;
			}
			case 5:{
				retail_price += 6.87*quantities;
				break;
			}
			default:{
				printf("Invalid product code: %d\n\t   Quantitiy: %d\n", item, quantities);
				break;
			}
		}
		scanf("%d", &item);
	}
	
	printf("The total retail value was:  %.2f",retail_price);
	
	return 0;
}

 

3.6.

Calculate the value of 𝜋 from the infinite series

#include<stdio.h>
int main()
{
	double pi = 0.0, denom;
	long int loop, accuracy, term;
	printf("Accuracy set at : ");
	scanf("%d", &accuracy);
	printf("Term set at : ");
	scanf("%d", &term);
	printf("term\t\t   pi\n");

	for (loop = 1;loop <= accuracy;loop++) {
		denom = loop * 2 - 1;
		if (loop % 2 == 0)
			pi -= 4 / denom;//(double)4/(loop*2-1)형변환  
		if (loop % 2 == 1)
			pi += 4 / denom;
		if (loop%term == 0)
			printf("%d\t\t%.6f\n", loop, pi);
	}

	return 0;
}
반응형

'학부공부 > 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
2. Loop[C Basic]  (0) 2021.11.06
1. Input and Output[C Basic]  (0) 2021.11.06

댓글