Find output of C programs (floating point)

Here, you will find, C programs with outputs and explanations based on floating point numbers their assignment, comparisons etc.
Submitted by Amit Shukla, on June 20, 2017

1) Select the correct floating point data type in C programming?

  1. float, double, int
  2. bool, double, long int
  3. long double, double, float
  4. long int, float

Output

C) long double, double, float

Explanation

In options A, B, C int, long int, bool functions present and these function store int values. Only in option C all the data types can contains floating point variables. You can also refer the following program:

The following program proofs that in C programming float and double datatype can contains float point variable and int data type cannot contains floating data type.

#include<stdio.h>
int main()
{
	float a;
	double b;
	int i;

	printf("Enter float value : ");
	scanf("%f",&a);	
	printf("Float value : %f\n",a);

	printf("Enter double value : ");
	scanf("%lf",&b);
	printf("Double value : %lf\n",b);

	printf("Enter int value : ");
	scanf("%d",&i);
	printf("Int value : %d\n",i);

	return 0;
}
Enter float value : 150.55
Float value : 150.550000
Enter double value : 150.55
Double value : 150.550000
Enter intvalue : 150.55
Intvalue : 150

2) How can we store 5.95 as a float?

  1. use 5.95f
  2. use 5.95
  3. use 5.95ld
  4. none of the above.

Output

A) use 5.95f

Explanation

In case of C and C++ programming 5.95 treat as double value by default. To convert double value into float value we have to use ‘f’ at the end of value. To specify 5.95 as long double we have to use character ‘L’ at the end of value.


3) Which of the following is range of float?

  1. -3.4E+37 to +3.4E+37
  2. -3.5E+37 to +3.5E+37
  3. -3.5E+38 to +3.5E+38
  4. -3.4E+38 to +3.4E+38

Output

D) -3.4E+38 to +3.4E+38

Explanation

The range of floating point primitive data type of size 32 bits is -3.4E+38 to +3.4E+38


4) How can we perform module operation (%) between two floating point variables 6.78 and 3.26?

  1. For floating point modulo is not define hence we cannot find modulo of floating variables.
  2. 6.78 % 3.26 is the result of modulus operation.
  3. int(6.78)%int(3.26) is the result of modulus operation.
  4. fmod(6.78,3.26) is the result of modulus operation.

Output

D) fmod(6.78,3.26) is the result of modulus operation.

Explanation

In C and C++ programming we cannot use modulus (%) operator directly to find modulus of any two floating point values or variables.

So there is a function fmod(floating point value 1, floating point value 2) using this function we can find the modulus of any two floating points variable. This function is available in header file <math.h>.

You can refer the following program for more detail:

#include <stdio.h>
#include <math.h>

int main()
{
	float a,b;
	printf("Enter 1st float value : ");
	scanf("%f",&a);
	printf("Enter 2nd float value : ");
	scanf("%f",&b);
	printf("Modulus of %f and %f is %f",a,b,fmod(a,b));
	
	return 0;
}
Enter 1st float value : 6.78
Enter 2nd float value : 3.26
Modulus of 6.780000 and 3.260000 is 0.260000

5) How can we round off any float variable f into a int value i?

  1. i=(int)(f+0.5)
  2. i=int(f+0.5)
  3. i=(int)x+0.5
  4. We cannot perform the given operation.

Output

A) i=(int)(f+0.5)

Explanation

In the given question we have to find out the round off value. It means replacing the floating number by nearest value that is approximately equal or smaller or greater to the given number.

You can refer the following program for more detail:

#include<stdio.h>

int main()
{
	float f;

	printf("Enter float value : ");
	scanf("%f",&f);

	int i = (int)(f + 0.5);
	printf ("Value after roundng off is %d\n",i);

	return 0;
}
Enter float value : 145.62
Value after roundng off is 146





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.