What is wrong in this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is wrong in this code?

#include<stdio.h> int main(int argc, char *argv[]) { float C_P,S_P; float profit=S_P-C_P; float profit percentage=(profit*100)/C_P; printf("क्रय मूल्य=₹"); scanf("%f",&C_P); printf("विक्रय मूल्य=₹"); scanf("%f",&S_P); if(S_P>C_P){ printf("लाभ=₹%.2f ",S_P-C_P);} if(C_P>S_P){ printf("हानि=₹%.2f",C_P-S_P);} if(S_P>C_P){ printf("लाभ प्रतिशत=%f",profit percentage);}}

5th Jan 2023, 10:29 AM
Yogesh Kumar
5 Answers
+ 2
An underscore between profit and percentage line 6. Space are not allowed in variable name
5th Jan 2023, 10:40 AM
Kenobi
Kenobi - avatar
+ 2
Yogesh Kumar the variables are not given an initial value before they are used. They will hold whatever random value happens to be in the memory location at run time. If C_P happens to get initialized with zero, then the program will start out with a division by zero error. The program is prompting for the values only AFTER the calculations are being performed. Understand that program lines are executed in sequence from top to bottom. 1. Declare the variables 2. Prompt for input values 3. Calculate 4. Print the results. (You have steps 2 and 3 swapped out of order)
5th Jan 2023, 12:40 PM
Brian
Brian - avatar
+ 1
Okay
5th Jan 2023, 11:08 AM
Yogesh Kumar
+ 1
Brian yeah that's True but it is not a problem to run the programm
5th Jan 2023, 12:42 PM
Kenobi
Kenobi - avatar
0
There are a few issues with this code: 1. profit and profit percentage are defined and calculated before the values for C_P and S_P are inputted by the user. Therefore, profit and profit percentage will always be equal to 0. You should move the definitions of these variables after the user inputs the values for C_P and S_P. 2. The if statement for calculating the profit percentage is incomplete. It is missing the opening curly brace { after the if statement. Following is correct code: #include<stdio.h> int main(int argc, char *argv[]) { float C_P, S_P; printf("क्रय मूल्य=₹"); scanf("%f", &C_P); printf("विक्रय मूल्य=₹"); scanf("%f", &S_P); if (S_P > C_P) { float profit = S_P - C_P; printf("लाभ=₹%.2f ", profit); float profit_percentage = (profit * 100) / C_P; printf("लाभ प्रतिशत=%f", profit_percentage); } if (C_P > S_P) { printf("हानि=₹%.2f", C_P - S_P); } }
6th Jan 2023, 6:54 PM
Aditya Dixit