Question

I'm using code blocks to perform this. These problems are from my C Programming Beginners book....

I'm using code blocks to perform this. These problems are from my C Programming Beginners book. Not too familiar with

C programming so any help would be great. I would like to see how it is formatted. Also, I don't understand Problem 4 at all. Thanks.

1- Create three integer variables and initialize to zero.

2- Create three float variables and initialize to zero.

3- Create three double variables and initialize to zero.

4- Create one character variables and initialize to the space character.

5- Prompt the user for integer input and store in the first integer variable.

6- Prompt the user for integer input and store in the second integer variable.

7- Add the values stored in the two integers and assign it to the third integer.

8- Output the value of the third integer as shown in sample run.

9- Repeat steps 7 and 8 for subtraction, multiplication, and division.

10- Repeat steps 5 through 9 for floats and doubles accordingly. Output float with precision

of 2 and doubles with precision of 4.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

question 1 to 4 is quite straight forward , just declare a variable and assign 0 to it .Which you can easily understand ow to do it by looking at the code thoroughly once, then in question 5 and 6 we, have to prompt the user for entering new value for our declared variables of various data type one by one, so in c for user input , we use scanf() function, in it we use different format specifier for different datatype of input, for integer we have used %d for float we have used %f and for double we have used %lf.

then we have to do binary operations like addition , subtraction, multiplication and division and store the value in the third remaining variable.and then print it using the right format specifier as shown in the code and  as used during the use of scanf() function.

In question 8, you are asking about the output as in the sample run, but yu have not provided any sample run screenshot, so i have printed the output in my own way , if it's different than yours, please comment what was in the sample run, i'll help you then.

Question 9 is just the repetition of the above steps, just for different datatype , so we have done accordingly,

Question 10 we have to print the float result with precision value to 2, means after decimal we want only 2 digits , and precision value to 4 in case of double, means 4 digits after the decimal value in the result, so we can set precision of decimal values by using %0.2f in case of float and %0.4lf in case of double here.

You can get more clarity when you go through the code , i have inserted enough comments to help you understand each line.

NOTE: I have used online c compiler.

Code:

#include <stdio.h>

