100 cases of classical C language programming - question + answer code (31-40)

[procedure 31]

Title: Please input the first letter of the day of the week to judge the day of the week. If the first letter is the same, continue to judge the second letter. 1. Program analysis: it is better to use case statement. If the first letter is the same, then judge the second letter with case statement or if statement. 2. Program source code:

#include
void main()
{
char letter;
printf("please input the first letter of someday\n");
while ((letter=getch())!='Y')/*End when Y is pressed*/
{ switch (letter)
{case 'S':printf("please input second letter\n");
if((letter=getch())=='a')
printf("saturday\n");
else if ((letter=getch())=='u')
printf("sunday\n");

 
else printf("data error\n");
break;
case 'F':printf("friday\n");break;
case 'M':printf("monday\n");break;
case 'T':printf("please input second letter\n");
if((letter=getch())=='u')
printf("tuesday\n");
else if ((letter=getch())=='h')
printf("thursday\n");
else printf("data error\n");
break;
case 'W':printf("wednesday\n");break;
default: printf("data error\n");
}
}
}

[procedure 32]

Title: Press any key to change color, do you want to try it. Please hurry up!

#include
void main(void)
{
int color;
for (color = 0; color < 8; color++)
{
textbackground(color);/*Set the background color of the text*/
cprintf("This is color %d\r\n", color);
cprintf("Press any key to continue\r\n");
getch();/*Input character not visible*/
}
}

[procedure 33]

Topic: learn the gotoxy() and clrscr() functions
Program source code:

#include
void main(void)
{
clrscr();/*Clear screen function*/
textbackground(2);
gotoxy(1, 5);/*Location function*/

 
cprintf("Output at row 5 column 1\n");
textbackground(3);
gotoxy(20, 10);
cprintf("Output at row 10 column 20\n");
}

[procedure 34]

Topic: exercise function call
Program source code:

#include
void hello_world(void)
{
printf("Hello, world!\n");
}
void three_hellos(void)
{
int counter;
for (counter = 1; counter <= 3; counter++)
hello_world();/*Call this function*/
}
void main(void)
{
three_hellos();/*Call this function*/
}

[procedure 35]

Title: text color setting
Program source code:

#include
void main(void)
{
int color;
for (color = 1; color < 16; color++)
{
textcolor(color);/*Set text color*/
cprintf("This is color %d\r\n", color);
}
textcolor(128 + 15);
cprintf("This is blinking\r\n");
}

[procedure 36]

Title: find prime numbers within 100
program source code

#include
#include "math.h"
#define N 101
main()
{
int i,j,line,a[N];
for(i=2;ifor(i=2;i for(j=i+1;j {
if(a[i]!=0&&a[j]!=0)
if(a[j]%a[i]==0)
a[j]=0;}
printf("\n");
for(i=2,line=0;i{
if(a[i]!=0)
{printf("]",a[i]);
line++;}
if(line==10)
{printf("\n");
line=0;}
}
}

[procedure 37]

Title: sort 10 numbers
1. Program analysis: you can use the selection method, that is, from the last nine comparison processes, select the smallest one to exchange with the first one,
The next analogy is to compare and exchange the second element with the last eight.
2. Program source code:

#define N 10
main()
{int i,j,min,tem,a[N];
/*input data*/
printf("please input ten num:\n");
for(i=0;i{
printf("a[%d]=",i);
scanf("%d",&a[i]);}
printf("\n");
for(i=0;iprintf("]",a[i]);
printf("\n");
/*sort ten num*/
for(i=0;i{min=i;
for(j=i+1;jif(a[min]>a[ j]) min=j;
tem=a[i];

 
a[i]=a[min];
a[min]=tem;
}
/*output data*/
printf("After sorted \n");
for(i=0;iprintf("]",a[i]);
}

[procedure 38]

Title: find the sum of diagonal elements of a 3 * 3 matrix
1. Program analysis: use double for loop control to input two-dimensional array, and then add a[i][i] to output.
2. Program source code:

main()
{
float a[3][3],sum=0;
int i,j;
printf("please input rectangle element:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%f",&a[i][j]);
for(i=0;i<3;i++)
sum=sum+a[i][i];
printf("duijiaoxian he is %6.2f",sum);
}

[procedure 39]

Title: there is an array already arranged. Now input a number and insert it into the array according to the original rule.
1. Program analysis: first, judge whether the number is greater than the last number, and then consider the situation of inserting the middle number. After inserting
The number after this element, one position in turn.
2. Program source code:

main()
{
int a[11]={1,4,6,9,13,16,19,28,40,100};
int temp1,temp2,number,end,i,j;
printf("original array is:\n");
for(i=0;i<10;i++)
printf("]",a[i]);
printf("\n");
printf("insert a new number:");
scanf("%d",&number);
end=a[9];
if(number>end)
a[10]=number;
else

 
{for(i=0;i<10;i++)
{ if(a[i]>number)
{temp1=a[i];
a[i]=number;
for(j=i+1;j<11;j++)
{temp2=a[j];
a[j]=temp1;
temp1=temp2;
}
break;
}
}
}
for(i=0;i<11;i++)
printf("m",a[i]);
}

[procedure 40]

Title: output an array in reverse order.
1. Program analysis: exchange the first one with the last one.
2. Program source code:

#define N 5
main()
{ int a[N]={9,6,5,4,1},i,temp;
printf("\n original array:\n");
for(i=0;i printf("M",a[i]);
for(i=0;i {temp=a[i];
a[i]=a[N-i-1];
a[N-i-1]=temp;
}
printf("\n sorted array:\n");
for(i=0;i printf("M",a[i]);
}

Oh, I almost forgot. I have a humble little dream. I really have a good look at the full Sichuan nice on the CSDN. There are talents. If only I could make friends with her one day.

135 original articles published, praised 420, visited 140000+
Private letter follow

Posted on Fri, 13 Mar 2020 03:53:34 -0700 by abbe-rocks