1.
Write a program that prompts for and reads a floating-point value.
The program prints the whole part on one line and the decimal (fraction) part on a second line.
For example, if the input value is 123.456, it would print the output:
the input value is 123.456
the whole part is 123
the decimal(fraction) part is 0.456
複製代碼
#include<stdio.h>
int main(){
double a,b,c;
printf("input a floating-point number");
scanf(" %lf", &a);
b = (int)a;
c = a-b;
printf("\nthe input value is %lf",a);
printf("\nthe whole part is %lf",b);
printf("\nthe decimal(fraction) part is %lf",c);
}
複製代碼
作者: Smallcat 時間: 2018-9-29 23:16
2.
Write a program that asks the user to enter two numbers,
obtains the two numbers from the user and prints the sum, product, difference, and quotient of the two numbers.
Number1 Number2
Case I int int
Case II float float
Case III double double
Case VI int float
Consider and discuss the results.
#include<stdio.h>
int main(){
int a,b;
double c,d,e,f;
printf("case1: input two numbers");
printf("\nNo.1:");
scanf(" %d",&a);
printf("\nNo.2:");
scanf(" %d",&b);
c = a+b;
d = a*b;
e = a-b;
f = a/b;
printf("\nthe sum is %lf",c);
printf("\nthe product is %lf",d);
printf("\nthe difference is %lf",e);
printf("\nthe quotient is %lf",f);
float h,i;
printf("\n\ncase2: input two numbers are float");
printf("\nNo.1:");
scanf(" %f",&h);
printf("\nNo.2:");
scanf(" %f",&i);
c = h+i;
d = h*i;
e = h-i;
f = h/i;
printf("\nthe sum is %lf",c);
printf("\nthe product is %lf",d);
printf("\nthe difference is %lf",e);
printf("\nthe quotient is %lf",f);
double j,k;
printf("\n\ncase2: input two numbers are double");
printf("\nNo.1:");
scanf(" %lf",&j);
printf("\nNo.2:");
scanf(" %lf",&k);
c = j+k;
d = j*k;
e = j-k;
f = j/k;
printf("\nthe sum is %lf",c);
printf("\nthe product is %lf",d);
printf("\nthe difference is %lf",e);
printf("\nthe quotient is %lf",f);
int m;
float m;
printf("\n\ncase2: input two numbers are double");
printf("\nNo.1:");
scanf(" %d",&m);
printf("\nNo.2:");
scanf(" %f",&n);
c = m+n;
d = m*n;
e = m-n;
f = m/n;
printf("\nthe sum is %lf",c);
printf("\nthe product is %lf",d);
printf("\nthe difference is %lf",e);
printf("\nthe quotient is %lf",f);
}
複製代碼
作者: Smallcat 時間: 2018-9-29 23:23
3.
Write a program that accepts an integer between 7 and 9 digits long.
a. Extracts and prints the third-rightmost digit of the input data.
b. Writes the integer with commas between every third digit starting from the right.
Example:
Input: 12345678
Output:
The third-rightmost digit of the input data is 6
The input data with commas between every third digit is 12,345,678
#include<stdio.h>
int main(){
int a,b,c,d,e,f;
printf("input an integer between 7 and 9 digits long: ");
scanf(" %d", &a);
b = a%1000000;
c = a%1000;
f = c/100;
d = a/1000000;
e = b/1000;
printf("\nThe third-rightmost digit of the input data is %d",f);
printf("\nThe input data with commas between every third digit is %d,%d,%d",d,e,c);
}
複製代碼
作者: Smallcat 時間: 2018-9-29 23:27
4.
Write a program to calculate the diameter, the circumference, and the area of a circle with a radius of 6.75.
Assign the radius of float variable, and then output the radius with an appropriate message.
Declare a named const PI with the value 3.14159.
The program should output the diameter, the circumference, and the area, each on a separate line.
Print each value to five decimal places within a total field width(欄位) of 10.
Note1:
when the compiler reads this specifier: %10.5f
i.The compiler prepares 10 columns to output this real number with the five right-most columns for the fraction part.
ii.If the real number has less than five digits in the fraction part, the compilers pads the remaining columns with zero.
iii.The 6th column from the right is the decimal point.
iv.The remaining four columns are the integer part.
If the real number has less than four digits in the integer part, the output is padded with blank on the left.