3. Chapter III Arithmetic Operators and Arithmetic Expressions

1-Separate Numbers

Write a program that inputs x (three digits) and outputs 10, 100 digits separated by spaces

Tasks and Code

/*
*copyright (c)2017 CSDN College, 2001
*All rights resrrved.
*File name: mian.c
*Author: Huang Jiajun
*Completion date: 8 December 2017
*Version number: v1.0
*/
//  Description of the problem: Write a program, enter x (three digits), output its 10, 100 digits, separated by spaces
//  Program output; 10, 100 digits.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x,a,b,c;
    printf("Please enter an integer:");
    scanf("%d",&x);
    printf("The 100 digits are:%d %d %d",(a=x%10),(b=(x/10)%10),(c=x/100));   //The number of digits is equal to an integer divided by 10 and the remainder is equal to an integer divided by 10 and divided by 10.Divide 100 digits by 100.
    return 0;
}


Run Results


  


2. Separate integers and fractions

Write a program that inputs a floating point number (three digits after the decimal point) from the keyboard and outputs the integer and decimal parts of the number, respectively.

Tasks and Code

/*
*copyright (c)2017 CSDN College, 2001
*All rights resrrved.
*File name: mian.c
*Author: Huang Jiajun
*Completion date: 8 December 2017
*Version number: v1.0
*/
//  Description of the problem: Write a program that inputs a floating point number (three digits after the decimal point) from the keyboard and outputs the integer and decimal parts of the number respectively.
//  Program output; integer and decimal parts of the number.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float x;
    int a,b;
    printf("Please enter a floating point number:");
    scanf("%f",&x);
    a=x;    //Cast
    b=(int)(x*1000)%1000;   //100 digits multiplied by 1000 and divided by 1000, a decimal number
    printf("Namely:%d %d ",a,b);
    return 0;
}

Run Results




3. -How do I buy roses?

Xiao Hui has a birthday and Xiao Ming wants to buy flowers for her.Each red rose costs 5 yuan, five for one branch and 20 for five.Xiao Ming has n (n>10) yuan in total. How much can he buy at most?

Tasks and Code

/*
*copyright (c)2017 CSDN College, 2001
*All rights resrrved.
*File name: mian.c
*Author: Huang Jiajun
*Completion date: 8 December 2017
*Version number: v1.0
*/
//  Description of the problem: Xiao Hui has a birthday and Xiao Ming wants to buy flowers for her.Each red rose costs 5 yuan, five for one branch and 20 for five.Xiao Ming has n (n>10) yuan in total. How much can he buy at most?
//  Program output; bought roses.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int money,flower,give1,give2;
    printf("Please enter the money you paid for the flowers:");
    scanf("%d",&money);
    flower=money/5;    //Actual Roses
    give1=flower/20*5;  //Greedy method calculates 20 send 5 first
    give2=(flower-(flower/20)*20)/5;  //Not enough to buy 20 for 5, enjoy 5 for 1
    printf("Altogether available %d Flowers!Thank you for your patronage.",flower+give1+give2);
    return 0;
}

Run Results




4. Play with numbers

Enter three double-precision real numbers and find the sum, mean, sum of squares, and the square sum of them, respectively, and output the calculated values.

Tasks and Code

/*
*copyright (c)2017 CSDN College, 2001
*All rights resrrved.
*File name: mian.c
*Author: Huang Jiajun
*Completion date: 8 December 2017
*Version number: v1.0
*/
//  Problem Description: Enter three double-precision real numbers and find the sum, mean, sum of squares, and the square sum of them, respectively, and output the calculated values.
//  Program output; sum, mean, sum of squares, and the square of the sum of squares
#include <stdio.h>
#include <stdlib.h>
#Include <math.h> //function Library
int main()
{
     float x,x1,x2;  
    float sum,avg,sq_sum,root;  
    scanf("%f %f %f", &x, &x1, &x2);  
    sum = x + x1 + x2;  //The sum of three numbers
    avg = sum / 3;    //The sum of three numbers divided by three equals the mean
    sq_sum = x*x + y*y + z*z;  //Sum of squares
    root = sqrt(sq_sum);     //  Calculating the square of a sum using a function library
    printf("And:%f\n", sum);  
    printf("Average:%f\n", avg);  
    printf("Sum of squares:%f\n", sq_sum);  
    printf("Square and square:%f\n", root);  
    return 0;  
}


Run Results




5. Coordinate Conversion

Write a program to manipulate polar coordinates (r, theta) (theta is in degrees) is converted to rectangular coordinates (X,Y).

Tasks and Code

/*
*copyright (c)2017 CSDN College, 2001
*All rights resrrved.
*File name: mian.c
*Author: Huang Jiajun
*Completion date: 8 December 2017
*Version number: v1.0
*/
//  Description of the problem: Write a program to convert polar coordinates (r, theta) (theta is in degrees) to rectangular coordinates (X,Y).
//  Program output; Cartesian coordinates
#include <stdio.h>
#include <stdlib.h>
#Include <math.h> //function Library
#define PI 3.1415926 //symbol constant
int main()
{
     float r, theta, x, y;
     printf("Please enter polar coordinates:");
     scanf("%f %f", &r, &theta);
    x = r * cos(theta/180*PI);//Note that cos requires radians as parameters
    y = r * sin(theta/180*PI);
    printf("Cartesian coordinates are %f %f", x, y);
    return 0;
}

Run Results


Posted by priya_amb on Thu, 02 Jul 2020 09:16:23 -0700