# 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?**

- float, double, int
- bool, double, long int
- long double, double, float
- 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?**

- use 5.95f
- use 5.95
- use 5.95ld
- 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?**

- -3.4E+37 to +3.4E+37
- -3.5E+37 to +3.5E+37
- -3.5E+38 to +3.5E+38
- -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?**

- For floating point modulo is not define hence we cannot find modulo of floating variables.
- 6.78 % 3.26 is the result of modulus operation.
- int(6.78)%int(3.26) is the result of modulus operation.
- 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?**

- i=(int)(f+0.5)
- i=int(f+0.5)
- i=(int)x+0.5
- 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

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.