Educoder sequential structure programming

Off 1: addition operation

Task description

This off task: write an addition program, input integers a and B, and output their sum.

Relevant knowledge (omitted)

Programming requirements

Please supplement the code between begin end, write an addition program, input integers a and B, and output their sum.

To avoid format errors, please directly copy and paste the format string and prompt information given in the title into your program.

  • Input format: scanf ('% D,% d', & A, & B);

  • Output format: printf("%d+%d=%d\n",a,b,c);

Test description

The platform will test the code you write. If it is the same as the expected output, it will be considered as customs clearance. Sample input: 3,4 sample output: 3 + 4 = 7

Start your mission. I wish you success!

#include<stdio.h>
	int main(void)
	{  
	  int a,b,c; 
      //Please input a,b:
	  /*********Begin*********/
	  scanf("%d,%d,",&a,&b);
	  c=a+b;
      printf("%d+%d=%d\n",a,b,c);
	  /*********End**********/ 
       return 0;
	}

 

Level 2: realize the exchange of two numbers without using the third variable

Task description

This task: the following program is to realize the operation of exchanging two numbers without the third variable. The program code is as follows: 1 #include < stdio h> 2 int main(void) 3 { 4 int a,b;printf(“Enter a and b:”); 5 scanf(“%d%d”,&a,&b); 6 printf(“a=%d b=%d\n”,a,b); 7 a= ① ;b= ② ;a= ③ ; 8 printf(“a=%d b=%d\n”,a,b); 9 return 0; 10 }

Relevant knowledge (omitted)

Programming requirements

According to the prompt, supplement the code at the beginning end of the editor on the right, and complete the related tasks as required.

Test description

The platform will test the code you write. If it is the same as the expected output, it will be cleared. Input: 3 4 Output: a=3 b=4 a=4 b=3

Input: 7 85 output: a=7 b=85 a=85 b=7

Start your mission. I wish you success!

 

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

Off 3: defining constants with macros

Task description

Task of this level: it is known that the unit price of an item is 30 and the quantity is x. Ask for the total price of the goods. Define the unit price of an item with a macro.

Relevant knowledge (omitted)

Programming requirements

According to the prompt, supplement the code in the right editor begin end and define the unit price of the item with a macro.

  • Input: an integer x, representing the quantity of items.

  • Output: output the total price.

Test description

The platform will test the code you write. If it is the same as the expected output, it will be cleared.

Sample input: 5 sample output: 150

Start your mission. I wish you success!

 

#include<stdio.h>
#define price 30
	int main(void)
	{  
	  /*********Begin*********/
      int sum;
      int x;
      scanf("%d",&x);
      sum=price*x;
      printf("%d",sum);
	  
	  /*********End**********/ 
       return 0;
	}

Off 4: Digital separation

Task description

Enter a three digit number and find the values of each digit, ten digit and hundred digit of x.

Relevant knowledge (omitted)

Programming requirements

According to the prompt, supplement the code at the beginning end of the editor on the right, enter a three digit number, and calculate the values of each digit, ten digit and hundred digit of x respectively.

  • Input: a three digit number

  • Output: output the hundreds, tens and bits of the number. The numbers are separated by a space.

Test description

The platform will test the code you write. If it is the same as the expected output, it will be cleared.

Sample input: 123

Sample output: 1 2 3

Start your mission. I wish you success!

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  int x;
      scanf("%d",&x);
      int a,b,c;
      a=x/100;
	  b=x/10-a*10;
      c=x%10;
      printf("%d %d %d",a,b,c);
	  /*********End**********/ 
       return 0;
	}

Level 5: calculate the total score and average score

Task description

This pass task: programming to input a student's five grades from the keyboard and calculate the student's total score and average score.

Relevant knowledge (omitted)

Programming requirements

According to the prompt, supplement the code at the beginning end of the editor on the right, and enter the five grades of a student from the keyboard to calculate the total score and average score of the student.

  • Input: five integers

  • Output: total score and average score, in which the average score retains two decimal places.

Test description

The platform will test the code you write. If it is the same as the expected output, it will be cleared.

Sample input: 60 70 80 90 100

Sample output 400 80.00

