Home > Computer Tutorials > Computer Knowledge > Design a function in C language to implement addition, subtraction, multiplication and division operations

Design a function in C language to implement addition, subtraction, multiplication and division operations

王林
Release: 2024-01-07 18:50:14
forward
1450 people have browsed it

Design a function in C language to implement addition, subtraction, multiplication and division operations

# include

double jia(double x, double d)

{

double s;

s = x d;

return s;

}

double jian(double x, double d)

{

double s;

s = x-d;

return s;

}

double cheng(double x, double d)

{

double s;

s = x*d;

return s;

}

double chu(double x, double d)

{

double s;

s = x/d;

return s;

}

int main(void)

{

double i, j, k;

char t;

k = 0;

printf ("Please enter what operation you want to calculate" ""-""*""/"\n");

scanf("%c", &t);

if(t == ' ')

{

printf("Please enter the two numbers to be added\n");

scanf("%lf %lf", &i, &j);

k = jia(i, j);

printf ("The sum of these two numbers is %6.2lf\n", k);

}

else if(t == '-')

{

printf("Please enter the two numbers to be subtracted\n");

scanf("%lf %lf", &i, &j);

k = jian(i, j);

printf ("The subtraction of these two numbers is %6.2lf\n", k);

}

else if(t == '*')

{

printf("Please enter the two numbers to be compared\n");

scanf("%lf %lf", &i, &j);

k = cheng(i, j);

printf ("The phase of these two numbers is %6.2lf\n", k);

}

else if(t == '/')

{

printf("Please enter the two numbers you want to compare\n");

scanf("%lf %lf", &i, &j);

k = chu(i, j);

printf ("These two numbers are %6.2lf\n", k);

}

else

{

printf ("Sorry, currently only supports """-""*""/"\n");

}

return 0;

}

How to write a C language program to let the computer automatically perform four arithmetic calculations

#include

#include

#include

#include

int main()

{

int type;

int left, right;

float result;

srand(unsigned(time(NULL)));

while(1)

{

type = rand() % 4;

left = rand() % 10;

right = rand() % 10;

switch(type)

{

case 0:

printf("%d %d = ?\n", left, right);

scanf("%f", &result);

if(left right == result)

printf("right!\n");

else

printf("wrong!The result is %d\n", left right);

break;

case 1:

printf("%d - %d = ?\n", left, right);

scanf("%f", &result);

if(left - right == result)

printf("right!\n");

else

printf("wrong!The result is %d\n", left - right);

break;

case 2:

printf("%d * %d = ?\n", left, right);

scanf("%f", &result);

if(left * right == result)

printf("right!\n");

Use the basic library classes of java to implement a program that calculates four arithmetic operations

package Ex1; //Ex1 is the package name

import java.util.*;

public class SzYs { //SzYs is the class name

public static void main(String[] args){

double num1;

double num2;

String fuHao;

Scanner input = new Scanner(System.in);

System.out.print("Enter the value of the first number:");

num1 = input.nextDouble();

System.out.print("Enter the value of the second number:");

num2 = input.nextDouble();

System.out.print("Enter operation symbol (- * \\)");

fuHao = input.next();

if(fuHao.equals("")){

System.out.print(The sum of num1 " and " num2 " is " (num1 num2));

}else if(fuHao.equals("-")){

System.out.print(The difference between num1 " and " num2 " is " (num1-num2));

}else if(fuHao.equals("8")){

System.out.print(The product of num1 " and " num2 " is " (num1*num2));

}else if(fuHao.equals("\\")){

if(num2==0){

System.out.print("The divisor cannot be 0");

}else{

System.out.print(The quotient of num1 " and " num2 " is " (num1/num2));

}

}else{

System.out.print("The symbol cannot be recognized");

}

}

}

The above is the detailed content of Design a function in C language to implement addition, subtraction, multiplication and division operations. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template