当前位置:网站首页>C language instance_ five

C language instance_ five

2022-07-07 01:25:00 Vicky__ three thousand and twenty-one

1. Addition operation

Write an addition program , Input integer a,b, Output their and .

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

2. Don't use the second 3 A variable , Realize the exchange of two numbers

No third variable , Realize the operation of exchanging two numbers .

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

3. Define constants with macros

It is known that the unit price of an item is 30, The number of x. Find the total price of the goods . Define the unit price of the item with a macro .

#include<stdio.h>
int main(void)
{
      
	  int x,f;
      int p=30;
      scanf("%d",&x);
      f = x*p;
      printf("%d",f);
      return 0;
}

4. Calculate the total score and average score

Programming to enter a student's five grades from the keyboard , Calculate the student's total score and average score .

#include<stdio.h>
int main(void)
{
      
	  int a,b,c,d,e;
      int m;
      double n;
      scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
      m=a+b+c+d+e;
      n=m/5.0;
      printf("%d %.2lf",m,n);
      return 0;
}

5. Find the area of the triangle

Programming requires a、b、c Is the area of a triangle with side length area.

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

6. Calculate the greatest common divisor of two positive integers

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

Running example of program :

12,3
3

#### Function prototype description
The prototype of the function for finding the greatest common divisor is as follows :

int  MaxCommonFactor( int a, int  b);

Return value : Returns the greatest common divisor ; If any of the input data does not meet the conditions , The return value is -1.
Parameters :a,b Are two integer numbers

#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)
	{
      
	  int a,b;
      scanf("%d,%d",&a,&b);
      int x=MaxCommonFactor(a,b);
      printf("%d",x);
    return 0;
}
原网站

版权声明
本文为[Vicky__ three thousand and twenty-one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061739025584.html