C Language Programming Notes – Examples of Using scanf and Arithmetic Operators

Example 1: Input a time duration (in minutes) from the keyboard and calculate how many hours and minutes that duration corresponds to.(1) Source Code

#include <stdio.h>
#include <stdlib.h>
int main(){    int a,b,c;    printf("Please enter a time duration (in minutes):");    scanf("%d",&a);    b=a/60;    c=a%60;    printf("%d minutes is equivalent to %d hours and %d minutes\n",a,b,c);    return 0;}

(2) Output ScreenshotC Language Programming Notes - Examples of Using scanf and Arithmetic OperatorsExample 2: Input a four-digit positive integer from the keyboard and separate each digit.(1) Source Code

#include <stdio.h>
#include <stdlib.h>
int main(){    int A,yi,san,wu,qi;    printf("Please enter a four-digit positive integer:");    scanf("%d",&A);    yi=A/1000;    san=A%1000/100;    wu=A%1000%100/10;    qi=A%10;    printf("The digits of %d are:
Thousands: %d  Hundreds: %d  Tens: %d  Units: %d\n",A,yi,san,wu,qi);    return 0;}

(2) Output ScreenshotC Language Programming Notes - Examples of Using scanf and Arithmetic Operators

Example 3: Input a real number (with at least3 decimal places) from the keyboard, and round it to2 decimal places.

(1) Source Code

#include <stdio.h>
#include <math.h>
int main(){    double A,B;    printf("Please enter a real number with at least 3 decimal places:");    scanf("%lf",&A);    B=(float)(round)(A*100)/100;    printf("Rounding %lf (keeping two decimal places) results in: %lf\n",A,B);    return 0;}

(2) Output ScreenshotC Language Programming Notes - Examples of Using scanf and Arithmetic Operators

Example 4: Input the lengths of the three sides of a triangle from the keyboard and calculate the area of the triangle.

Note: The mathematical library function to calculate the square root ofx is sqrt(x), and you need to include the header file #include <math.h>

C Language Programming Notes - Examples of Using scanf and Arithmetic Operators(1) Source Code

#include <stdio.h>
#include <math.h>
int main(){    int a,b,c;    double x,s;    printf("Please enter the three sides of the triangle:");    scanf("%d,%d,%d",&a,&b,&c);    x=(a+b+c)/2;    s=sqrt(x*(x-a)*(x-b)*(x-c));    printf("The area of the triangle is: %lf\n",s);    return 0;}

(2) Output ScreenshotC Language Programming Notes - Examples of Using scanf and Arithmetic Operators

Example 5: Input 3 digit characters from the keyboard and convert them to their corresponding integer values (i.e., the character0 is converted to the integer0, the character1 is converted to the integer1, and so on), and then calculate the average of3 integers.

(1) Source Code

#include <stdio.h>
#include <math.h>
int main(){    char a,b,c;    int x,y,z;    double aver;    printf("Please enter 3 digit characters:");    scanf("%c,%c,%c",&a,&b,&c);    x=a-48,y=b-48,z=c-48;    aver=(x+y+z)/3;    printf("Average: %lf\n",aver);    return 0;}

(2) Output ScreenshotC Language Programming Notes - Examples of Using scanf and Arithmetic Operators

Leave a Comment