October 8, 2013

C program to display integers in Ascending without using Loop

This program run sorts and display the integers in ascending order. It finds the largest and smallest integer values respectively.



#include "stdio.h"
#include "conio.h"


int main()



{


    int a=0, b=0, c=0,d=0,e=0, f=0, grade;
    printf("Enter four numbers\n");
   
    scanf("%d",&a);
    scanf("%d",&b);
    scanf("%d",&c);
    scanf("%d",&d);
   
    if(a>b && a>c && a>d)
    {
           printf("1st value that you entered is largest\n");
           e=1;
    }
    else if(b>a && b>c && b>d)
    {
           printf("2nd value that you entered is largest\n");
           e=2;
    }
    else if(c>a && c>b && c>d)
    {
           printf("3rd value that you entered is largest\n");   
           e=3;
    }
    else if(d>a && d>b && d>c)
    {
           printf("4th value that you entered is largest\n");
           e=4;
    }

// For finding smallest value

     if(a
    {
           printf("1st value that you entered is smallest\n");
           f=1;
    }
    else if(b
    {
           printf("2nd value that you entered is smallest\n");
           f=2;
    }
    else if(c
    {
           printf("3rd value that you entered is smallest\n");
           f=3;
    }
    else if(d
    {
           printf("4th value that you entered is smallest\n");
           f=4;
    }

//Sorting //

    if(e==1 && f==2)
    {
            if(c
            {
                   printf("Sorted order is \n %d %d %d %d", b,c , d, a);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", b,d , c, a);
            }
    }
    else
    if(e==1 && f==3)
    {
            if(b
            {
                   printf("Sorted order is \n %d %d %d %d", c,b , d, a);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", c,d , b, a);
            }
    }
    else
    if(e==1 && f==4)
    {
             if(b
            {
                   printf("Sorted order is \n %d %d %d %d", d,b , c, a);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,c , b, a);
            }
    }
   
    // for e=2
   
        if(e==2 && f==1) // a is smallest, b is largest
    {
            if(c
            {
                   printf("Sorted order is \n %d %d %d %d",a, c , d, b);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", a,d , c, b);
            }
    }
    else
    if(e==2 && f==3)// c is smallest
    {
            if(a
            {
                   printf("Sorted order is \n %d %d %d %d", c,a , d, b);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", c,d , a, b);
            }
    }
    else
    if(e==2 && f==4)  // d is smallest
    {
             if(a
            {
                   printf("Sorted order is \n %d %d %d %d", d,a , c, b);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,c , a, b);
            }
    }
   
    // for c is largest i.e.. e=3
            if(e==3 && f==1) // a is smallest, c is largest
    {
            if(b
            {
                   printf("Sorted order is \n %d %d %d %d",a, b , d, c);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", a,d , b, c);
            }
    }
    else
    if(e==3 && f==2)// b is smallest
    {
            if(a
            {
                   printf("Sorted order is \n %d %d %d %d", b,a , d, c);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", b,d , a, c);
            }
    }
    else
    if(e==3 && f==4)  // d is smallest
    {
             if(a
            {
                   printf("Sorted order is \n %d %d %d %d", d,a , b, c);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,b , a, c);
            }
    }   
   
       
    // for d is largest i.e.. e=4
   
   
   
            if(e==4 && f==1) // a is smallest
    {
            if(b
            {
                   printf("Sorted order is \n %d %d %d %d",a, b , c, d);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", a,c , b, d);
            }
    }
    else
    if(e==4 && f==2)// b is smallest
    {
            if(a
            {
                   printf("Sorted order is \n %d %d %d %d", b,a , c, d);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", b,c , a, d);
            }
    }
    else
    if(e==4 && f==3)  // d is smallest
    {
             if(a
            {
                   printf("Sorted order is \n %d %d %d %d", d,a , b, d);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,b , a, d);
            }
    }       

    getch();
    return 0;
   
   
}


Here is the output.


 

No comments:

Post a Comment

C program to Read From a File

#include <stdio.h> #include <stdlib.h> void main() {     FILE *fptr;     char filename[15];     char ch;   ...