Start your mission. I wish you success!

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  int a,b,c,d,e;
      scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
      float average;
      float sum;
      sum=a+b+c+d+e;
      average=sum/5;
	  printf("%.0f %.2f",sum,average);
	  /*********End**********/ 
       return 0;
	}

Level 6: find the area of the triangle

Task description

This related task: program to calculate the area of triangles with side lengths of a, b and c.

Relevant knowledge

The calculation formula of triangle area is: assuming that the three sides of the triangle are a, b and c respectively, where s = (a+b+c) / 2, the area is:

Programming requirements

According to the prompt, supplement the code at the beginning end of the editor on the right, and program to calculate the area of the triangle with a, b and c as the side length.

  • Input: three sides of a b c triangle, which can be decimal;

  • Output: triangle area with 3 decimal places.

Test description

The platform will test the code you write. If it is the same as the expected output, it will be cleared.

Sample input: 3 4 5

Sample output: 6.000

Start your mission. I wish you success!

 

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  float a,b,c,s,area;
      scanf("%f%f%f",&a,&b,&c);
      s=(a+b+c)/2;
      area=sqrt(s*(s-a)*(s-b)*(s-c));
      printf("%.3f",area);
	  
	  /*********End**********/ 
       return 0;
	}

Level 7: solid geometry calculation problem

Task description

This task: set the circle radius r, the cylinder height h, calculate the circle perimeter C1, the sphere surface area Sb with radius r, the circle radius r, and the cylinder volume Vb with cylinder height H. Input data with scanf and output calculation results. Take two digits after the decimal point when outputting. Please program. PI=3.14

Relevant knowledge (omitted)

Programming requirements

  • Input: two double floating point numbers, r and h;

  • Output: circumferential length C1, spherical surface area Sb, cylindrical volume Vb. Keep two decimal places and wrap each result.

Test description

The platform will test the code you write. If it is the same as the expected output, it will be cleared.

Sample input: 1.5,3

Example output: C1=9.42 Sb=28.26 Vb=21.20

Tips

The formula for calculating the surface area of a sphere is s=4 π r2, and r is the radius of the sphere.

The calculation formula of cylinder volume is: V = π r2h, R is the radius and H is the height.

Start your mission. I wish you success!

#include<stdio.h>
	int main(void)
	{  
	  /*********Begin*********/
	  double r,h;
      scanf("%lf,%lf",&r,&h);
      double C1,Sb,Vb;
      double PI=3.14;
      C1=2*PI*r;
      Sb=4*PI*r*r;
      Vb=PI*r*r*h;
	  printf("C1=%.2lf\n",C1);
      printf("Sb=%.2lf\n",Sb);
      printf("Vb=%.2lf\n",Vb);
	  /*********End**********/ 
       return 0;
	}

Off 8: calculate the maximum common divisor of two positive integers

Task description

Programming to calculate the maximum common divisor of two positive integers. The function prototype for finding the maximum common divisor has been given. Please call the function programmatically in the main function to output the maximum common divisor.

Example of program operation: 12,3 ↙ 3 ###### function prototype description the function prototype for finding the maximum common divisor is as follows: int maxcommonfactor (int a, int b); Return value: returns the maximum common divisor; If any of the entered data does not meet the conditions, the return value is - 1. Parameters: A and B are two integer numbers

Relevant knowledge

This task mainly investigates the calling methods of functions#### Programming requirements: according to the prompt, supplement the code at the beginning end of the editor on the right, and program to calculate the maximum common divisor of two positive integers.

  • Input: input format: '% d,%d'
  • Output: output format: '% d\n'

Test description

The platform will test the code you write. If it is the same as the expected output, it will be cleared. Sample input: 467465 sample output: 1

Start your mission. I wish you success!

 

 

#include<stdio.h>
int MaxCommonFactor( int a, int b)
{ 
   int c; 
   if(a<=0||b<=0) 
      return -1; 
   while(b!=0)
   { 
     c=a%b; 
     a=b;
     b=c;
   } 
  return a; 
}   
int main(void)
	{  
	  /*********Begin*********/
	  int a,b,output;
      scanf("%d,%d",&a,&b);
      output=MaxCommonFactor(a,b);
      printf("%d\n",output);
	  
	  /*********End**********/ 
    return 0;
}

Keywords: C

Added by itguysam on Fri, 07 Jan 2022 09:53:51 +0200