Experiment 2-1 input 3 number and output in order from large to Small.
Experimental Requirements:
Write a C program, Enter 3 number, and in the order from large to small Output.
Source:
#include <stdio.h>
void Main () {
int a,b,c,t;
printf ("please Enter three integers:");
scanf ("%d%d%d", &a,&b,&c);
If (a<b) {
t = a;
A = b;
b = t;
}
If (b>c) {
printf ("%d\t%d\t%d\n", a,b,c);
}
else if (c>a) {
printf ("%d\t%d\t%d\n", c,a,b);
}
else{
printf ("%d\t%d\t%d\n", a,c,b);
}
}
Experiment 2-2 Enter The value of x from the keyboard and the value of the output y According to the calculation
Experimental Requirements: Enter the value of x from the keyboard and the value of the output y according to the calculation
tips:
- using Data functions requires #include <math.h>
- root function:sqrt (x)
- absolute Value function:fabs (x)
Source:
# include <stdio.h>
# include <math.h>
int main (void)
{
Double x, y;
printf ("enter x:");
scanf ("%lf", &x);
If (x >4) {
y = sqrt (x-4);
}
else if (x <-5) {
Y =fabs (x);
}
else{
y = x+3;
}
printf ("f (%.2f) =%.2f\n", x, y);
Return 0;
}
Experimental Results:
Experiment 2-3 Enter a letter from the keyboard, if it is a lowercase letter, convert it to uppercase and Output.
Experimental Requirements: Enter a letter from the keyboard, if it is a lowercase letter, convert it to uppercase and Output.
tips:
- input character to variable c
Char c;
Method one:c = GetChar ();
Method Two: scanf ("%c", &c);
- output character variable c
Method one:Putchar (c);
Method Two: printf ("%c", c);
Program source code #include <stdio.h>
int main () {
Char c;
printf (" Enter a letter :");
scanf ("%c", &c);
printf ("%c\n", c-32);
}
Run result capture
Experiment 2-4 Enter The value of x from the keyboard and the value of the output y According to the calculation
Experimental Requirements: Enter the value of x from the keyboard and the value of the output y according to the calculation
Program Source Code
#include <stdio.h>
int main ()
{
Float x, y;
scanf ("%f", &x);
If (x<1)
y=x;
else if (x>=1 && x<10)
y=2*x-1;
Else
y=3*x-11;
printf ("%f", y);
Return 0;
}
Run result capture
Experiment 2-5 give a percentile grade, request grade
'
A
'
,
'
B
'
,
'
C
'
,
'
D
'
,
'
E
'
, where the output of the above
'
A
'
, the output
'
B
'
,~ + output
'
C
'
, the output
'
D
'
, the output of the following
'
E
'
.
Experimental Requirements:
Give a percentile performance, require grades ' a ', ' B ', ' C ', ' D ', ' E ', where the output of more than ' a ', the output ' B ',~ + output ' C ','~ ' output ' D ' , the output of the following ' E '.
tips:
This experiment requires the students to use two methods to Complete:
method One: Use The IF statement to complete
method two: Use the switch statement to Complete.
Program Source Code
#include <stdio.h>
int main ()
{
Float a;
scanf ("%f", &a);
If (a>=90)
printf ("A");
else if (a>=80 && a<90)
printf ("B");
else if (a>=70 && a<80)
printf ("C");
else if (a>=60 && a<70)
printf ("D");
Else
printf ("E");
Return 0;
}
Run result capture
Experiment Experience
Each language program has its own unique characteristics, we need to be careful to complete, although the different, but there will be some similarities worthy of us to go and other procedures to reference, as long as we are careful enough, patient, the brain is clear, you will be able to complete the design of the Program. For me may be not very familiar with, hope that in the future will gradually more understanding.
School Number 160809224 name Huang Jia-shuai C language Program design experiment 2 Select structure Program design