# M2021 spring C, Java introductory exercise paragraph I - variables, expressions, branches, loops 85-91

7-85 temperature conversion (5 points)
This problem requires writing a program to calculate the Celsius temperature corresponding to the Fahrenheit temperature of 150 ° F. Calculation formula: C=5 × (f − 32) / 9, where: C represents Celsius temperature, f represents Fahrenheit temperature, and the output data shall be integer.

Input format:
This topic is not entered.

Output format:
Output in the following format

fahr = 150, celsius = integer value of calculated temperature in Celsius

```#include<stdio.h>
int main()
{
printf("fahr = 150, celsius = %d",5*(150-32)/9);
return 0;
}
```

7-86 integer four arithmetic operations (10 points)
This problem requires writing a program to calculate the sum, difference, product and quotient of two positive integers and output them. The problem is to ensure that the input and output are all within the integer range.

Input format:
Input gives two positive integers A and B in one line.

Output format:
In the 4 lines, the sum, difference, product and quotient are output in the order of format "A operator B = result".

Input example:
3 2
No blank lines at the end
Output example:
3 + 2 = 5
3 - 2 = 1
3 * 2 = 6
3 / 2 = 1
No blank lines at the end

```#include<stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d + %d = %d\n",a,b,a+b);
printf("%d - %d = %d\n",a,b,a-b);
printf("%d * %d = %d\n",a,b,a*b);
printf("%d / %d = %d\n",a,b,a/b);
return 0;
}
```

7-87 calculating piecewise function [1] (10 points)
This topic requires to calculate the value of the following piecewise function f(x):

formula

Input format:
The input gives the real number x in one line.

Output format:
Output in the format of "f(x) = result" in one line, where x and result keep one decimal place.

Input example 1:
10
No blank lines at the end
Output example 1:
f(10.0) = 0.1
No blank lines at the end
Input example 2:
0
Output example 2:
f(0.0) = 0.0

```#include<stdio.h>
int main()
{
float x,y;
scanf("%f",&x);
if(x==0)y=0;
else y=1.0/x;
printf("f(%.1f) = %.1f",x,y);
return 0;
}
```

7-88 calculating piecewise function [2] (10 points)
This topic requires to calculate the value of the following piecewise function f(x):

f2-11

Note: you can include math.h in the header file, call sqrt function to find the square root and pow function to find the power.

Input format:
The input gives the real number x in one line.

Output format:
Output in the format of "f(x) = result" in one line, where both X and result retain two decimal places.

Input example 1:
10
No blank lines at the end
Output example 1:
f(10.00) = 3.16
No blank lines at the end
Input example 2:
-0.5
Output example 2:
f(-0.50) = -2.75

```#include<stdio.h>
#include<math.h>
int main()
{
float x,y;
scanf("%f",&x);
if(x>=0)y=sqrt(x);
else y=pow(x+1,2)+2*x+1/x;

printf("f(%.2f) = %.2f",x,y);
return 0;
}
```

7-89 step tariff (15 points)
In order to encourage residents to save electricity, a provincial power company implements the "step price", and the price of residential users who install one meter for one household is divided into two "steps": the price is 0.53 yuan / kWh if the monthly power consumption is less than 50 kWh (including 50 KWH); If the electricity consumption exceeds 50 kwh, the electricity price of the excess part shall be increased by 0.05 yuan / kWh. Please write a program to calculate the electricity bill.

Input format:
Enter the monthly power consumption (in KWH) of a user given in one line.

Output format:
Output the electricity charge (yuan) payable by the user in one line, and keep two decimal places for the result, such as: "cost = electricity charge payable"; If the power consumption is less than 0, "Invalid Value!" is output.

Input example 1:
10
No blank lines at the end
Output example 1:
cost = 5.30
No blank lines at the end
Input example 2:
100
Output example 2:
cost = 55.50

```#include<stdio.h>
int main()
{
float x,y;
scanf("%f",&x);
if(x<0)printf("Invalid Value!");
else if(x<=50)
{
y=0.53*x;
printf("cost = %.2f",y);
}
else
{
y=50*0.53+0.58*(x-50);
printf("cost = %.2f",y);
}
return 0;
}
```

7-90 statistical characters (15 points)
This problem requires the preparation of a program to input 10 characters and count the number of English letters, spaces or carriage returns, numeric characters and other characters.

Input format:
The input is 10 characters. The last carriage return indicates the end of input and is not included.

Output format:
In one line, press

letter = number of English letters, blank = number of spaces or carriage returns, digit = number of numeric characters, other = number of other characters
Format output.

Input example:
aZ &
09 Az
No blank lines at the end
Output example:
letter = 4, blank = 3, digit = 2, other = 1

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

int main()
{
char x;
int i=0,blank=0,digit=0,letter=0,other=0;
for(i=0;i<10;i++)
{
scanf("%c",&x);
if(x==' '||x=='\n'){blank++;}
else if(x<='9'&&x>='0'){digit++;}
else if(x<='z'&&x>='a'){letter++;}
else if(x<='Z'&&x>='A'){letter++;}
else{other++;}
}
printf("letter = %d, blank = %d, digit = %d, other = %d",letter,blank,digit,other);
return 0;
}
```

7-91 output leap year (15 points)
Output all leap years since a certain year in the 21st century. Note: the criterion of leap year is that the year of the year can be divided by 4, but can not be divided by 100, or can be divided by 400.

Input format:
Enter a certain cut-off year for the 21st century in one line.

Output format:
Output all leap year years that meet the conditions line by line, that is, each year accounts for one line. If it is not the year of the 21st century, it will output "Invalid year!". If there are no leap years, "None" is output.

Input example 1:
2048
No blank lines at the end
Output example 1:
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048
No blank lines at the end
Input example 2:
2000
Output example 2:
Invalid year!

```#include<stdio.h>
int main()
{
int y,i;
int flag=0;
scanf("%d",&y);
if(y<=2000||y>2100)
{
printf("Invalid year!");
}
else
{
for(i=2001;i<=y;i++)
{
if(i%4==0&&i%100!=0||i%400==0)
{
printf("%d\n",i);
flag=1;
}
}
if(flag==0)
{
printf("None");
}
}

return 0;
}
```

Tags: C

Posted on Sat, 23 Oct 2021 06:42:18 -0400 by zorgon