write a Java program to find the second greatest number in the given three numbers
Answers
Answer:
public class SecondLargestInArrayExample{
public static int getSecondLargest(int[] a, int total){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[total-2];
}
public static void main(String args[]){
int a[]={1,2,5,6,3,2};
int b[]={44,66,99,77,33,22,55};
System.out.println("Second Largest: "+getSecondLargest(a,6));
System.out.println("Second Largest: "+getSecondLargest(b,7));
}}
Answer:
#include <stdio.h>
int find_second(int a, int b, int c)
{
if (a > b && b > c)
{
return b;
}
else if (a > c && c > b)
{
return c;
}
else
{
return -1;
}
}
int main(void)
{
int a, b, c, second1, second2, second3;
scanf("%d %d %d", &a, &b, &c);
second1 = find_second(a, b, c);
second2 = find_second(b, c, a);
second3 = find_second(c, a, b);
if (second1 != -1)
{
printf("%d\n", second1);
}
else if (second2 != -1)
{
printf("%d\n", second2);
}
else if (second3 != -1)
{
printf("%d\n", second3);
}
return 0;
}