C语言

C语言基础算法案例

时间:2025-05-10 22:38:54 C语言 我要投稿
  • 相关推荐

C语言基础算法案例(精选)

  C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。下面整理了一些C语言基础算法案例,希望对大家有所帮助!

  1、C语言打印一条语句

  源代码:

  /* C Program to print a sentence. */

  #include

  int main()

  {

  printf("C Programming"); /* printf() prints the content inside quotation */

  return 0;

  }

  输出:

  C Programming

  2、C语言打印用户输入的一个整数

  #include

  int main()

  {

  int num;

  printf("Enter a integer: ");

  scanf("%d",&num); /* Storing a integer entered by user in variable num */

  printf("You entered: %d",num);

  return 0;

  }

  输出:

  Enter a integer: 25

  You entered: 25

  3、C语言实现两个整数相加

  /*C programming source code to add and display the sum of two integers entered by user */

  #include

  int main( )

  {

  int num1, num2, sum;

  printf("Enter two integers: ");

  scanf("%d %d",&num1,&num2); /* Stores the two integer entered by user in variable num1 and num2 */

  sum=num1+num2; /* Performs addition and stores it in variable sum */

  printf("Sum: %d",sum); /* Displays sum */

  return 0;

  }

  输出:

  Enter two integers: 12

  11

  Sum: 23

  4、C语言实现两个小数相乘

  /*C program to multiply and display the product of two floating point numbers entered by user. */

  #include

  int main( )

  {

  float num1, num2, product;

  printf("Enter two numbers: ");

  scanf("%f %f",&num1,&num2); /* Stores the two floating point numbers entered by user in variable num1 and num2 respectively */

  product = num1*num2; /* Performs multiplication and stores it */

  printf("Product: %f",product);

  return 0;

  }

  输出:

  Enter two numbers: 2.4

  1.1

  Product: 2.640000

  5、C语言查找字符的ASCII值

  /* Source code to find ASCII value of a character entered by user */

  #include

  int main(){

  char c;

  printf("Enter a character: ");

  scanf("%c",&c); /* Takes a character from user */

  printf("ASCII value of %c = %d",c,c);

  return 0;

  }

  输出:

  Enter a character: G

  ASCII value of G = 71

  6、C语言根据用户输入的整数做商和余数

  /* C Program to compute remainder and quotient */

  #include

  int main(){

  int dividend, divisor, quotient, remainder;

  printf("Enter dividend: ");

  scanf("%d",÷nd);

  printf("Enter divisor: ");

  scanf("%d",&divisor);

  quotient=dividend/divisor; /* Computes quotient */

  remainder=dividend%divisor; /* Computes remainder */

  printf("Quotient = %d ",quotient);

  printf("Remainder = %d",remainder);

  return 0;

  }

  输出:

  Enter dividend: 25

  Enter divisor: 4

  Quotient = 6

  Remainder = 1

  7、C语言获取整型、单精度浮点型、双精度浮点型和字符型的长度

  基本语法:

  /* This program computes the size of variable using sizeof operator.*/

  #include

  int main(){

  int a;

  float b;

  double c;

  char d;

  printf("Size of int: %d bytes ",sizeof(a));

  printf("Size of float: %d bytes ",sizeof(b));

  printf("Size of double: %d bytes ",sizeof(c));

  printf("Size of char: %d byte ",sizeof(d));

  return 0;

  }

  输出:

  Size of int: 4 bytes

  Size of float: 4 bytes

  Size of double: 8 bytes

  Size of char: 1 byte

  8、C语言获取关键字long的长度范围

  #include

  int main(){

  int a;

  long int b; /* int is optional. */

  long long int c; /* int is optional. */

  printf("Size of int = %d bytes ",sizeof(a));

  printf("Size of long int = %ld bytes ",sizeof(b));

  printf("Size of long long int = %ld bytes",sizeof(c));

  return 0;

  }

  输出:

  Size of int = 4 bytes

  Size of long int = 4 bytes

  Size of long long int = 8 bytes

  9、C语言交换数值

  #include

  int main(){

  float a, b, temp;

  printf("Enter value of a: ");

  scanf("%f",&a);

  printf("Enter value of b: ");

  scanf("%f",&b);

  temp = a; /* Value of a is stored in variable temp */

  a = b; /* Value of b is stored in variable a */

  b = temp; /* Value of temp(which contains initial value of a) is stored in variable b*/

  printf(" After swapping, value of a = %.2f ", a);

  printf("After swapping, value of b = %.2f", b);

  return 0;

  }

  输出:

  Enter value of a: 1.20

  Enter value of b: 2.45

  After swapping, value of a = 2.45

  After swapping, value of b = 1.2

  10、C语言检查数值是奇数还是偶数

  /*C program to check whether a number entered by user is even or odd. */

  #include

  int main(){

  int num;

  printf("Enter an integer you want to check: ");

  scanf("%d",&num);

  if((num%2)==0) /* Checking whether remainder is 0 or not. */

  printf("%d is even.",num);

  else

  printf("%d is odd.",num);

  return 0;

  }

  输出1:

  Enter an integer you want to check: 25

  25 is odd.

  输出2:

  Enter an integer you want to check: 12

  12 is even.

  也可以用条件运算符解决:

  /* C program to check whether an integer is odd or even using conditional operator */

  #include

  int main(){

  int num;

  printf("Enter an integer you want to check: ");

  scanf("%d",&num);

  ((num%2)==0) ? printf("%d is even.",num) : printf("%d is odd.",num);

  return 0;

  }


【C语言基础算法案例】相关文章:

10个经典的C语言面试基础算法及代码10-06

C语言面试的10个经典基础算法及代码09-16

c语言的排序算法07-22

C语言基本算法09-09

C语言面试中10个经典的基础算法及代码10-21

c语言排序的几种算法10-16

C语言奇偶排序算法08-05

最常用的c语言算法07-18

PID算法的C语言实现07-19