#include<stdio.h>
main()
{
int m1, m2, m3, m4, m5, per;
printf("Enter marks in five subjects");
scanf("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5);
per=(m1+m2+m3+m4+m5)/500*100;
if(per>=60)
 printf("First division");
  else{
if(per>=50)
  printf("Second division");
  else{
if(per>=40)
  printf("Third division");
  else
  printf("Fail");
}

Enter marks in five subjects 100, 100, 10, 50, 60
After giving this input printf showing Fail. But it will be First Division.
What is the problem in this program?

Hi sdtechi welcome at Daniweb. :)
Line 10 and line 13 show starting brackets {. Where are the ending brackets } ?

commented: Yes i missed the two ending braces..but the problem not because of baces it is in the per operation +0

hey sdtechi !!!!
There are like hundreds methods to do this program....ok?

As far as i m able to get ur method there will be one thing i would suggest to declare the per as float .

secondly, first multiply by 100 and then divide it by 500 as >>
per=(m1+m2+m3+m4+m5)*100/500;
because if u divide first by 500 then it will for sure result in some number less than 1 [As u have mentioned here [(100+100+10+50+60 = 320) and 320/500 = 0.64 ]] and it will due to type conversion convert into 0 (integer) and then multiplying it later by 100 wont matter.it will always result in 0 and hence result in 'fail'.

and that { braces are not proper as mentioned already by @ddanbe :)

i also attaching the corrected codes.
Hope it help.
and plz tell me if my english was not clear or proper .Its not my first language. :)

#include<stdio.h>
    main()
    {
    int m1, m2, m3, m4, m5;
    float per;
    printf("Enter marks in five subjects : ");
    scanf("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5);
    per=(m1+m2+m3+m4+m5)*100/500;
    if(per>=60)
     printf("First division");
      else{
    if(per>=50)
      printf("Second division");
      else{
    if(per>=40)
      printf("Third division");
      else
      printf("Fail");}}
    }
commented: Yes Smn its now working +0

Good tip about multiplying and dividing.
But the bracket style, still needs improvement:

double per = (100 + 100 + 10 + 50 + 60) * 100 / 500;
            if (per >= 60)
                Console.WriteLine("First division");
            else
            {
                if (per >= 50)
                    Console.WriteLine("Second division");
                else
                {
                    if (per >= 40)
                        Console.WriteLine("Third division");
                    else
                        Console.WriteLine("Fail");
                }
            }

For a few lines more you gain a lot of clarity and you will make lesser mistakes.
The benefits of that will become clear if you are debugging more than 1000 lines of code in search for a forgotten bracket... :)

commented: structured one +0

This line: per=(m1+m2+m3+m4+m5)/500*100; means that per is equal to the sum of the inputs divided by 50000. It that your intention? Or is it to divide the sum by 500 and then multiply that result by 100? If so, then the expression should be (since C/C++ treat multiply and divide operators with equal prescidence) per=((m1+m2+m3+m4+m5)/500)*100;. You need the parentheses to force the order of evaluation.

Thank you all

I appreciate it that you give a comment on why you neg repped me. :)
Change the Console.WriteLine in printf and it will be perfect C code.
I was just pointing out that bracketing can make things much clearer and less errorprone. Perhaps mark this thread as solved?

commented: ok..got it..thanks +0

@sdtelchi - which is evaluated in what order when the operator precidence is the same is up to the compiler. This can vary - I have experienced this when using compilers other than GCC. I think my post was worth a comment - not a down vote! In any case, it is definitely a situation where caveat programmer... Always state it the way you mean it. After all, your intention could have just as easily been this: per=((m1+m2+m3+m4+m5)/(500*100); - totally different result. Remember, some compilers parse left to right, and others right to left. This is why the parens are so important. MAKE NO ASSUMPTIONS!

commented: So true. +15
commented: Yes you true rubberman..thanks +0

Thank you friends (ddanbe, rubberman, Smn) for helping me and clearing my confusion.

#include <stdio.h>
#define MAX 500

main()
{
    float a, per, sum = 0;
    int i;
    char *pos[] = {"1st", "2nd", "3rd", "4th", "5th"};
    for (i = 0; i < 5; i++)
    {
        printf("Enter marks of %s subject: ", pos[i]);
        scanf("%f", &a);
        sum += a;
    }
    per = (sum/MAX)*100;
    printf("Your average is %.2f and ", per);
    if (per >= 60)
    {
        printf("you got First Division");
    }
    else if (per >= 50)
    {
        printf("you got Second Division");
    }
    else if (per >= 40)
    {
        printf("you got Third Division");
    }
    else
    {
        printf("you are Failed!");
    }
    putchar('\n');
    getchar();
    return 0;
}
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.