# M2021 spring C, Java introductory exercise, paragraph I - variables, expressions, branches, loops 127-133

7-127 Sum of the digits (6 points)
Given a none-negative number, print out the sum of its digits.

Input Format:
A none-negative integer number.

Output Format:
The sum of its all digits.

Sample Input:
123
No blank lines at the end
Sample Output:
6
No blank lines at the end

```#include<stdio.h>
int main()
{
int n,d;
int sum=0;
scanf("%d",&n);
while(n>0)
{
d=n%10;
n/=10;
sum+=d;
}
printf("%d",sum);
return 0;
}
```

7-128 minimum prime number greater than m (10 points)
Programming to find the minimum prime greater than m.

Input format:
Enter a positive integer directly

Output format:
Output the result directly without any additional format control.

Input example:
12
No blank lines at the end
Output example:
13
No blank lines at the end

```#include<stdio.h>
int main()
{
int n;
int i,j;
int flag=0;
scanf("%d",&n);
i=n;

do
{
i++;
for(j=2;j<i;j++)
{
if(i%j==0)
{
flag=1;
}
}
}while(flag=0);
printf("%d",i);
return 0;
}
```

7-129 height difference of the best couple (10 points)
Experts found that the height difference of the best couple follows a formula: (the height of the woman) × 1.09 = (man's height). If so, the height difference between you is the most harmonious difference, whether it is holding hands, hugging or kissing.

Now please write a program to calculate the best height of his / her couple for any user.

Input format:
Enter the first line to give a positive integer N (≤ 10), which is the number of users coming to query. Then N lines, each line gives the gender and height of the user who comes to query in the format of "gender and height", where "gender" is "F" for women and "M" for men; "Height" is a real number between [1.0, 3.0].

Output format:
For each query, the best height of the couple is calculated for the user in one row, with 2 decimal places reserved.

Input example:
2
M 1.75
F 1.8
No blank lines at the end

Output example:
1.61
1.96
No blank lines at the end

```#include<stdio.h>
int main()
{
int num,i;
float h;
char s;
scanf("%d",&num);

for(i=1;i<=num;i++)
{
scanf(" %c %f",&s,&h);
if(s=='M')
{
printf("%.2f\n",h/1.09);
}
else
{
printf("%.2f\n",h*1.09);
}

}

return 0;
}
```

7-130 accumulator (10 points)
Please implement an accumulator. Enter n nonnegative integers and output their sum. 1 < n < 1000, and each number is < 10000.

Input format:
The input consists of two lines. The first line: includes an integer n, indicating a total of N numbers. The second line: contains n integers.

Output format:
Output the sum of n numbers.

Input example:
4
3 2 1 4
No blank lines at the end
Output example:
10
No blank lines at the end

```#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);

int x[n];
int i=0,sum=0;
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}

for(i=0;i<n;i++)
{
sum+=x[i];
}
printf("%d",sum);
return 0;
}
```

7-131 isosceles right triangle (10 points)
An isosceles right triangle is a triangle in which one angle is a right angle and two right sides are equal. Here we output the format of isosceles right triangle with right angle side length n as follows: for example, if n=1, output:

n=2, output:
*
**
n=3, output:

**

So, can you implement it by program?

Input format:
Enter a number n to represent the side length of the triangle. 1<n<1000.

Output format:
Output the corresponding isosceles right triangle represented by *.

Input example:
4
No blank lines at the end
Output example:
*
**

No blank lines at the end

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

7-132 draw diamond (10 points)
Diamond is a special parallelogram, which has four equal sides. The title gives the side length n of the diamond and draws the diamond with *. If n=1, output:

n=2, output:

n=3, output:

So, can you implement it by program?

Input format:
Enter the side length n of the diamond, 1 < n < 100.

Output format:
Output the corresponding diamond represented by *.

Input example:
4
No blank lines at the end
Output example: ```#include <stdio.h>

int main() {
int n,m=1,i,j,k;
scanf("%d",&n);
for(i=1;i<=n;i++){      //Upper half
for(j=1;j<=n-i;j++){
printf(" ");
}
for(k=1;k<=m;k++){
printf("*");
}
m+=2;
printf("\n");
}
m-=4;
for(i=n-1;i>0;i--){     //Lower half
for(j=i;j<=n-1;j++){
printf(" ");
}
for(k=1;k<=m;k++){
printf("*");
}
m-=2;
printf("\n");
}
return 0;
}

```

7-133 666 (10 points)
Chinese people like the number 6 very much, because people always say 66 Dashun. Math maniac Li likes to digitize everything, so she also defines the word smooth in order of magnitude. 6 represents level 1 smooth, 66 represents Level 2 smooth, 666 represents Level 3 smooth, and so on. You see, the world of math maniacs is always incomprehensible. Today, Li decided to finish math. Now she sets the top n level, and the smooth sum is sn.

sn=6+66+666 +... + 66... 66(n 6).
Assuming you already know the number n, can you help someone Li find sn?

Input format:
Enter a positive integer n in the range [0,10].

Output format:
Output the value of Sn.

Input example:
2
No blank lines at the end
Output example:
72
No blank lines at the end

```#include<stdio.h>
int main()
{
int n;
int x=6;
int sum=0;
scanf("%d",&n);
for(;n>0;n--)
{
sum+=x;
x=x*10+6;
}

printf("%d",sum);
return 0;
}
```

Tags: C

Posted on Sat, 23 Oct 2021 06:10:16 -0400 by soloslinger