21xrx.com
2024-04-20 18:52:43 Saturday
登录
文章检索 我的文章 写文章
C中两个数相加
2021-07-06 20:45:47 深夜i     --     --
C

C语言中两个数的相加是将它们相加并在屏幕上打印它们的和的算术运算。 例如,如果输入为 5 和 6,则输出为 11。

 

C中的加法程序

#include <stdio.h>


int main()
{
  int x, y, z;

  printf("Enter two numbers to add\n");
  scanf("%d%d", &x, &y);

  z = x + y;

  printf("Sum of the numbers = %d\n", z);

  return 0;
}

程序的输出:

下载添加号码程序。

类似地,我们可以编写一个 C 程序来执行两个数的减法、乘法和除法。

另外溢出

在表达式(z = x + y)中,如果总和大于变量z可以存储的最大值,则可能发生整数溢出。

而不是9518406073(1234567891 + 8283838182),结果是928471481,因为溢出了。 一个 4 字节的整数可以容纳的最大值是 2,147,483,647。

实数加法C程序

该程序只能添加整数。 如果您想将数字与小数相加怎么办? 为此,我们需要使用 double 数据类型(您也可以使用 float 或 long double 数据类型)。

#include <stdio.h>


int main()
{
  double a, b, c;
 
  printf("Enter two numbers\n");
  scanf("%lf%lf", &a, &b);

  c = a + b;
  printf("%lf\n", c);
 
  return 0;
}

输出:

Enter two numbers
3.21
7.47
10.680000

默认情况下,%lf 打印六位十进制数字。 要打印最多两位十进制数字,请在 printf 函数中使用 '%.2lf'。
 

#include <stdio.h>


int main()
{
  double a, b, c;
 
  printf("Enter two numbers\n");
  scanf("%lf%lf", &a, &b);

  c = a + b;
 
  printf("%.1lf\n", c);
  printf("%.2lf\n", c);
  printf("%.3lf\n", c);
  printf("%.4lf\n", c);
  printf("%.5lf\n", c);
  printf("%.6lf\n", c);
  printf("%.7lf\n", c);
  printf("%.8lf\n", c);

  return 0;
}

 

C中两个数字的总和

#include<stdio.h>


int main()
{
   int a = 1, b = 2;
   
   /* Storing result of the addition in variable a */
   
   a = a + b;
         
   printf("Sum of a and b = %d\n", a);
       
   return 0;
}

不建议这样做,因为变量 'a' 的原始值丢失了; 如果我们在程序中进一步需要它,那么我们将不会拥有它。

C程序将两个数字重复相加

#include <stdio.h>


int main()
{
  int a, b, c;
  char ch;

  while (1) {
    printf("Input two integers\n");
    scanf("%d%d", &a, &b);
    getchar();

    c = a + b;

    printf("(%d) + (%d) = (%d)\n", a, b, c);

    printf("Do you wish to add more numbers (y/n)\n");
    scanf("%c", &ch);

    if (ch == 'y' || ch == 'Y')
      continue;
    else
      break;
  }

  return 0;
}

程序输出:

Input two integers
2 6
(2) + (6) = (8)
Do you wish to add more numbers (y/n)
y
Input two integers
2 -6
(2) + (-6) = (-4)
Do you wish to add more numbers (y/n)
y
Input two integers
-5 3
(-5) + (3) = (-2)
Do you wish to add more numbers (y/n)
y
Input two integers
-5 -6
(-5) + (-6) = (-11)
Do you wish to add more numbers (y/n)
n

使用函数将两个数相加的C程序

我们可以使用函数计算两个整数的和。

#include<stdio.h>
 
long addition(long, long);
 
int main()
{
   long first, second, sum;
 
   scanf("%ld%ld", &first, &second);
 
   sum = addition(first, second);
 
   printf("%ld\n", sum);
 
   return 0;
}
 
long addition(long a, long b)
{
   long result;
 
   result = a + b;
 
   return result;
}

我们使用 long 数据类型,因为它可以处理大量数字。 要添加不适合内置数据类型的数字,请使用数组、字符串或其他合适的数据结构。

C程序将两个数字相加

将 a 添加到 b(假设 b >= 0)相当于向 a 添加 b 次。 例如,3 + 5 = 3 + 1 + 1 + 1 + 1 + 1(3 加五)。 让我们通过一个程序来实现它。

#include <stdio.h>


int main()
{
  int x, y, sum, c;

  printf("Enter two numbers to add\n");
  scanf("%d%d", &x, &y);
 
  sum = x;

  for (c = 1; c <= y; c++)
        sum = sum + 1;

  printf("%d\n", sum);

  return 0;
}

 

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复