My program is not working when I use scanf function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

My program is not working when I use scanf function

Sololearn Code: #include <stdio.h> /* declaration */ int square (int num); int main() { int x, result; x = 5; result = square(x); printf("%d squared is %d\n", x, result); return 0; } /* definition */ int square (int num) { int y; y = num * num; return(y); } Modified by me using scanf() funcation #include<stdio.h> //Declaration// int square (int num); int main() { int x; int result; result= square (x) ; scanf("%d", &x); printf ("%d Square is %d\n", x, result); return 0; } // Declearation // int square (int num) { int y; y= num * num; return (y); }

7th Aug 2020, 12:42 PM
Rahul Khandekar
Rahul Khandekar - avatar
3 Answers
+ 4
That's because you are trying to calculate square of the number before inputting that number. Here's the fix👇 https://code.sololearn.com/cqbUXlQAwZ23/?ref=app
7th Aug 2020, 12:45 PM
Arsenic
Arsenic - avatar
+ 3
You are using scanf after calling the function ,😐 int=square(x) ,
7th Aug 2020, 12:45 PM
Abhay
Abhay - avatar
+ 1
Thank you both of you
7th Aug 2020, 4:38 PM
Rahul Khandekar
Rahul Khandekar - avatar