100 examples of interesting C language programming (2)

Source: Internet
Author: User
Tags case statement

"Program 31"
Title: Please enter the first letter of the day of the week to determine the day of the week, if the first letter, then continue
Judge the second letter.
1. Procedure analysis: Use the situation statement is better, if the first letter, then judge the case statement or if statement to determine the second letter.
2. Program Source code:
#include <stdio.h>
void Main ()
{
Char letter;
printf ("Please input the first letter of someday\n");
while ((Letter=getch ())! = ' Y ')/* ends when the letter Y is pressed/*
{Switch (letter)
{case ' S ':p rintf ("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 ':p rintf ("friday\n");
Case ' M ':p rintf ("monday\n");
Case ' T ':p rintf ("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 ':p rintf ("wednesday\n");
default:printf ("Data error\n");
}
}
}
==============================================================
"Program 32"
Title: Press any key-to-change-color, do-want to try it. Please hurry up!
1. Program Analysis:
2. Program Source code:
#include <conio.h>
void Main (void)
{
int color;
for (color = 0; color < 8; color++)
{
Textbackground (color);/* Sets the background color of text */
Cprintf ("This is a color%d\r\n", color);
cprintf ("Press any key to continue\r\n");
Getch ();/* input character not visible */
}
}
==============================================================
"Program 33"
Title: Learning Gotoxy () and CLRSCR () functions
1. Program Analysis:
2. Program Source code:
#include <conio.h>
void Main (void)
{
CLRSCR ();/* Clear screen function */
Textbackground (2);
Gotoxy (1, 5);/* Positioning function */
cprintf ("Output at row 5 column 1\n");
Textbackground (3);
Gotoxy (20, 10);
cprintf ("Output at row column 20\n");
}
==============================================================
"Program 34"
Title: Practice function calls
1. Program Analysis:
2. Program Source code:
#include <stdio.h>
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 */
}
==============================================================
"Program 35"
Title: Text color settings
1. Program Analysis:
2. Program Source code:
#include <conio.h>
void Main (void)
{
int color;
for (color = 1; color < color++)
{
TextColor (color);/* Set text color */
Cprintf ("This is a color%d\r\n", color);
}
TextColor (128 + 15);
Cprintf ("This is blinking\r\n");
}
==============================================================
"Program 36"
Title: Number of primes within 100
1. Program Analysis:
2. Program Source code:
#include <stdio.h>
#include "math.h"
#define N 101
Main ()
{
int i,j,line,a[n];
for (i=2;i<n;i++) a[i]=i;
For (I=2;i<sqrt (N); i++)
for (j=i+1;j<n;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<n;i++)
{
if (a[i]!=0)
{printf ("%5d", A[i]);
line++;}
if (line==10)
{printf ("\ n");
line=0;}
}
}
==============================================================
"Program 37"
Title: Sort 10 Numbers
1. Program Analysis: You can use the selection method, that is, from the next 9 comparison process, select a minimum with the first element exchange,
Next analogy, the second element is compared with the next 8 and is exchanged.
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<n;i++)
{
printf ("a[%d]=", I);
scanf ("%d", &a[i]);}
printf ("\ n");
for (i=0;i<n;i++)
printf ("%5d", A[i]);
printf ("\ n");
/*sort Ten num*/
for (i=0;i<n-1;i++)
{min=i;
for (j=i+1;j<n;j++)
if (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;i<n;i++)
printf ("%5d", A[i]);
}
==============================================================
"Program 38"
Title: Finding the sum of diagonal elements of a 3*3 matrix
1. Program Analysis: Use the dual for loop control input two-dimensional array, and then add a[i][i] after the 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);
}
==============================================================
"Program 39"
Title: There is an array that has been sequenced. Now enter a number that requires it to be inserted into the array as it was originally.
1. Program Analysis: First determine whether this number is greater than the last number, and then consider inserting the middle number of cases, after inserting
The number after which this element is moved, followed by one position.
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 ("%5d", 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 ("%6d", A[i]);
}
==============================================================
"Program 40"
Title: Output an array in reverse order.
1. Procedure analysis: With the first and the last exchange.
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<n;i++)
printf ("%4d", A[i]);
for (i=0;i<n/2;i++)
{Temp=a[i];
A[I]=A[N-I-1];
A[n-i-1]=temp;
}
printf ("\ n sorted array:\n");
for (i=0;i<n;i++)
printf ("%4d", A[i]);
}

Classic C Program 100 cases ==41--50

