Write a C program to get float datatype as input and print the output as integer datatype. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a C program to get float datatype as input and print the output as integer datatype.

I tried this code but I guess I am wrong 😒😣!!! #include <stdio.h> #include <conio.h> void main() { float a; int b; b = (int)a; printf("Enter a float number"); scanf("%f",&b); printf(b); return 0; }

6th May 2021, 5:56 AM
Damayanti sarkar
Damayanti sarkar - avatar
4 Answers
+ 4
This is because after assigning type casted value "a" to to integer variable "b", you are again telling compiler to cast "b" to type float by using "%f" format specifier. Here is the fix 👇 https://code.sololearn.com/ceQKfNCo578U/?ref=app P.S. in the fix, I have removed excess or non-standard code from your original program.
6th May 2021, 6:23 AM
Arsenic
Arsenic - avatar
+ 1
I think you should use "a" in scanf instead of "b" and then typecast it to int
6th May 2021, 5:59 AM
Michal Doruch
+ 1
Thank you so much Arsenic. It was of great help. 🤗
6th May 2021, 6:47 AM
Damayanti sarkar
Damayanti sarkar - avatar
0
I tried Name the way you told:- #include <stdio.h> #include <conio.h> void main() { float a; int b; b = (int)a; printf("Enter a float number"); scanf("%f",&a); printf("Value in integer: %f\n",&a); return 0; } But I am getting the value as zero after inputting any float value.
6th May 2021, 6:13 AM
Damayanti sarkar
Damayanti sarkar - avatar