Program that will Arrange Three Numbers in Descending Order

Can you please help me in my problem.?
The problem is "error else without a previous if" in line 33 and 46

here is my code!
#include<stdio.h>
main()
{
int a,b,c;
int temp1,temp2,temp3;
printf("Enter No. 1: ");
scanf("%d", &a);
printf("Enter No. 2: ");
scanf("%d", &b);
printf("Enter No. 3: ");
scanf("%d", &c);
if(a<b)
{
if(a<c)
temp1=a;
{
if(b<c)
temp2=b;
temp3=c;
printf("\nThe Descending Value are:\n%d\n%d\n%d",temp1,temp2,temp3);
}
}
else
{
temp1=a;
{
if(b<c)
temp2=c;
temp3=b;
printf("\nThe Descending Value are:\n%d\n%d\n%d",temp2,temp1,temp3);
}
}
else
{
if(a<c)
temp1=a;
{
if(b<c)
{
temp2=b;
temp3=c;
printf("\nThe Descending Value are:\n%d\n%d\n%d",temp3,temp1,temp2);
}
}
}
else
{
temp2=c;
temp3=b;
printf("\nThe Descending Value are:\n%d\n%d\n%d",temp3,temp2,temp1);
}
}
First: please use code tags and indentation. They make code easier to read.
See http://www.cplusplus.com/articles/jEywvCM9/

Here is your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stdio.h>
main() // Note: standard expects return type of int
{
  int a,b,c;
  int temp1,temp2,temp3;
  printf("Enter No. 1: ");
  scanf("%d", &a);
  printf("Enter No. 2: ");
  scanf("%d", &b);
  printf("Enter No. 3: ");
  scanf("%d", &c);
  if(a<b)
    {
      if(a<c)
        temp1=a;

      {
        if(b<c)
          temp2=b;

        temp3=c;
        printf("\nThe Descending Value are:\n%d\n%d\n%d",temp1,temp2,temp3);
      }
    }
  else
    {
      temp1=a;
      {
        if(b<c)
          temp2=c;

        temp3=b;
        printf("\nThe Descending Value are:\n%d\n%d\n%d",temp2,temp1,temp3);
      }
    }
  else
    {
      if(a<c)
        temp1=a;

      {
        if(b<c)
          {
            temp2=b;
            temp3=c;
            printf("\nThe Descending Value are:\n%d\n%d\n%d",temp3,temp1,temp2);
          }
      }
    }
  else
    {
      temp2=c;
      temp3=b;
      printf("\nThe Descending Value are:\n%d\n%d\n%d",temp3,temp2,temp1);
    }
}

What the computer sees in that (when leaving out "irrelevant lines"), is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  if(a<b)
    {
    }
  else
    {
    }

  else // error, no 'if' before this 'else'
    {
    }

  else // error, no 'if' before this 'else'
    {
    }

Where do those syntax errors come from? Most likely from within the "irrelevant lines". Scoping rules.

Your code, one more time, but with different style. It contains exactly the same errors as the original:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<stdio.h>
main()
{
  int a,b,c;
  int temp1,temp2,temp3;
  printf("Enter No. 1: ");
  scanf("%d", &a);
  printf("Enter No. 2: ");
  scanf("%d", &b);
  printf("Enter No. 3: ");
  scanf("%d", &c);
  if(a<b)
    {
      if(a<c)
        {
          temp1=a;
        }

      if(b<c)
        {
          temp2=b;
        }

      temp3=c;
      printf("\nThe Descending Value are:\n%d\n%d\n%d",temp1,temp2,temp3);
    }
  else
    {
      temp1=a;

      if(b<c)
        {
          temp2=c;
        }

      temp3=b;
      printf("\nThe Descending Value are:\n%d\n%d\n%d",temp2,temp1,temp3);
    }

  else
    {
      if(a<c)
        {
          temp1=a;
        }

      if(b<c)
        {
          temp2=b;
          temp3=c;
          printf("\nThe Descending Value are:\n%d\n%d\n%d",temp3,temp1,temp2);
        }
    }

  else
    {
      temp2=c;
      temp3=b;
      printf("\nThe Descending Value are:\n%d\n%d\n%d",temp3,temp2,temp1);
    }
}

Do you see logical errors in that? You should (learn to see).
Last edited on
Topic archived. No new replies allowed.