Monday, June 18, 2012

Write a program to find the greatest among ten numbers

// program in c++
#include <iostream>
using namespace std;

int main()
{
    int a[10];
    int i;                //int i is used in loop
    cout << "\n This program in C++";
    cout << "\n Enter your 10 number: ";
   
    //store 10 numbers in an array
    for (i=0; i<10; i++){
        cin >> a[i];
    }
   
    //assume that a[0] is greatest
    int greatest;
    greatest = a[0];
   
    for (i=0; i<10; i++){
        if (a[i] > greatest){
            greatest = a[i];
        }
    }
   
    cout << "\n Greatest of 10 number is: " << greatest << "\n\n";
    return 0;
}

-----------------------------------------------------------------------------------------------
// program in C
#include <stdio.h>
#include <conio.h>

int main()
{
    int a[10];
    printf("\n This program in C");
    printf("\n Enter your 10 number: ");
   
    //store 10 numbers in an array
    for (int i=0; i<10; i++){               //here we declare int i in loop
        scanf ("%d",&a[i]);
    }
   
    //assume that a[0] is greatest
    int greatest;
    greatest = a[0];
   
    for (int i=0; i<10; i++){     //here we again declare int i in loop, because
        if (a[i] > greatest){       //we haven't declare like previous program
        greatest = a[i];
        }
    }
   
    printf ("\n Greatest of 10 number is: %d \n\n", greatest);
    return 0;
}


OUTPUT:

No comments:

Post a Comment