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

4. Function[C Basic]

by sonpang 2021. 11. 7.
반응형

4.1.

Define a function ‘hypotenuse’ that calculates the length of the hypotenuse of a right triangle when the other two sides are given

The function should take two argument of type double and return the hypotenuse as a double

#include<stdio.h>
#include<math.h>

double hypotenuse(double side1, double side2);

int main()
{
	int i;
	double n1, n2;

	for (i = 0;i < 3;i++) {
		printf("Enter the sides of the triangle : ");
		scanf("%lf %lf", &n1, &n2);

		printf("Hypotenuse : %0.1lf\n", hypotenuse(n1, n2));
	}

	return 0;
}

double hypotenuse(double side1, double side2)
{
	return (sqrt(pow(side1, 2) + pow(side2, 2)));
}

 

4.2.

An integer is said to be a perfect number if its factors, including 1, sum to the number

For example, 6 is a perfect number because 6 = 1+2+3

Write a function ‘perfect’ that determines whether parameter number is a prefect number

The numbers are in the range 1 through 1000

#include<stdio.h>

int perfect_num(int num);

int main()
{
	int i;
	
	printf("For the integers from 1 to 1000:\n");
	for (i = 2;i < 1000;i++)
	{
		if (perfect_num(i))
			printf("%d is perfcet \n", i);
	}

	return 0;
}

int perfect_num(num)
{
	int i, factor_sum=1;
	for (i = 2;i < num;i++){
		if (num%i == 0)
			factor_sum += i;
	}
	if (factor_sum == num)
		return 1;
	else
		return 0;
}

 

4.3.

Write a program that inputs a character and passes it to function ‘ulcase’, which uses the ASCII values to determine whether the given character is in uppercase or lowercase

The function should take a character argument and return 1 (true) if it is uppercase and 0 (false) otherwise

#include<stdio.h>

int ulcase(char c1);

int main()
{
	char c1;
	printf("Enter the character : ");
	scanf("%c", &c1);
	
	printf("%c: %s\n", c1, ulcase(c1) ? "Uppercase" : "Lowercase");

	return 0;
}

int ulcase(char c1)
{
	if (c1>='A'||c1<='Z')
		return 1;
	else
		return 0;
}

 

4.4.

Write a function that takes an integer value and returns the number with its digits reversed

For example, given the number 7631, the function should return 1367

#include<stdio.h>
#include<math.h>

int reverse(int num);

int main()
{
	int n1;
	printf("Enter a number between 1 and 9999: ");
	scanf("%d", &n1);
	printf("The number with its digits reversed is: %d",reverse(n1));

	return 0;
}
int reverse(num)
{
	int reverse_num = 0;
	while(num){
		reverse_num*=10;
		reverse_num+=(num%10);
		num/=10;
	}
	return reverse_num;
}

 

4.5.

Write a program that tosses a coin 100 times, and count the number of times each side of the coin appears

For each toss of the coin the program should print Heads or Tails

The program should call a separate function ‘flip’ that takes no arguments and returns 0 for tails and 1 for heads 

Use srand and time statement for randomization

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int rand_coin();

int main()
{
	int i, coin, count_head = 0, count_tail = 0;

	srand(time(NULL));

	for (i = 1;i <= 100;i++) {
		coin = rand_coin();
		printf("%s ", coin ? "Heads" : "Tails");
		if (i % 10 == 0)
			printf("\n");
		if (coin == 0)
			count_tail++;
		else
			count_head++;
	}
	printf("\nThe total number of Heads was %d\n", count_head);
	printf("The total number of Tails was %d", count_tail);

	return 0;
}

int rand_coin()
{
	return rand() % 2;
}

 

4.6.

Write a program to solve the “Greatest common divisor” problem

Ex) gcd(200,180) = gcd(180,200-180) = gcd(180,20) = gcd(20,180-20*9) = gcd(20,0) = 20

Input two integers

Use a recursive function “gcd” with two parameters( ▪ The larger of the two integers ▪ The smaller of the two integers)

#include<stdio.h>

int gcd(int n1,int n2);

int main()
{
	int n1, n2;
	printf("Input gcd number : ");
	scanf("%d %d",&n1,&n2);
	printf("gcd : %d\n",gcd(n1,n2));
	
	return 0;
}

int gcd(n1, n2)
{
	if(n2==0)
		return n1;
	if(n1>n2)
		return gcd(n2,n1%n2);
	else
		return gcd(n1,n2%n1);
}
반응형

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

6. Pointer[C Basic]  (0) 2021.11.07
5. Array and Functions[C Basic]  (0) 2021.11.07
3. Loop[C Basic]  (0) 2021.11.07
2. Loop[C Basic]  (0) 2021.11.06
1. Input and Output[C Basic]  (0) 2021.11.06

댓글