c Language Foundation

Keywords: C

Catalog

1 Calculate the value of the symbolic function

2 Addition, subtraction, multiplication and division to get started with arithmetic

3Who is the winner  

4 Calculating days

5 Score Conversion

1 Calculate the value of the symbolic function

For any integer n, the sign(n) function is defined as follows:

Write a program to calculate the value of this function for any input integer.

Input format:

The input gives an integer n in one line.

Output format:

Output the function value corresponding to the integer n in one line according to the format "sign(n) =function value".

Input Sample 1:

10

No empty line at end

Output Sample 1:

sign(10) = 1

No empty line at end

Input Sample 2:

0

Output Sample 2:

sign(0) = 0

Input Sample 3:

-98

Output Sample 3:

sign(-98) = -1

  Answer:

#include<stdio.h>
int main()
{
int x=0;
int n=0;
scanf("%d",&x);
if(x<0)
{
n=-1;
}
else if(x==0)
{
n=0;
}
else
{
n=1;
}
printf("sign(%d) = %d",x,n);
return 0;
}

2 Addition, subtraction, multiplication and division to get started with arithmetic

For the two integers entered, the sum product is output as required.

Input format:

Enter two non-negative integers a and b, no more than 100, in one line, separated by a space and guaranteed that B is not zero.

Output format:

There are four lines in this format:

[a] + [b] = [a+b]
[a] - [b] = [a-b]
[a] * [b] = [a*b]
[a] / [b] = [a/b]

Where bracketed contents (e.g. [a], [b], [a+b], etc.) represent the values of the corresponding integers or operations, and the actual values are substituted for the output.

And: if a can be integer divided by b, a/b should be output in integer format, otherwise a/b should be output in format with two decimals.

Tip: Note the spaces in the expression.

Input Sample 1:

6 3

No empty line at end

Output Sample 1:

6 + 3 = 9
6 - 3 = 3
6 * 3 = 18
6 / 3 = 2

No empty line at end

Input Sample 2:

8 6

Output Sample 2:

8 + 6 = 14
8 - 6 = 2
8 * 6 = 48
8 / 6 = 1.33

Answer:

#include<stdio.h>
int main()
{
	int a, b;
	float c;
	scanf("%d %d", &a, &b);
	c = a + b;
	printf("%d + %d = %.0f\n", a, b, c);
	c = a - b;
	printf("%d - %d = %.0f\n", a, b, c);
	c = a * b;
	printf("%d * %d = %.0f\n", a, b, c);
	c = a / b;
	if (a%b!=0)
	{
		printf("%d / %d = %0.2f", a, b, c);
	}
	else
		printf("%d / %d = %0.0f", a, b, c);
}

 

3Who is the winner  

A TV station's entertainment program has a performance review session, in which two artists are scheduled to perform at a time. Their success or failure is determined by the voting of the audience and the voting of three judges. The rule is: if an artist has a high audience count and is approved by at least one jury, the artist wins; Or an artist who has a low audience count and is approved by all the judges can win. The program guarantees an odd number of voters, so there is no tie. This topic asks you to use the program to determine who wins.

Input format:

Enter the first line and give two positive integers, Pa and Pb, no more than 1000, for the number of audience votes for Artist A and Artist b, respectively. The Title guarantees that the two numbers are not equal. The second line then gives the voting results of the three judges. The number 0 votes for a and the number 1 votes for b, separated by a space.

Output format:

Output winners in the following format:

The winner is x: P1 + P2

among   x   Is the letter representing the winner, P1   Is the number of audience votes won by the winner, P2   Is the number of jury votes that the winner gets.

Input sample:

327 129
1 0 1

No empty line at end

Output sample:

The winner is a: 327 + 1

No empty line at end

Answer:

#include<stdio.h>
int main()
{
int pa,pb;
int a,b,c,d,e;
scanf("%d %d\n",&pa,&pb);
scanf("%d %d %d",&a,&b,&c);
d=a+b+c;
e=3-d;
if(pa>pb&&e!=0)
{
printf("The winner is a: %d + %d",pa,e);
}
else if(pa<pb&&d!=0)
{
printf("The winner is b: %d + %d",pb,d);
}
else if(pa<pb&&e==3)
printf("The winner is a: %d + %d",pa,e);
else
printf("The winner is b: %d + %d",pb,d);

return 0;
}

4 Calculating days

This topic requires a program to calculate the day of a month in a year as the day of the year.

Input format:

Enter the date given in one line in the format "yyyy/mm/dd" (that is, "year/month/day"). Note: A leap year is a year that can be divided by four but not by 100 or by 400. Leap year has 29 days in February.

Output format:

The output date on one line is the day of the year.

Input Sample 1:

2009/03/02

No empty line at end

Output Sample 1:

61

No empty line at end

Input Sample 2:

2000/03/02

Output Sample 2:

62

Answer:  

#include<stdio.h>
int main()
{
	int arr1[] = { 31,29,31,30,31,30,31,31,30,31,30,31 };//Leap year
	int a, b, c, d;
	scanf_s("%d/%d/%d", &a, &b, &c);
	int i = 0;
	d = c;
	for (i = 0; i < b - 1; i++)
	{
		d = d + arr1[i];
	}
	if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0))//Determine whether it is a leap year
	{
		printf("%d", d);
	}
	else
	{
		d = d - 1;
		printf("%d", d);
	}
}

5 Score Conversion

This topic requires a program to convert a percentage to a five-point score. Conversion rules:

  • Greater than or equal to 90 is divided into A;
  • B is less than 90 and greater than or equal to 80;
  • C is less than 80 and greater than or equal to 70.
  • D is less than 70 and greater than or equal to 60;
  • Less than 60 is E.

Input format:

Enter a percentage result that gives an integer in one line.

Output format:

Output the corresponding quintile score on one line.

Input sample:

90

No empty line at end

Output sample:

A

No empty line at end

Answer:

#include<stdio.h>
int main()
{
int a=0;
scanf("%d",&a);
 if(a<60)
printf("E");
else if(a>=60&&a<70)
printf("D");
else if(a>=70&&a<80)
printf("C");
else if(a>=80&&a<90)
printf("B");
else if(a>=90)
printf("A");
    return 0;
}

Posted by jaysmyhero on Mon, 18 Oct 2021 09:34:15 -0700