Is my code correct??? I would like to find the percentage like for example:

30pcs of 60pcs is 50%. But how do I code it??? I mean, how to code to get the PERCENTAGE... Is the formula:
(num/total) * 100 ??? Is this correct:

for(x=0;x<3;x++)
	{
		y=total[x];
		p[x]=((float)(y/grand))*100;
	}

I put 'y' variable to have the 'total[x]' value becuase the compiler will say that it is an error. I think it's like declaration syntax error... I forgot... Please help me.... And by the way, this is a TURBO C program...

Dani AI

Generated

asked whether the percentage formula and loop were correct, and correctly requested data types and a minimal compileable example. Two things usually cause the errors described when using Turbo C: integer division (casting after the division does not recover lost fractional bits) and older compilers that require all local variable declarations at the start of a block. Also confirm that the grand total is initialized and not zero.

A concise, correct pattern (using different identifiers than in the thread) is shown below. Note the cast to double happens before the division so the division is done in floating point, and variables are declared up front to suit older compilers.

#include <stdio.h>

int main(void) {
    int counts[3] = {30, 10, 20};
    int grandTotal = 60;
    double percent[3];
    int i;

    if (grandTotal == 0) return 1;

    for (i = 0; i < 3; i++) {
        percent[i] = (double)counts[i] * 100.0 / (double)grandTotal;
        printf("count=%d -> %.1f%%\n", counts[i], percent[i]);
    }
    return 0;
}

Practical notes: use double for better precision, or ensure at least one operand is a floating literal (100.0) so promotion happens; check for division by zero; and keep declarations at the top of the function/block with Turbo C (pre-C99). Following this pattern prevents integer-truncation and declaration-scope errors while producing the expected percentage values.

Recommended Answers

All 3 Replies

Is my code correct??? I would like to find the percentage like for example:

30pcs of 60pcs is 50%. But how do I code it??? I mean, how to code to get the PERCENTAGE... Is the formula:
(num/total) * 100 ??? Is this correct:

for(x=0;x<3;x++)
	{
		y=total[x];
		p[x]=((float)(y/grand))*100;
	}

I put 'y' variable to have the 'total[x]' value becuase the compiler will say that it is an error. I think it's like declaration syntax error... I forgot... Please help me.... And by the way, this is a TURBO C program...

One of the most important things to include with questions like these is the data types. A small snippet (say less than 100 lines) of compileable code that demonstrates the problems is always best.

OK thanks for the info... But is code correct?

>OK thanks for the info... But is code correct?

One of the most important things to include with questions like these is the data types. A small snippet (say less than 100 lines) of compileable code that demonstrates the problems is always best.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.