School Number 160809224 name Huang Jia-shuai C language Program design experiment 2 Select structure Program design

Source: Internet
Author: User

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:
    1. using Data functions requires #include <math.h>
    2. root function:sqrt (x)
    3. 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:
    1. input character to variable c

Char c;

Method one:c = GetChar ();

Method Two: scanf ("%c", &c);

    1. 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

Related Article

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.