int main()
/*question number 1*/
{ int x; // creating first integer variable
int y; // creating second integer variable
int z; // creating third integer variable
x = 0; // initialising x to zero(first integer)
y = 0; // initialising y to zero(second integer)
z = 0; // initialising z to zero(third integer)
  
/*question number 2*/
float p; // creating first float variable
float q; // creating second float variable
float r; // creating third float variable
p = 0; // initialising p to zero(first float)
q = 0; // initialising q to zero(second float)
r = 0; // initialising r to zero(third float)
  
/*question number 3*/
double a; // creating first double variable
double b; // creating second double variable
double c; // creating third double variable
a = 0; // initialising a to zero(first double)
b = 0; // initialising b to zero(second double)
c = 0; // initialising c to zero(third double)
  
/*question number 4*/
char ch; // creating char variable named ch
ch = " "; // initialising char ch to space character
  
/*question number 5*/
printf("Enter the new value of first integer :"); // printing message for prompting
scanf("%d",&x); // taking user input using scanf
  
/*question number 6*/
printf("Enter the new value of second integer :"); // printing message for prompting
scanf("%d",&y); // taking user input using scanf
  
/*question number 7*/
z = x + y; // adding variable x and y and storing the sum into z
  
/*question number 8*/
printf("sum of %d and %d is equal to %d \n",x,y,z); // output the value of third variable z
  
/*question number 9*/
z = x - y; // subtracting y from x and storing the result in z
printf("Difference of %d and %d is equal to %d \n",x,y,z); // output the value subtraction
  
z = x * y; // multiplication of x and y storing result in z
printf("Product of %d and %d is equal to %d \n",x,y,z); // output the Product value
  
z = x / y; // division of a and y storing result in z
printf("Division of %d and %d is equal to %d \n\n",x,y,z); // output the integer quotient value

  
/*question number 10*/
  
// **************************for float***********************
  
/*repeating question 5*/
printf("Enter the new value of first float :"); // printing message for prompting
scanf("%f",&p); // taking user input using scanf
  
/*repeating question 6*/
printf("Enter the new value of second integer :"); // printing message for prompting
scanf("%f",&q); // taking user input using scanf
  
/*repeating question 7*/
r = p + q; // adding variable p and q and storing the sum into r
  
/*question number 8*/
printf("sum of %f and %f is equal to %0.2f \n",p,q,r); // output the value of third variable r
  
/*question number 9*/
r = p - q; // subtracting q from p and storing the result in r
printf("Difference of %f and %f is equal to %0.2f \n",p,q,r); // output the subtraction value(using %0.2f for precision upto 2 digit after decimal)
  
r = p * q; // multiplication of p and q storing result in r
printf("Product of %f and %f is equal to %0.2f \n",p,q,r); // output the Product value(using %0.2f for precision upto 2 digit after decimal)
  
r = p / q; // division of p and q storing result in r
printf("Division of %f and %f is equal to %0.2f \n\n",p,q,r); // output the integer quotient (using %0.2f for precision upto 2 digit after decimal)
  
  
  
// ***********************for double***************************
  
  
  
/*repeating question 5*/
printf("Enter the new value of first double :"); // printing message for prompting
scanf("%lf",&a); // taking user input using scanf
  
/*repeating question 6*/
printf("Enter the new value of second double :"); // printing message for prompting
scanf("%lf",&b); // taking user input using scanf
  
/*repeating question 7*/
c = a + b; // adding variable a and b and storing the sum into c
  
/*question number 8*/
printf("sum of %lf and %lf is equal to %0.4lf \n",a,b,c); // output the value of third variable c
  
/*question number 9*/
c = a - b; // subtracting q from p and storing the result in r
printf("Difference of %lf and %lf is equal to %0.4lf \n",a,b,c); // output the subtraction value(using %0.4f for precision upto 4 digit after decimal)
  
c = a * b; // multiplication of p and q storing result in r
printf("Product of %lf and %lf is equal to %0.4lf \n",a,b,c); // output the Product value(using %0.4f for precision upto 4 digit after decimal)
  
a = b / c; // division of p and q storing result in r
printf("Division of %lf and %lf is equal to %0.4lf \n",a,b,c); // output the integer quotient (using %0.4f for precision upto 4 digit after decimal)

  
return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
I'm using code blocks to perform this. These problems are from my C Programming Beginners book....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming Complete the following function by writing C statements that correspond to the description in...

    C Programming Complete the following function by writing C statements that correspond to the description in each comment in the code.             #include <stdio.h> intmain() { // Declare a floating point variable to store a rectangle’s width // Declare a floating point variable to store a rectangle’s length // Declare a third floating point variable that will store the area of the rectangle. // Prompt the user to enter a width for the rectangle. // Get the user’s input, and store...

  • Please code in c 1. Write a program which does the following: Declare and initialize three...

    Please code in c 1. Write a program which does the following: Declare and initialize three pointers. One points to an integer, one points to a float and one points to a character variable Ask the user to enter appropriate values for these variables. Output the values to the screen by accessing the variables directly and then by using pointer references. Print the address of each variable. Modify the variables by performing any arithmetic operation only using pointer refer- ences...

  • Help. In language c View Layout References Mailings Program 1 Write a simple invoice generating program...

    Help. In language c View Layout References Mailings Program 1 Write a simple invoice generating program for a pet store as follows: 1- Ask the user for tee tax rate and store it in a float variable. 2- Ask the user for the number of cats being purchased and store the value in an integer variable. 3- Ask the user for the price of one cat and store it in a float variable. 4- Ask the user for the number...

  • Create a program using Visual Studio 2017 called dataExercise.c that declares the following variables and displays...

    Create a program using Visual Studio 2017 called dataExercise.c that declares the following variables and displays their values: 1. Declare a character variable with value 'a', and display the character using printf with a %c format. Then add a second printf that displays the character with a %d format. 2. Declare a short int variable with a value set to the maximum value of a short int (the maximum value for whatever machine I happen to use to grade your...

  • Please help me to make this c programming code!! Thank you!! Concatenation of two strings using...

    Please help me to make this c programming code!! Thank you!! Concatenation of two strings using pointers Step 1: Ask the user to input ni,n2 and create two character arrays (strings) with ni, n2 size using malloc. Step 2: create a 3rd array using malloc of size(n1+n2). Step 3: Ask the user to input String1 (of size nl) Step 4: Ask the user to input String2 (of size n2) Step 5: concatenate the two arrays and store them in String3...

  • 8). Name five of the fundamental ter ns which encompass object-oriented programming 9). Write a class...

    8). Name five of the fundamental ter ns which encompass object-oriented programming 9). Write a class called NumberOfGoals that represents the total number of goals scored by a ootball team. The NumberOfGioals class should contain a single integer as data, representing the number of goals scored. Write a constructor to initialize the number of goals to Zero. 10). Write a set of instructions to prompt the user for an int value and input it using the Scanner class into the...

  • Using Unix/Linux Command line in terminal and C programming. (b) Write a basic C program contained...

    Using Unix/Linux Command line in terminal and C programming. (b) Write a basic C program contained in a1q4.c and perform the following tasks in your program’s main() function: (8 marks) • Declare and initialize at least one variable of these types: char, int, unsigned int, float, double and long double. When initializing your variables, ensure the constants you use are of the same type as your variable. See the “Constants” section of Topic 7 for more information. • Use printf(3)...

  • I'm falling behind on my hw and I can really use a hand with using structures...

    I'm falling behind on my hw and I can really use a hand with using structures in C,++ C++ Object Oriented Programming Programming Assignment #4 Structures Create a structure representing a student. The member variables should include student name, student ID, and four test grades for the student (an array of four grades as a member variable). Prompt the user to enter the name, ID and the four test results. Store all the data in the structure. Calculate the average...

  • This is java and make simple program. CPSC 1100. Thanks CSPS 1100 Lab 4 ay total...

    This is java and make simple program. CPSC 1100. Thanks CSPS 1100 Lab 4 ay total Also I would need a new name for the daubles. An example here might be double tRtalD-caradd xD, YD) 1. (a) We are going to begin with some simple math, reading input, and formatted output. Create a class called MuCalsustec Yau will not have an instance variable. Since you have no instance variables to initialize, you do nat need a constructar. Do NOT create...

  • Computer Architecture Project Description: farh-to-cel.asm : # Revised and added code taken from Hennesey and Patterson...

    Computer Architecture Project Description: farh-to-cel.asm : # Revised and added code taken from Hennesey and Patterson 5th edition # to work with this simulator. # # Prompts a user to enter a Fahrenheit temperature as a floating point. # Displays the converted temperature in Celcius. # 10/28/2015 .data const5: .float 5.0 # store a floating point constant 5.0 const9: .float 9.0 const32: .float 32.0 .align 2 # align the next string on a word boundary .space 100 prompt: .asciiz "Please...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT