Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Enter, save and compile the following program to be called ex 2.c #include /*

ID: 3876566 • Letter: 2

Question

2. Enter, save and compile the following program to be called ex 2.c #include /* enter your own name here/ EGN 3211 ex-2: integer, floating point and mixed arithmetic / main() ( rt num1 = 7, num2 = 2, num 3-4. num; float d1 = 70, d2 = 2.0, d3 = 4.0, d; num1 d=d1/d2 pritf(B (default floating) 96f divided by %fis: %rn", d 1, d2, d). printf( (formatted floating) % 21f dvided by 96.2t is. % 1lfn. d1 d2 d. d = num 1 / d2. printf("D (mixed mode to float) %d divided by % 1 Itis % tinn", num 1, d2, d) num num1/d2 printf('E (maxed mode to integer) %d divided by % 1lfis %defin", num 1, d2, numi, num = num1 % num2 printrF (integer) %d mod %d %dn", num1, num2, num); num num3 % num2 printf("G (integer) %d mod %d = %dn" num3, num2, num). num: num3 % num1 , printfC"H (integer) %d mod %d : %dn" num3, num 1, num).

Explanation / Answer

Output:

Hi,

copy the above program in a notepad and save it with the name ex_2.c and execute it.

The output will like the shown above.

The main motto of this program is to show the differences between int, float. And how to display a formatted floating point

comparing A and B

7/2 is 3.5 but if we use an integer in the division the result will be converted to an integer so 3.5 will be converted to 3

if the datatype is float the results will be floats

In a division, if we want the output to be exact with decimal points, there is no need that dividend should also be of float type. Both divisor and remainder variables should be of float type. That is what he is trying to say in C&D.

In B&C there is %2lf and %1lf is used. These are used to format a floating point means if it is %6.5 the decimal points will be displayed until 5 number only.

In E he is trying to format an int using the " lf " but it will not work of integer type so in the output ' lf ' will be printed after number 3 "3lf".

In F&G&H just integers % integers. the % operator is called modulo.

the modulo operator will give the remainder of any division

7= 2*3 +1 so remainder would be 1

4%2 is 0 because 2 can completely divide 4.4 = 2*2

and the last one 7 cannot divide 4. 4= 0*7+4 remainder is 4

#include <stdio.h>
/*enter your own name here*/
/*EGN 3211 ex-2: integer,floating point and mixed arithmetic */
main(){
int num1=7,num2=2,num3=4,num;
float d1=7.0,d2=2.0,d3=4.0,d;
printf("A(integer):%d divided by %d=%d ",num1,num2,num1/num2);
d=d1/d2;
printf("B(default floating):%f divided by %f is :%f ",d1,d2,d);
printf("C(formatted floating): % 2lf divided by % 2lf is :% 1lf ",d1,d2,d);
d=num1/d2;
printf("D(mixed mode to float): %d divided by % 1lf is :% 1lf ",num1,d2,d);
num=num1/d2;
printf("E(mixed mode to integer): %d divided by % 1lf is :%dlf ",num1,d2,num);
num=num1 % num2;
printf("F(integer): %d mode %d = %d ",num1,num2,num);
num=num3%num2;
printf("G(integer): %d mode %d = %d ",num3,num2,num);
num=num3%num1;
printf("H(integer): %d mode %d = %d ",num3,num1,num);
}

Output:

  A(integer):7 divided by 2=3  B(default floating):7.000000 divided by 2.000000 is :3.500000   C(formatted floating):  7.000000 divided by  2.000000 is : 3.500000  D(mixed mode to float): 7 divided by  2.000000 is : 3.500000  E(mixed mode to integer): 7 divided by  2.000000 is :3lf  F(integer): 7 mode 2 = 1  G(integer): 4 mode 2 = 0  H(integer): 4 mode 7 = 4  
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote