Introduction to C programming--a comprehensive exercise (iv) on-machine test questions

Source: Internet
Author: User

This procedure is our College C language on the machine test questions, by my classmates summed up, and shared to everyone. (Word of the program, layout some problems, please forgive me)


Sum or product of n (item) data

The and (or product) of n data is #include <stdio.h>int main () {     int a[100];    int i   = 0;     int n;     printf ("This program will implement tired and there are cumulative features, please enter the number of items \ n");     scanf ("%d", &n);     printf ("Please input each \ n");     for (i   = 0; i<n; i++) {         scanf ("%d", &a[i]);      }    Beg tired and     int sum = 0;     for (i   = 0; i<n; i++) {         sum = sum + a[i];      }     printf ("sum =%d\n", sum);    Seek cumulative     int ji = 1;     for (i   = 0; i<n; i++) {         ji  = Ji * a[i];      }     printf ("Ji  =%d\n", Ji);      return 0;  }

the sum or product of the numbers of decimal integers

1#include <stdio.h>

2

3int Main ()

4 {

5 int A;

6 int sum;

7 int ji;

8 int b[10];

9 scanf ("%d", &a);

Ten if (a<10) {

printf ("Sum =%d\n", a);

printf ("Ji =%d\n", a);

13}

+ Else if (a>=10&&a<100) {

B[0] = A/10;

B[1] = a% 10;

printf ("sum =%d\n", b[0]+b[1]);

printf ("Ji =%d\n", b[0]*b[1]);

19}

ElseIf (a>=100&&a<1000) {

B[0] = a/100;

B[1] = (a% 100)/10;

B[2] = ((a% 100)% 10)/1;

printf ("sum =%d\n", b[0]+b[1]+b[2]);

printf ("Ji =%d\n", b[0]*b[1]*b[2]);

26}

The else if (a>=1000&&a<10000) {

B[0] = a/1000;

B[1] = (a% 1000)/100;

B[2] = ((a% 1000)% 100)/10;

B[3] = (((a% 1000)% 100)%10)/1;

printf ("sum =%d\n", b[0]+b[1]+b[2]+b[3]);

printf ("sum =%d\n", b[0]*b[1]*b[2]*b[3]);

34}

+ Else

printf ("It is too big\n");

37

38

0;

40}

Ask average of n data (exact value or rounding with fractional parts)

1#include <stdio.h>

2

3int Main ()

4 {

5 int n;

6 int i = 0;

7 int sum = 0;

8 int a[100];

9 float average;

Ten scanf ("%d", &n);

printf ("\ n");

(i = 0; i< N; i++) {

scanf ("%d", &a[i]);

14}

15

(i = 0; i< N; i++) {

sum = sum + a[i];

18}

printf ("average =%.3f\n", (float) sum/n);

return 0;

21}

To find the maximum (or minimum) value

1#include <stdio.h>

2

3int Main ()

4 {

5 int n;

6 int a[100];

7 int i = 0;

8 int Max;

9 int Min;

Ten scanf ("%d", &n);

if (n> 100) {

printf ("N is too big\n");

13}

else{

for (i = 0; i < n; i++) {

scanf ("%d", &a[i]);

17}

Max = a[0];

for (i = 1; i<n; i++) {

if (Max<a[i])

Max = A[i];

22}

Min = a[0];

(i = 1; i<n; i++) {

if (Min>a[i])

Min = A[i];

27}

28

29

30

printf ("max =%d\n", max);

printf ("min =%d\n", min);

33

34}

return 0;

36}

liters (Descending) Order sequencing

1#include <stdio.h>

2

3int Main ()

4 {

5 int a[100], I, J, temp, R;

6 int n;

7 scanf ("%d", &n);

8 if (n>100) {

9 printf ("N is Toobig");

10}

else{

(i = 0; i< N; i++) {

scanf ("%d", &a[i]);

14}

(i = 0; i<n-1; i++) {

R = i;

+ for (j = i+ 1; j<n; J + +) {

if (a[r]> a[j])

R = J;

20}

21st

if (r! = i) {

temp = A[i];

A[i] = A[r];

A[R] = temp;

26}

27}

28

(i = 0; i< N; i++)

printf ("%d", a[i]);

31}

32

33

return 0;

35}

