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