i have to write programme to find second largest number.just give me a hint. 1 MEDAL FOR SATISFACTORY HINT.
Which programming language?
@Harsha19111999 in c just hint only
Oh. If it is of two numbers, you can use the "if" function
@Harsha19111999 it is not two nos.it is '''n' number which is not known in advance.use willl choose how many no.will he enter.
Then, this program will help you #include <stdio.h> int main(){ int i,n; float arr[100]; printf("Enter total number of elements(1 to 100): "); scanf("%d",&n); printf("\n"); for(i=0;i<n;++i) /* Stores number entered by user. */ { printf("Enter Number %d: ",i+1); scanf("%f",&arr[i]); } for(i=1;i<n;++i) /* Loop to store largest number to arr[0] */ { if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/ arr[0]=arr[i]; } printf("Largest element = %.2f",arr[0]); return 0; }
@Harsha19111999 without using array.
How can you do it without an array? Where will you assign the values? Explain your question
Ok so without an array you need two variables. One to store the first number user inputs and another to store the highest overall. You would use a loop to get input and to check the two numbers at the same time, saving the highest...
Since you need the second highest overal, you need at least 3 variables: highest, second_highest, user_input. I think the best way would be to loop until you input an exit value. That will depend on the allowed range of numbers. A common exit number for a loop is -999. pseudo pseudo code: while user_input <> -999 do { input user_input; if user_input > highest then second_highest = highest and highest = user_input; if user_input > second_highest and user_input < highest then second_highest = user_input; }
g#include<stdio.h> main() { int n,m,i,max; printf("How many numbers(n) you going to enter:"); scanf("%d",&n); printf("Enter the numbers:"); scanf("%d",&m); max=m; for(i=2;i<=n;i++) { scanf("%d",&m); if(m>max) max=m; } printf("The Largest Number is %d",max); } Output: How many numbers(n) you going to enter:5 Enter the numbers: 25 410 362 5 56 The Largest Number is 410 With Using Arrays Program: #include<stdio.h> #include<conio.h> void main() { int max_num(int a[],int n); int max,i,n; int a[50]; clrscr(); printf("Enter n number:"); scanf("%d",&n); printf("Enter the numbers:"); for(i=0;i<n;i++) scanf("%d",&a[i]); max=max_num(a,n); printf("The largest number is %d",max); getch(); } int max_num(int a[],int n) { int i,m=0; for(i=0;i<n;i++) { if(a[i]>m) m=a[i]; } return m; } output: Enter n number:10 Enter the numbers: 123 456 789 963 852 147 5 56 600 753 The largest number is 963
this has one with arrays and one with out arrays
@danielgomez755 i wanted second largest,not largest.
Three variables: testNumber, secondLargest, largest if( testNumber > secondLargest ) { /* switch them */ if( secondLargest > largest ) /* switch them */ } printf( "%d", secondLargest );
Join our real-time social learning platform and learn together with your friends!