這些都是基本的 C 程序,可以幫助剛踏入 C 編程世界的新手??靵碓囋嚢桑?/p>
1、C 語言編程 – Hello World
int main()
{
printf("Hello world ");
printf("Linux迷 www.linuxmi.com");
return 0;
}
2. C 語言編程 - 執(zhí)行算術(shù)運算
#include
int main()
{
int a,b;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
printf("Sum=%d difference=%d product=%d quotient=%d ",a+b,a-b,a*b,a/b);
return 0;
}
3.C 編程 - 求圓的面積
int main()
{
float r;
float N=3.14;
float s;
//N*r*r;該行必須在輸入r值以后
printf("請輸入該圓的半徑:");
scanf("%f",&r);
s=N*r*r;
printf("%.7f ",s);//輸出小數(shù)點后7位
return 0;
}
4.C編程 - 在3個數(shù)字中找到最大值
int main()
{
int a, b, c;
printf(" Enter value of a, b & c : ");
scanf("%d %d %d", &a, &b, &c);
if ((a > b) && (a > c))
printf(" a is greatest ");
if ((b > c) && (b > a))
printf(" b is greatest ");
if ((c > a) && (c > b))
printf(" c is greatest ");
return 0;
}
5.C編程 - 查找偶數(shù)或奇數(shù)
int main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n%2==0)
{
printf("Number is even ");
}
else
{
printf("Number is odd ");
}
return 0;
}
6. C編程 - 顯示一個數(shù)字的因數(shù)
int main()
{
int n,i;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factors of %d are: ", n);
for(i=1;i<=n;++i)
{
if(n%i==0)
printf("%d ",i);
}
return 0;
}
7. C編程 - 檢查質(zhì)數(shù)
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i<=n/2; ++i)
{
// condition for nonprime number
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number. ",n);
else
printf("%d is not a prime number. ",n);
return 0;
}
8.C編程 - 檢查閏年
#include
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year. ", year);
else
printf("%d is not a leap year. ", year);
}
else
printf("%d is a leap year. ", year );
}
else
printf("%d is not a leap year. ", year);
return 0;
}
9.C編程 - 從1加到n的和
int main()
{
int i,n,sum=0;
printf("Upto how many terms you want to find the sum:");
scanf("%d",&n);
for(i=1;i<=n;i++){
sum = sum + i;
}
printf("Sum is %d ",sum);
return 0;
}
10. C編程 - 一個數(shù)的階乘
int main()
{
int n, i;
unsigned long long factorial = 1;
printf("Enter an integer: ");
scanf("%d",&n);
// show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu ", n, factorial);
}
return 0;
}
這些 C 編程示例,可以很好的幫助初學(xué)者進行編碼之旅。
寫在最后:另外,對于準備學(xué)習(xí)C/C++編程的小伙伴,如果你想更好的提升你的編程核心能力(內(nèi)功)不妨從現(xiàn)在開始!
整理分享(多年學(xué)習(xí)的源碼、項目實戰(zhàn)視頻、項目筆記,基礎(chǔ)入門教程)
歡迎轉(zhuǎn)行和學(xué)習(xí)編程的伙伴,利用更多的資料學(xué)習(xí)成長比自己琢磨更快哦!
原文標題:C語言實戰(zhàn)例題:小白必會的 10 個C語言經(jīng)典練習(xí)題!源碼分享
文章出處:【微信公眾號:C語言編程學(xué)習(xí)基地】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
-
C語言
+關(guān)注
關(guān)注
180文章
7605瀏覽量
136930 -
編程
+關(guān)注
關(guān)注
88文章
3616瀏覽量
93763 -
C程序
+關(guān)注
關(guān)注
4文章
254瀏覽量
36039
原文標題:C語言實戰(zhàn)例題:小白必會的 10 個C語言經(jīng)典練習(xí)題!源碼分享
文章出處:【微信號:cyuyanxuexi,微信公眾號:C語言編程學(xué)習(xí)基地】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論