|
算法是一个程序和软件的灵魂,作为一名优秀的程序员,只有对一些基础的算法有着全面的掌握,才会在设计程序和编写代码的过程中显得得心应手。本文是近百个C语言算法系列的第二篇,包括了经典的Fibonacci数列、简易计算器、回文检查、质数检查等算法。也许他们能在你的毕业设计或者面试中派上用场。6 D4 w% \, \' |# u+ Z Q
4 q% G- {" K3 Q
1、计算Fibonacci数列 1 a n# X+ F8 `( e
Fibonacci数列又称斐波那契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21。
1 D2 J- K0 [; K/ b
C语言实现的代码如下:
q0 h7 |1 F6 h5 |7 a# u# [- W) O- /* Displaying Fibonacci sequence up to nth term where n is entered by user. */( x1 S" C# D( }# d) A& a
- #include <stdio.h>. Z/ S, U! [# p1 z8 x1 R8 u
- int main()
$ L) U+ X# G4 j! d2 x4 B. s - {
7 H2 i: q0 O L8 a7 w" M - int count, n, t1=0, t2=1, display=0;
. O# G* a0 P. s& k - printf("Enter number of terms: ");
7 \$ S3 ~; Q+ v5 p5 H+ d - scanf("%d",&n);
! z! q% x7 [) W& |0 Q9 o6 T - printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */4 p: R- W. p* p, }5 t1 W( O
- count=2; /* count=2 because first two terms are already displayed. */- F/ X1 f* h# {) \+ ~3 ~6 v
- while (count<n) : E( a" U: M/ J- Z! B, r* l
- {9 E" V4 T" n% l- D5 M0 N& J. O
- display=t1+t2;
% S2 b. C1 J1 x) t - t1=t2;
9 R/ }6 X- ?* P. r, W - t2=display;
* u% Y& J( F" \ t+ z - ++count;
8 O2 X6 s' S F# j7 {( d - printf("%d+",display);
2 P. h8 v2 y& } - }( V* P. n7 I& X6 n9 H
- return 0;1 E; D8 ?% K0 E5 d: `. @. @$ J% g
- }
复制代码 : w/ I- t, D v6 a f3 O
结果输出:
( `6 C! L, P' T: U' V# n& y- Enter number of terms: 10
# X! T8 `: C) P& O z - Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+
复制代码 0 C: \) a# z$ i- X) k5 x. @- @
也可以使用下面的源代码:
+ n9 P' Z; ~9 q' u3 i- /* Displaying Fibonacci series up to certain number entered by user. */
6 r* }$ W( ]1 I2 S8 R - 1 I/ L, {* @ J- r
- #include <stdio.h>
, x3 f* k" n7 V; ?9 {3 m - int main()
R3 [3 H9 q5 M/ Y7 S - {
) J4 B# o+ o5 V: B* s& |% ]4 h( c - int t1=0, t2=1, display=0, num;8 H U6 R0 |! W% n3 X8 O% H( w
- printf("Enter an integer: ");
c. y [6 t: r" v - scanf("%d",&num);, f4 P" G9 _4 O, j
- printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */
7 E5 M! P7 E2 v+ Y - display=t1+t2;: W- U [4 Z( N0 U9 S9 J7 ^
- while(display<num)9 P1 N( |5 Q# T4 X; W
- {: Q9 Y/ [1 L) i
- printf("%d+",display);
2 Z7 A$ ~; v% ^5 U( n# n - t1=t2;
) A0 M9 c1 S6 I3 A2 O) P - t2=display;
8 e0 u( f! L2 z% I/ K- I - display=t1+t2;$ ]& K7 c+ L* S- j2 ]% a" E/ m
- }
# I" L* k8 M* S; \( @3 q - return 0;9 b }3 C% c/ ]- X
- }
复制代码 , p% j! W4 d7 O3 w1 D
结果输出:
) c; Q: R& i- y, \+ z% G! Z5 g& W- Enter an integer: 200
# L9 h" L* T, Y - Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+55+89+144+
复制代码
. N" K* Q( \2 C: f" h# F, z- b$ p- D" G9 X9 m& S
2、回文检查 + |% C* V! K( e7 f, Y, g* d4 L9 |
源代码:
I7 {/ @) u0 x3 a {! Y# y$ g% d- N& p' z- /* C program to check whether a number is palindrome or not *// v' [ j! \% ~, q
- 8 R! N7 }' N' U$ r% O- D, u7 b
- #include <stdio.h>
" E1 a5 y D" { - int main()+ i6 R* l2 M5 t
- {
5 r5 U5 v& k7 I3 G - int n, reverse=0, rem,temp;- a' a5 z2 Q6 U O) |5 {
- printf("Enter an integer: ");
8 a( K1 n: ~2 _# d1 G' N - scanf("%d", &n);
# K# u' Y F( ?) q2 R+ U; s - temp=n;
1 D2 X+ ?' Y# [, i5 S$ Q - while(temp!=0)) ^# d. h" L$ B/ A h
- {& ]) V7 G& ?/ p2 k0 X# J
- rem=temp%10;( ?, I, V$ \2 u- C
- reverse=reverse*10+rem;9 ^2 j/ t5 X A# ?9 g
- temp/=10;* H! ~. O2 t/ p, ?
- }
2 Z9 m4 I; u, q% k0 M) Y7 Q - /* Checking if number entered by user and it's reverse number is equal. */
9 O7 ?' \2 Y9 E0 `4 M - if(reverse==n) }: V. a" D, v* a! O* ~2 T
- printf("%d is a palindrome.",n);
1 Z, Q! w/ w" |9 D7 |; e - else- g: `# X$ F2 a* j$ l9 g' F! u
- printf("%d is not a palindrome.",n);% _5 }' c: r/ |+ c) r" ?
- return 0;$ C( S) q4 y" ?7 r. [$ {# g1 \ J
- }
复制代码 / D& b( ]. I# `' q1 P9 N8 r. C
结果输出:
9 m" A2 [7 z1 U' i8 A% t- Enter an integer: 12321
* s/ t3 A& O1 r5 ^ - 12321 is a palindrome.
复制代码 ( {) ^7 l) a a
: s: `2 X: p8 d) b' c- e
3、质数检查 / T1 P- D- K& g$ H
注:1既不是质数也不是合数。 ! ^+ w( c/ D! ^3 l8 W
源代码:0 q9 O) n" K/ W; r( w
- ; x( K/ Q- p5 x! ]; f y: p5 r
- /* C program to check whether a number is prime or not. */
8 C* i# k5 {: b' C - " l/ ]4 R# _/ K7 A& ]+ n# J! K
- #include <stdio.h># j0 |1 C6 q7 C) e5 P. I
- int main()# O2 U1 A9 {, @9 m: ?
- {3 }- x, ?$ G7 s
- int n, i, flag=0;
' ~- Y5 ^) [! b2 l @8 z1 C) p - printf("Enter a positive integer: ");6 b- f7 ~& R6 G/ B! C8 p
- scanf("%d",&n);
: t- S; S) V7 n' N. x! x7 j - for(i=2;i<=n/2;++i)
# [% y, I; l9 E3 }; n - {
0 c: p0 k7 `; O) j& A. O& k; O$ k - if(n%i==0)
, p y7 F! n+ \' s9 Y; O9 U6 { - {
3 \# c7 K0 T3 v# y$ e9 D# M - flag=1;& c- w% b1 H g- Q$ b. h. _
- break;
) m# [) j9 e! |2 a$ W - }0 q1 ~3 {: [6 ]; e0 J2 Y' c4 R
- }. a% S' w: m; E' l# Q. m
- if (flag==0)
3 R0 i$ {8 l( E5 _. o1 U - printf("%d is a prime number.",n);9 A# [. f9 f9 O5 \2 _' o; n/ c
- else
+ [% m. p* ~& W+ q7 P1 y% | - printf("%d is not a prime number.",n);; u, q. r: |' M8 h2 K
- return 0;0 W5 |, B5 t( d5 ]
- }
复制代码
" G+ k, ?! |; g8 ?; C* V! t结果输出: - * h& m) U' d) L0 @. d1 m
- Enter a positive integer: 29
. X. P; a& y/ R - 29 is a prime number.
复制代码) o- U+ M Z* C' O' {# E5 I9 J4 Z
4、打印金字塔和三角形 . m( L# O: E5 o$ D4 G5 R
使用 * 建立三角形
) s, T0 E' g7 @' I4 ^- *9 t* \% J. L5 P3 ]& P" j) X: Q
- * *; {3 x3 o$ A3 K$ I
- * * *
$ x( O3 X9 S6 Z7 [. W4 q( Y# r" l: i - * * * *. r5 I3 _! _; O8 I! F6 A8 d3 o
- * * * * *
复制代码
4 l2 S1 E# s0 f$ x8 G源代码:
4 V6 ]/ |( Q" a( B; d% ~- #include <stdio.h>$ t9 h" l. q+ H9 C5 v" e
- int main()
) V+ N7 m# H% i" n" s1 c# W7 y - {
3 o" S2 F+ V( T# Q - int i,j,rows;
- w% B8 n S& z& c6 ?& s3 N - printf("Enter the number of rows: ");, y. u5 H l2 U) v# ^% u6 E
- scanf("%d",&rows);//微信搜索公众号【C语言中文社区】关注回复C语言,免费领取200G学习资料
8 b& L8 o- w+ K) m( S" \! { - for(i=1;i<=rows;++i)# G0 R! n! n! x
- {
- r* D* Z3 R( {0 r5 ` - for(j=1;j<=i;++j)9 Z9 L. t: `: r6 s/ M0 @
- {$ x( c3 q- ?& F8 X# T4 s' u
- printf("* ");
' k( _: ~5 r+ d" O) r8 t4 p - }
2 M/ {+ ^$ _$ F' J% Q& f+ h - printf("\n");% q8 ~% k" Y# M9 j; {
- }# z5 a' n6 M$ ?
- return 0;
" I6 h" x' |. l% L0 l - }
复制代码 " r2 b6 h- C0 N$ v
如下图所示使用数字打印半金字塔。 - 5 h1 @, }1 x8 ^$ m( g X3 i* F* O( U
- 13 X. i" c- s$ a) E
- 1 2* v$ y, g) w4 m1 s/ L% C f
- 1 2 3
2 H8 V3 g. O( T5 f9 a - 1 2 3 42 U5 p- t( Y6 E z' q+ V- G
- 1 2 3 4 5
复制代码 7 \5 c( ?0 I3 q7 Q5 @
源代码: - ( j O; |+ G! E: d9 q9 W1 s
- #include <stdio.h>
& z) r4 }3 w/ V- ^ L - int main()
. ?1 u6 p) T: N4 E; a3 { - {
% ?& e) J5 N5 T! w& z" R( B- l) G - int i,j,rows;
4 Q% j6 p Y1 O+ y - printf("Enter the number of rows: ");4 g/ ?* _& u0 U: H8 n
- scanf("%d",&rows);
7 t, b. o$ l. d# r - for(i=1;i<=rows;++i)+ o+ a7 K Y9 i% y/ u6 m
- {
4 r+ `+ f8 g% W- ~3 [# x# s7 n - for(j=1;j<=i;++j)
3 z! ]2 Q( s3 ? - {
' c: d' e9 \2 S! v+ I U - printf("%d ",j);# T" v5 M" |% _: X* b" y+ f9 y
- }
7 M) V' h1 {* e( F* v - printf("\n");( t0 c. }0 X1 ~$ _9 S1 ^5 W
- }% E9 J! ^2 ^4 H" e
- return 0;
; v# u8 h8 {% H4 M- a, A# _7 \. H' p - }
复制代码 ! K, v1 i* ~; c# T5 {5 j6 y0 F
用 * 打印半金字塔
' z- E _& y% z* {2 g" Q: H- * * * * *! ~: Y* N/ }1 C2 N3 j7 H8 x; K/ x
- * * * *) i! G1 q* T. a2 h) ~
- * * * ! }& G! a+ b) {
- * *
`6 v! [! }$ O - *
复制代码 # ]* ^) s9 c' ?% S7 o
源代码:
8 S' x2 x: q, A- #include <stdio.h>1 g" z8 @+ k E/ d3 }
- int main()
1 u3 K2 _5 T; Q8 j - {( e- _4 {4 M2 C- `
- int i,j,rows;8 n2 |" g$ ?* V5 G8 N4 m
- printf("Enter the number of rows: ");5 ]: f% {# f. ]0 d3 q/ X, y2 D) c& T
- scanf("%d",&rows);4 h" P2 u/ R1 H% i) H u5 u3 a
- for(i=rows;i>=1;--i)
; P5 v! K5 l5 h9 @6 ` - {
5 k0 F R- k( q, R6 c - for(j=1;j<=i;++j)
& s5 s( O/ F* M- l, p9 M9 y - {
2 f" H3 @) e. \2 O$ n+ h; d% w - printf("* ");
& F5 J' f5 B& L! | - }7 V0 J6 E$ m* g F7 h6 ?
- printf("\n");
) J# \. H( ?8 x* e% c1 Q - } ^: [9 }9 }( [" e5 f
- return 0;7 o9 G0 K# I* | h4 |5 h
- }
复制代码 9 ?$ \' L) N* P6 h" [0 P
用 * 打印金字塔 - 9 Z/ e6 j# x. l! w- c5 Y
- *
2 t' H, D+ w4 C: ], F! n; | - * * *4 ~& F" p# C" G! y5 V
- * * * * *$ A) Y, f& S& \
- * * * * * * *0 K0 r( e1 x# q7 [
- * * * * * * * * *
复制代码 N S# }, P( |
源代码: - % Y& b- i* u- N; ^$ g
- #include <stdio.h>
. Q D4 o; }4 \$ Y$ K( ~6 {( | - int main()
- E. d6 L; u$ a4 a6 F - {
6 d& B9 Y8 {6 B2 S - int i,space,rows,k=0;+ Z7 a7 G" E+ ~! x, ~7 x) B
- printf("Enter the number of rows: ");
- s: I% W( {, ]" X/ | - scanf("%d",&rows);2 V T( @. J$ _2 p T! y
- for(i=1;i<=rows;++i)- p5 { J' M6 L9 ~
- {/ l6 F. {: t5 ?- ]0 C. Z
- for(space=1;space<=rows-i;++space)
% Y7 B+ X+ I y, q2 W k; J. z - {
p1 ~$ U1 N$ _7 n, O: d - printf(" ");' D9 G, _8 g+ W: m2 c" u
- }
; C" w- \ Z, e) K8 s' W- R6 |: F - while(k!=2*i-1)5 `4 Q) R; G& |& V1 |5 O! M9 K
- {0 o- N5 Z6 I" H/ h' n7 c& C8 O
- printf("* ");
: s) w; G. ?$ m& x$ N7 ^, y - ++k;
" s) Q7 _. ?( p2 G/ A - }
0 {9 e) Y7 U: H% \! e - k=0;
* j/ |3 w9 B# E! m/ w3 q: _ - printf("\n");
+ g# J. k# N8 I7 P+ G - }8 g* l- p6 u2 }% v C4 u, Y
- return 0;
4 N! ^" A; U, ?+ M* q1 t7 W9 ~ - }
复制代码 2 C7 a6 {6 A3 h3 w
用 * 打印倒金字塔
5 y* K; x& {; L$ f1 @! F/ J9 Y3 g- * * * * * * * * *
7 G1 ^3 o! e" a# F - * * * * * * *; H& ]7 a ^0 ?( a* q* x* C
- * * * * *
3 ]1 t# K6 |& H0 U7 M# v - * * *
- k2 \0 l( I2 d+ B7 K; L6 l - *
复制代码
& Q) C. E& d1 j* y6 w, {源代码: - 6 C: R% V9 Y& a' x3 `8 n4 b; P. K
- #include<stdio.h>
+ v( E/ E/ a. i1 [& J - int main() U( s" P0 K! X8 x9 o7 g
- {
8 @+ @8 ~( @$ c' E3 [ - int rows,i,j,space;2 k4 C$ E) U6 f+ F
- printf("Enter number of rows: ");' W! ~: g2 S2 ?1 W& s4 R
- scanf("%d",&rows);3 W: h$ r7 z4 ?: |9 o
- for(i=rows;i>=1;--i)
7 }. x. ^0 S b! M - {& p) {/ a" d% x
- for(space=0;space<rows-i;++space)
. b9 V7 V' A }% a, c2 L - printf(" ");
1 @/ G! t1 t" g/ z, P7 J - for(j=i;j<=2*i-1;++j)' R% B+ S% h2 o5 H% Y& {/ `" g
- printf("* ");
7 L) [7 k& Y/ m2 Y. h# I. z - for(j=0;j<i-1;++j)! o5 B, R( T3 }! V) U. X
- printf("* ");
) @9 j) `# L+ a" S( v - printf("\n");8 @/ E7 O( j! L# D2 }4 f# x& u1 J
- }
2 c1 G2 [3 l' }) E r - return 0;, T3 P3 H/ q+ k$ n- Y) U/ B
- }
复制代码
1 A9 O* ^& Q5 F" }8 ~
( L0 m' c! R; N" r
5、简单的加减乘除计算器 4 e4 ?" v Z' C! z9 d- m
源代码:
) @, `$ j6 B4 {- /* Source code to create a simple calculator for addition, subtraction, multiplication and division using switch...case statement in C programming. */
7 |9 P- N+ A; z( E9 G3 Z -
' H" D% F) g: L4 Y# o0 O - # include <stdio.h>7 J* \* d6 {; C3 F% v; G
- int main(), }: O6 }# q5 c" \
- {
( @6 t* `7 k1 T- @* i - char o;4 K* x. w6 Z. `- q3 ^
- float num1,num2;
% ?8 m/ f2 m* [: Y+ u! n) o0 q) e - printf("Enter operator either + or - or * or divide : ");
4 M# p7 B0 A; w D! J - scanf("%c",&o);) P# }2 r' P; @2 [
- printf("Enter two operands: ");& r9 \. s" u* w; {
- scanf("%f%f",&num1,&num2);
1 k. k" K V8 ^) f! T - switch(o) {- D6 l0 P, {9 {2 m1 F
- case '+':
! J, Z, m# H! q! v$ b) p; |1 h - printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
% L9 Q/ ~/ j% @# ? G5 m - break;
u: ^1 D8 V5 \8 M& f - case '-':, q. k% E4 H( i) w
- printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);+ c$ K- e2 S6 L! S
- break;
; p1 o$ \$ ]8 ?# u# ]1 J0 x/ s - case '*':1 E5 a5 r, K+ m, s" O
- printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
5 g% g' s2 ~' l' Y# ^( h2 Z7 C# T - break;
( U d' E; s! }& ]- s y7 {& Z - case '/':
! Q' P3 h" I3 H4 b5 N3 |3 k1 I - printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);8 A" ^9 J, q# x9 t
- break;
( B0 h0 `9 b) j7 { - default:
: z3 j* I: }& e0 B4 _- T$ f - /* If operator is other than +, -, * or /, error message is shown */- ^! G% c# n9 Z( s1 V' D h1 L
- printf("Error! operator is not correct");6 F+ W) ? E* V" T, n) |
- break;9 K( U4 B+ E6 W! W' {) A( E
- }
9 U. d, V [/ a& n3 ~ - return 0;
* z8 V/ J" y; F; b+ t7 G3 ? - }
复制代码 ! R1 Y* W3 K' N \, |. ]2 ]
结果输出:
& m. o5 @& x1 Y0 N* U- Enter operator either + or - or * or divide : -
3 e3 d/ I( z: x4 n - Enter two operands: 3.47 A' i7 H! O) e) M4 W
- 8.4# j- Y' ]; l- y6 [, o! N0 J$ w
- 3.4 - 8.4 = -5.0
复制代码 ' q: C0 s6 f$ o. e
' @( ?- t- q7 [" a4 ~. N
6、检查一个数能不能表示成两个质数之和 $ i. S% F5 f1 F9 G% |
源代码:
, ~( k, H0 c, V6 e7 ~- h- #include <stdio.h>0 f# x6 q i Z- C7 W, L
- int prime(int n);
1 A: Z+ K) J+ H o - int main()
6 N/ s( z' s: K% e$ o+ P/ }9 q - {
3 S. a) t; E0 p% ^4 P+ T - int n, i, flag=0;//微信搜索公众号【C语言中文社区】关注回复C语言,免费领取200G学习资料9 @4 h- s. M* G, k7 ^" j' }6 D; H
- printf("Enter a positive integer: ");9 D- y3 H S- R ], p+ l @
- scanf("%d",&n);
" }- \$ h% G$ i. p6 X - for(i=2; i<=n/2; ++i); `# a; f( f0 d2 G
- {
, C2 L2 G' q, k* N" `/ S - if (prime(i)!=0): n* `" d: O+ z8 R* T7 s" z
- {
7 e8 \3 D8 Z8 q( ~ - if ( prime(n-i)!=0)9 ?8 K# i O. D$ Q) P7 Q3 q4 B
- {& F9 i- g' _- c; S
- printf("%d = %d + %d\n", n, i, n-i); Q: F" ?2 r0 F' y# A$ ]
- flag=1;5 ]- Q' t/ N5 e5 q5 Q* j! ]( s1 A8 j
- }
, |+ |9 N+ W0 X2 r. n5 N o+ w& M -
& h2 d8 ^$ ~* K# E# g - }, e, O0 B3 G, \* v4 f/ P
- }
' m y- f$ H; ~4 K3 d3 z( \ - if (flag==0)' g* I# w. P* d
- printf("%d can't be expressed as sum of two prime numbers.",n);/ P5 G3 E. G; E. T1 E
- return 0;
% S& O/ {! ~& Y5 D* W: H3 y7 t - }
2 f2 P8 j& W5 F( \8 S2 x2 u1 H - int prime(int n) /* Function to check prime number */, l9 H% ]2 q' o
- {
* @ |3 v1 {4 |7 |! c - int i, flag=1;% H( [# E( j. ?. {/ l8 Y% Y' f
- for(i=2; i<=n/2; ++i) n( d& {8 b# ^5 K$ g% C
- if(n%i==0)$ B2 ?0 w6 g! l$ V/ W
- flag=0;) O7 D/ L; v8 X4 R' k: s
- return flag;
6 U m- y; |# ^) Z - }
复制代码
0 ~ ~' r7 T: Q2 d4 y0 h结果输出: - 5 r+ X; x& G7 k3 [; T% x
- Enter a positive integer: 34
2 s4 W- _2 m) D: W" i" F- H - 34 = 3 + 31$ C! P0 J+ b, L) ~! x! D8 ]
- 34 = 5 + 29
3 g% }+ U. ^3 K F" P$ ? - 34 = 11 + 23$ z9 M- b* n3 Z$ G: L' R- l
- 34 = 17 + 17
复制代码 ) B- ]4 M* O1 w
3 N& ^9 Z2 |, `' P5 I% _
7、用递归的方式颠倒字符串 * [3 l: V5 v. `% f2 B+ M7 l; M
源代码:
4 C5 u, z. l6 f7 f; M2 q! O+ ~2 I- /* Example to reverse a sentence entered by user without using strings. */
. r! S) C. ^) }+ u9 `4 Q0 V4 ] - ! ^1 F# ]3 E; Y
- #include <stdio.h>6 S% H# K1 d" s4 O% l3 ~8 i6 I
- void Reverse();
$ k. s& x* w) d9 [ - int main()- K8 S; J% e/ y" }: I+ ]
- {
: U7 C, x( j. S4 g! k! ]8 t - printf("Enter a sentence: ");
6 r4 U% t' C# m& T1 p' I - Reverse();5 A/ B' X0 p* H0 I! q3 b
- return 0;
' d* U; r- ]& u" W0 S9 T+ O' E - }
0 y7 x/ {1 f8 { - void Reverse()/ d8 z6 A7 S' N" v0 j/ O: m
- {
! V& p& C9 ~' |0 F& m. D - char c;0 R( b% {1 p) e
- scanf("%c",&c);
! r9 o$ P8 e- G4 ` - if( c != '\n')0 i/ ^4 B* c/ w
- {
) ^8 x( h4 f+ f U - Reverse();
7 U. J! O+ o6 w. F* v% Z# P - printf("%c",c); C8 s; \" Z% v) {7 D
- }9 @4 I8 {/ g8 p7 V: C, z( |& L, z
- }
复制代码 0 v: {5 H8 U& m5 d9 v/ N( a+ s) S
结果输出: - ! N/ ?( C4 w( [/ I8 b
- Enter a sentence: margorp emosewa- a, j' N# { G6 Y) x- F& h
- awesome program
复制代码
S6 I) n5 T% x3 o$ M
0 E9 H& f: D" c4 w: v* J1 c
8、实现二进制与十进制之间的相互转换 - . j" _# o k/ I1 b7 G% O9 D
- /* C programming source code to convert either binary to decimal or decimal to binary according to data entered by user. */
' y6 y$ K8 R3 n -
& s0 e3 s ?$ _5 k9 E: y - #include <stdio.h>" U& W; }1 Q" I8 ^8 e
- #include <math.h>" u- t, N# w! b& E' R
- int binary_decimal(int n);+ A( W5 v) l# A
- int decimal_binary(int n);- D- I. c+ P* ?1 q; t
- int main()8 F: a& K; Y6 F3 S
- {
: O3 _( t) H+ L9 H( `/ E+ j - int n;
) Z& f8 g5 ^! J4 J - char c;" f: {. V& ~- m5 \/ ?7 S" a
- printf("Instructions:\n");7 j$ ^ H& T$ s" `+ }/ ]
- printf("1. Enter alphabet 'd' to convert binary to decimal.\n");1 l5 i+ n7 b, n3 z
- printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
- |. u9 Y# r' M2 L4 B5 i; r - scanf("%c",&c);
3 E9 L1 \$ g. a+ o8 U# A. f - if (c =='d' || c == 'D')0 s& m9 z, v$ A1 ?( E& Z/ w
- {6 ]. R' Q7 E% u% N& y
- printf("Enter a binary number: ");' c4 s+ T! g6 N, w
- scanf("%d", &n);
) Y% h8 P. f5 v4 w) k9 { - printf("%d in binary = %d in decimal", n, binary_decimal(n));
; C% U# i% ~; h - }- \" [! T" f# L; J4 m4 p
- if (c =='b' || c == 'B')
: g4 | M0 a4 i b/ f! t - {
2 e5 E0 k! c& g! Y. E - printf("Enter a decimal number: ");- ]. `# ^+ B) i2 D
- scanf("%d", &n);
0 K3 F/ Y3 ]& ?% i; j - printf("%d in decimal = %d in binary", n, decimal_binary(n));. @% g1 u6 t5 A9 D
- }
|% k z! b9 |+ a6 o: V; j - return 0;8 Q* c" g5 w$ D4 {/ A% o- l5 h
- }
2 p% q$ c+ A8 r4 n' ] -
1 T+ w4 q' F5 p; O& } - int decimal_binary(int n) /* Function to convert decimal to binary.*/
* U3 O: s& L- ?6 R - {5 y4 F* Y6 R5 b8 N& N& T6 G
- int rem, i=1, binary=0;1 l* c& J; j/ f+ \
- while (n!=0)5 ^' B$ P4 b7 g. s# U/ ^/ A
- {) H4 B+ D# k# B' @+ H/ t
- rem=n%2;
" b4 d' o6 N0 Z& z* R - n/=2;$ W$ n5 D4 c3 b8 G
- binary+=rem*i;
^: y& F( f6 V- j9 e" f; T% s: E - i*=10;
% y; A9 E+ }& s6 W h2 K3 F - }
3 Q+ T$ U' G. ]# L+ K - return binary;6 G: L+ a D a) l3 k- N: B
- }0 V. S2 U' Z6 u7 V
- , ]. ]6 x* F! h, C P% M, [
- int binary_decimal(int n) /* Function to convert binary to decimal.*/
8 z3 @ {6 V" q' ~/ @ - {
+ `5 R# @) W7 w9 G& j - int decimal=0, i=0, rem;1 @- y$ b$ f4 `4 s( Z: X
- while (n!=0)
3 _# h4 Q2 q$ M2 Z$ @: c - {
# J ?* N% T7 c9 ^* a, v( Y; y! j - rem = n%10;4 H( d! m, E* p) x1 i
- n/=10;0 M) P% ~. p9 N! E+ w
- decimal += rem*pow(2,i);
. \8 J W. X5 d7 N3 U5 S% ~" W: y9 V - ++i;
0 g5 M h1 \: R - }
: @9 Z. {# \5 D, t# q: } - return decimal;# M. p( z8 J" K
- }
复制代码" k. f* P: [: l, f* ^0 B
结果输出:
. k1 z) A5 E+ g- ~5 ]
9、使用多维数组实现两个矩阵的相加
2 Q) U" [ s- P7 ~7 L; {2 q; F' _
源代码: - % J4 L$ Z, ]8 i; r) ]6 g. h
- #include <stdio.h>
; E; G* g6 a, F: u6 ^4 P& H - int main(){1 p" h. u. k+ Q, w4 u
- int r,c,a[100][100],b[100][100],sum[100][100],i,j;7 F+ ~0 M! a) l
- printf("Enter number of rows (between 1 and 100): ");
( \: H: j; m: n; l8 c5 ? - scanf("%d",&r);. Q7 Y. M0 h, L* M
- printf("Enter number of columns (between 1 and 100): ");5 b, t& r- @6 y7 g! q* k
- scanf("%d",&c);5 Q4 U5 K1 r, s- X+ N. u: a2 q
- printf("\nEnter elements of 1st matrix:\n");' y6 A% e0 G( i5 {9 o4 F) B9 j( ?
-
6 F ?# }: G3 x' M - /* Storing elements of first matrix entered by user. */
+ w5 `' R2 @7 y& P% u8 ] -
, ]0 p7 e! w8 ~2 z, K/ a - for(i=0;i<r;++i)% d) X- U6 ?5 m
- for(j=0;j<c;++j)1 S$ b9 @) l8 ~+ G4 ]9 J+ h
- {
. Z* S$ Z& |9 I; p0 U' x - printf("Enter element a%d%d: ",i+1,j+1);: E1 Y3 F2 \. Y+ v
- scanf("%d",&a[i][j]);+ w2 A. V9 S/ ^& e) W4 A9 r y
- }
/ \2 F, x8 F2 {+ Q: L -
& p8 {3 w) _8 c: W5 s1 _ - /* Storing elements of second matrix entered by user. */
- [ j" u' n |/ W - 7 \9 T Q" q+ ^: W. j& R0 i
- printf("Enter elements of 2nd matrix:\n");$ [# u, J" g2 `# i& i: W! l# ]
- for(i=0;i<r;++i)% F" s, M; E9 ^* L( t1 _" J% W
- for(j=0;j<c;++j)
1 n4 ^* V5 n) z - {
# o7 O/ G" {2 p: J; ?" K) @& S - printf("Enter element a%d%d: ",i+1,j+1);
$ A! q2 v3 Y w+ [3 y- Q) ]! r - scanf("%d",&b[i][j]);$ f) p) ]* Z" H& K
- }
, S; J9 L7 v! y2 K; M - 5 U( _- f) t; e$ q. k0 ]* [
- /*Adding Two matrices */, ~* f' j+ T3 I1 s; z, B
-
% ^ Z, E3 M% J( @ - for(i=0;i<r;++i)
9 L$ j7 \ Q9 L1 v8 ~4 ] - for(j=0;j<c;++j)
- d0 d. `" a9 ]4 n4 `. L - sum[i][j]=a[i][j]+b[i][j];
0 n8 u i: p3 x3 V0 n- y, ~$ W - + R3 M% I: v# D* [
- /* Displaying the resultant sum matrix. */( V' l% \# z2 P! ]
- 5 S, m+ C& E% `3 I# v' [
- printf("\nSum of two matrix is: \n\n");
& ~$ ], Z) E) h, p6 \1 H - for(i=0;i<r;++i) a* Q5 ~7 O5 P
- for(j=0;j<c;++j)
: q' T5 r) d3 m* v5 e3 m8 a; ]% k - {8 h1 d" p" n/ X5 N2 [$ S) _+ m' n
- printf("%d ",sum[i][j]);7 s; X1 j8 U. L4 S/ d
- if(j==c-1)
a. G- K# S# J+ ^6 D0 t0 h - printf("\n\n");; e# U {' {+ Y. O8 W/ }
- }
" ~ Y' |5 C; R; ], K2 C -
! c+ \" m- i2 f, y - return 0;+ D# ]4 p) r. \9 l
- }
复制代码
$ O$ S5 Z# ]5 S6 c" F/ ?6 I8 |结果输出: ! K% l5 u! s- h7 m2 V/ I
10、矩阵转置
* y. K9 b- P3 } G, a
源代码:
9 K3 C" l" @* Q) ~: V- #include <stdio.h>9 ?$ m, w, _/ _1 V2 I# p
- int main() E, k2 f5 d7 H) `/ Y# ~4 d# _
- {4 S6 p" o& n% g+ I
- int a[10][10], trans[10][10], r, c, i, j;
( A+ ?! X5 ^4 | - printf("Enter rows and column of matrix: ");3 x: M& S% w; }; r8 F
- scanf("%d %d", &r, &c);' c6 A* N$ B, o. @
- + k8 N2 L! I2 g/ Y& j; m1 L
- /* Storing element of matrix entered by user in array a[][]. */
' }& q6 ]$ L" i7 k4 a, _ - printf("\nEnter elements of matrix:\n");( \( Z$ I5 Y$ u* a; M
- for(i=0; i<r; ++i)
" i) F/ Q# U8 P7 S: G _' a - for(j=0; j<c; ++j)- \& `# C+ X) G" F
- {2 H2 ?' J6 O' Q. h
- printf("Enter elements a%d%d: ",i+1,j+1);
% m7 C+ I/ L0 g2 R1 R - scanf("%d",&a[i][j]);* O6 k1 ]8 i3 G
- }
+ s3 E# D' L( Q - /* Displaying the matrix a[][] */: _8 K0 T- H: h
- printf("\nEntered Matrix: \n");
( H3 q% \7 { `# m% q - for(i=0; i<r; ++i)
5 g. T& f1 l r+ S" h. S - for(j=0; j<c; ++j)
4 b1 S% p) ^6 Y1 E7 V c - {
O( m1 S9 W$ m0 C - printf("%d ",a[i][j]);
; \7 X# C/ h/ I+ c - if(j==c-1)7 i+ @( x$ M; f$ K' Q
- printf("\n\n");
" Z7 A$ }4 x V - }
, ^0 L3 T E6 E - " T% w* y6 X/ _- E1 ~" _
- /* Finding transpose of matrix a[][] and storing it in array trans[][]. */% W3 H' p, n3 @: N4 M: w' i
- for(i=0; i<r; ++i)
9 c' I6 b3 ~! Q3 h: q! } - for(j=0; j<c; ++j)
* [) [) z2 u+ i; S7 _. ^8 Y - {
3 P3 ^! F3 |6 T* S7 S+ Z - trans[j][i]=a[i][j];
6 p7 o1 I- l$ ?! F& e C$ J' ? - }: b: Z) _& R; U- m; W; Y
-
2 V$ X) Y9 M6 i# U+ i. i - /* Displaying the transpose,i.e, Displaying array trans[][]. */3 [, n) R& o7 W# z
- printf("\nTranspose of Matrix:\n");
) Y! h* z9 r4 \! P - for(i=0; i<c; ++i)
; z: I/ j6 R. j8 C5 J0 c, Y - for(j=0; j<r; ++j) _( r( }3 z6 l$ C+ z
- {" Z9 q5 e1 g- J, ]
- printf("%d ",trans[i][j]);3 Z i# `. [/ z. \
- if(j==r-1)" ?% w3 j3 K1 m+ \- K
- printf("\n\n");
- z( m- s. Q2 H; q0 R" s; _, q - }
: b, f- g' g& _& A0 [8 v - return 0;) z+ b% W* V9 y) `3 z) H
- }
复制代码
7 m" V- e+ Y$ y' c8 t; t0 E }9 \结果输出:
$ @4 k$ o) a1 [7 w3 D6 m% L: m |