Letter Case Conversion

1#include <stdio.h>

2#include <string.h>

3

4int Main ()

5 {

6 Char a[100];

7 int i;

8 scanf ("%s", a);

9 printf ("beforeexchange\n\t%s\n", a);

Ten for (i = 0; I<strlen (a); i++) {

if (a[i]>=97&&a[i]<=122)

A[i] = a[i]-32;

All else if (a[i]>=65&&a[i]<=90)

A[i] = A[i] + 32;

15}

printf ("After exchange\n\t%s\n", a);

0;

18}

Count the number of occurrences of a character in a string

1#include <stdio.h>

2#include <string.h>

3

4int Main ()

5 {

6 Char a[100];

7 Char B;

8 int n = 0;

9 int i;

Ten scanf ("%s\n", a);

One scanf ("%c", &b);

12

13

+ for (i = 0; I<strlen (a); i++) {

if (a[i] = = b)

n++;

17}

18

printf ("%d\n", N);

return 0;

21}

Connect two strings

1#include <stdio.h>

2#include <string.h>

3

4int Main ()

5 {

6 Char a[100], b[100];

7 memset (A, 100, 0);

8 memset (b, 100, 0);

9

Ten int i = 0;

one int n;

int m;

scanf ("%s", a);

scanf ("%s", b);

15

n = strlen (a);

+ M = strlen (b);

18

for (i= 0; i<m; i++) {

A[n+i]=b[i];

21}

A[m+n] = ' + ';

23

printf ("%s\n", a);

return 0;

26}

copies the substring before (or after) the specified character the previous;

1#include <stdio.h>

2

3//defines a function such as

4int Fu_zhi (Char *str1, char *str2, Char ch)

5 {

6 int i = 0, j = 0;

7 int k;

8 while (STR1[I]!=CH)

9 i++;

10

One j = i;

K = i;

while (i>=0) {

STR2[J-1] = str1[i-1];

i--;

j--;

17}

Str2[k] = ' + ';

return 0;

20}

21st

23int Main ()

24 {

Char a[100];

+ Char b[100];

Char ch;

printf ("Please enter the specified character:");

scanf ("%c", &ch);

printf ("Please enter the specified string:");

scanf ("%s", a);

Fu_zhi (A, b, ch);

printf ("%s\n", b);

37

return 0;

39}

After that;

1#include <stdio.h>

2

3//defines a function such as

4int Fu_zhi (Char *str1, char *str2, Char ch)

5 {

6 int i = 0, j = 0;

7 while (STR1[I]!=CH)

8 i++;

9 while (str1[i]!= ') {

Ten str2[j]= str1[i+1];

One by one i++;

j + +;

13}

STR2[J] = ' + ';

return 0;

16}

17

18

19int Main ()

20 {

(+ char a[100];

-Char b[100];

-Char ch;

24

printf ("Please enter the specified character:");

scanf ("%c", &ch);

printf ("Please enter the specified string:");

scanf ("%s", a);

29

Fu_zhi (A, b, ch);

31

printf ("%s\n", b);

33

return 0;

35}

the input and output of a struct type variable (or array element) value

1#include <stdio.h>

2#include <stdlib.h>

3

4struct zhengjiewei{

5 Char a[20];

6 int b;

7 int c[10][10];

8 Char ch;

9

10};

11

12int Main ()

13 {

Zhengjiewei-struct * TWOB;

Twob = (struct Zhengjiewei *) malloc (sizeof (struct zhengjiewei));

16//To assign memory to a variable in a struct, you cannot treat it as a local variable ...

scanf ("%s", twob->a);

scanf ("%d", &twob->c[3][6]);

printf ("%d\n", twob->c[3][6]);

printf ("%s\n", twob->a);

21st

return 0;

23

24}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Introduction to C programming--a comprehensive exercise (iv) on-machine test questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.