【c語言】c程式設計--譚浩強--(第二章)--2

來源:互聯網
上載者:User

標籤:c程式設計

// 有兩個瓶子a和b,分別盛放醋和醬油,要求將它們互換#include <stdio.h>int main(){int a,b,c;printf("請輸入兩個整數:");scanf("%d%d",&a,&b);c = a;a = b;b = c;printf("交換後的結果是:%d %d\n",a,b);return 0;}



// 依次將10個數輸入,要求輸出其中最大的數#include <stdio.h>int main(){int a[10];int i;int temp;printf("請輸入10個數:");for( i = 0; i < 10; i++ ){scanf("%d",&a[i]);}for( i = 0; i < 10; i++ ){if( a[i] > a[i+1] ){temp = a[i];a[i] = a[i+1];a[i+1] = temp;}}printf("其中最大的數是:%d\n",a[9]);return 0;}


// 有3個數a,b,c,要求按大小順序把它們輸出#include <stdio.h>int main(){int a[3] ;int i,j;int temp;printf("請輸入a,b,c三個數: ");for( i = 0; i < 3; i++ ){scanf("%d",&a[i]);}for( i = 0; i < 3; i++ ){for( j = 0; j < (3-i); j++ )if( a[j] > a[j+1] ){temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}printf("這三個數由小到大的排列順序如下:");for( i = 0; i < 3; i++ ){printf("%d ",a[i]);}printf("\n");return 0;}


// 求 1+2+3+4+...+100#include <stdio.h>int main(){int i;int sum = 0;for( i = 1; i <= 100; i++ ){sum = sum + i;}printf("1+2+3+4+...+100的和是:%d\n",sum);return 0;}


// 判斷一個數n能否同時被3和5整除#include <stdio.h>int main(){int n;printf("請輸入一個整數:");scanf("%d",&n);if( n % 3 == 0 && n % 5 == 0 )printf("這個數可以同時被3和5整除\n");elseprintf("這個數不可以同時被3和5整除\n");return 0;}



// 將100~200之間的素數輸出#include <stdio.h>#include <math.h>int main(){int i,j;printf("100~200之間的素數如下:\n");for(i = 101; i <= 199; i++ ){for( j = 2; j <= sqrt(i); j++ ){if( i % j == 0 )break;}if( sqrt(i) < j )printf("%d\t",i);}printf("\n");return 0;}


// 求兩個數m和n的最大公約數(輾轉相除法)#include <stdio.h>int yue( int x, int y ){int temp;int tem;// 保證分母不為0if( y == 0 ){x = temp;temp = y;y = x;}// 輾轉相除法while( tem ){tem = x % y;x = y;y = tem;}return x;}int main(){int a,b;printf("請輸入要求的兩個數:");scanf("%d%d",&a,&b);printf("最大公約數是:%d\n",yue( a,b ));return 0;}


// 求方程式 ax^2+bx+c=0 的根,分別考慮:1、有兩個不等的實根 2、有兩個相等的實根#include <stdio.h>#include <math.h>int main(){int d;int a,b,c;double x,y;double f;printf("請輸入a,b,c:");scanf("%d%d%d",&a,&b,&c);d = b*b-4*a*c;f = sqrt(d);if( d > 0){x = ( ( -1 ) * b + f ) / ( 2 * a );y =( ( -1 ) * b - f ) / ( 2 * a ); printf("有兩個實根:x = %f  y = %f\n",x,y);}else if( d == 0 ){x = ( ( -1 ) * b ) / ( 2 * a );printf("此方程只有一個實根:x = y = %f\n",x);}elseprintf("此方程沒有實根\n");return 0;}




【c語言】c程式設計--譚浩強--(第二章)--2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.