"Program 41"
Title: Learning static defining the use of statics
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
Varfunc ()
{
int var=0;
static int static_var=0;
printf ("\40:var equal%d \ n", Var);
printf ("\40:static var equal%d \ n", Static_var);
printf ("\ n");
var++;
static_var++;
}
void Main ()
{int i;
for (i=0;i<3;i++)
Varfunc ();
}
==============================================================
"Program 42"
Topic: Learning to use Auto to define the usage of variables
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
Main ()
{int i,num;
num=2;
for (i=0;i<3;i++)
{printf ("\40:the num equal%d \ n", num);
num++;
{
auto int num=1;
printf ("\40:the internal block num equal%d \ n", num);
num++;
}
}
}
==============================================================
"Program 43"
Title: Learn another use of static.
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
Main ()
{
int i,num;
num=2;
for (i=0;i<3;i++)
{
printf ("\40:the num equal%d \ n", num);
num++;
{
static int num=1;
printf ("\40:the internal block num equal%d\n", num);
num++;
}
}
}
==============================================================
"Program 44"
Title: Learn how to use external.
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
int a,b,c;
void Add ()
{int A;
A=3;
C=a+b;
}
void Main ()
{a=b=4;
Add ();
printf ("The value of C is equal to%d\n", c);
}
==============================================================
"Program 45"
Title: Learn how to define variables using register.
1. Program Analysis:
2. Program Source code:
void Main ()
{
register int i;
int tmp=0;
for (i=1;i<=100;i++)
Tmp+=i;
printf ("The sum is%d\n", TMP);
}
==============================================================
"Program 46"
Title: Macro # define command exercises (1)
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
#define TRUE 1
#define FALSE 0
#define SQ (x) (x) * (x)
void Main ()
{
int num;
int again=1;
printf ("\40:program would stop if input value less than 50.\n");
while (again)
{
printf ("\40:please input number==>");
scanf ("%d", &num);
printf ("\40:the Square for this number is%d \ n", SQ (num));
if (num>=50)
Again=true;
Else
Again=false;
}
}
==============================================================
"Program 47"
Title: Macro # define command exercises (2)
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
#define Exchange (A, b) {\ */* Macro allows two clothes commands to be included, you must add "\" to the right
int t;\
T=a;\
A=b;\
B=t;\
}
void Main (void)
{
int x=10;
int y=20;
printf ("x=%d; Y=%d\n ", x, y);
Exchange (x, y);
printf ("x=%d; Y=%d\n ", x, y);
}
==============================================================
"Program 48"
Title: Macro # define command exercises (3)
1. Program Analysis:
2. Program Source code:
#define LAG >
#define SMA <
#define EQ = =
#include "stdio.h"
void Main ()
{int i=10;
int j=20;
if (i LAG j)
printf ("\40:%d larger than%d \ n", i,j);
else if (i EQ j)
printf ("\40:%d equal to%d \ n", i,j);
else if (i SMA j)
printf ("\40:%d smaller than%d \ n", i,j);
Else
printf ("\40:no such value.\n");
}
==============================================================
"Program 49"
Title: Comprehensive application of #if #ifdef和 #ifndef.
1. Program Analysis:
2. Program Source code:
#include "stdio.h"
#define MAX
#define MAXIMUM (x, y) (x>y)? x:y
#define MINIMUM (x, y) (x>y)? y:x
void Main ()
{int a=10,b=20;
#ifdef MAX
printf ("\40:the larger one is%d\n", MAXIMUM (b));
#else
printf ("\40:the Lower one is%d\n", MINIMUM (b));
#endif
#ifndef MIN
printf ("\40:the Lower one is%d\n", MINIMUM (b));
#else
printf ("\40:the larger one is%d\n", MAXIMUM (b));
#endif
#undef MAX
#ifdef MAX
printf ("\40:the larger one is%d\n", MAXIMUM (b));
#else
printf ("\40:the Lower one is%d\n", MINIMUM (b));
#endif
#define MIN
#ifndef MIN
printf ("\40:the Lower one is%d\n", MINIMUM (b));
#else
printf ("\40:the larger one is%d\n", MAXIMUM (b));
#endif
}
==============================================================
"Program 50"
Title: Application Exercises for #include
1. Program Analysis:
2. Program Source code:
The Test.h file is as follows:
#define LAG >
#define SMA <
#define EQ = =
#include "test.h"/* A new file 50.c, including test.h*/
#include "stdio.h"
void Main ()
{int i=10;
int j=20;
if (i LAG j)
printf ("\40:%d larger than%d \ n", i,j);
else if (i EQ j)
printf ("\40:%d equal to%d \ n", i,j);
else if (i SMA j)
printf ("\40:%d smaller than%d \ n", i,j);
Else
printf ("\40:no such value.\n");
}

Fun C Language Programming 100 cases (2)

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.