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語言中指針 C語言指針的基礎使用
為什麼説指針是C語言的精髓?本文主要介紹了對C語言中指針的理解與其基礎使用實例,有需要的朋友可以參考一下!想了解更多相關信息請持續關注我們應屆畢業生考試網!C語言的指針,關鍵意思在於“指”。“指”是什麼意思?其實完全可以理解為指示的意思。比如,有一個物體,...
-
2017年計算機二級C語言對C語言的深入探討知識點
引導語:它的應用範圍廣泛,具備很強的數據處理能力。以下是本站小編分享給大家的2017年計算機二級C語言對C語言的深入探討知識點,歡迎閲讀!【考點1】編譯預處理凡以#開頭的這一行,都是編譯預處理命令行,編譯預處理不加分號,不佔運行時間。宏替換僅是簡單的文本替換,如#...
-
c語言如何控制硬件
你們知道在C語言中如何控制計算機的硬件嗎?下面是應屆畢業生小編帶來的關於c語言如何控制硬件的內容,歡迎閲讀!c語言如何控制硬件?C語言是沒辦法控制硬件的首先,C語言不能夠直接對硬件進行操作。從本質上來説,連彙編語言都不可以。只有機器語言能夠直接操作硬件。...
-
c語言中多個if的用法
c語言中多個if的用法的用法你知道嗎?下面小編就跟你們詳細介紹下c語言中多個if的用法的用法,希望對你們有用。c語言中多個if的用法的用法如下:用if語句可以構成分支結構。它根據給定的條件進行判斷,以決定執行某個分支程序段。C語言的if語句有三種基本形式。語句的...