- Written by: Rama Boya
The program finds the second largest element persistent in the given an array of integers.
For Example:
Sample Input: arr[] = {8, 1, 18, 9, 13, 5}
Sample Output: The second largest element is 13.
Explanation: The largest element of the array is 18 and the second largest element is 13
/* C Program to find Second largest Number in an Array */
#include <stdio.h>
#include <limits.h>
int main()
{
int arr[50], i, size, firstL, secondL;
printf("Please Enter the Number of elements in an array : \n");
scanf("%d", &size);
printf("Please Enter %d elements of an Array \n", size);
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
firstL = secondL = INT_MIN;
for (i = 0; i < size; i++)
{
if(arr[i] > firstL)
{
secondL = firstL;
firstL = arr[i];
}
else if(arr[i] > secondL && arr[i] < firstL)
{
secondL = arr[i];
}
}
printf("\nThe Largest Number in this Array = %d", firstL);
if (secondL == INT_MIN)
{
printf("The Second Largest Number does not exist.\n");
}
else
{
printf("\nThe Second Largest Number in this Array = %d", secondL);
}
return 0;
}
Test Case 1:
Please Enter the Number of elements in an array :
5
Please Enter 5 elements of an Array
10
14
25
4
8
The Largest Number in this Array = 25
The Second Largest Number in this Array = 14
Test Case 2:
Please Enter the Number of elements in an array :
3
Please Enter 3 elements of an Array
14
14
8
The Largest Number in this Array = 14
The Second Largest Number in this Array = 8
Test Case 3:
Please Enter the Number of elements in an array :
3
Please Enter 3 elements of an Array
5
3
3
The Largest Number in this Array = 5
The Second Largest Number in this Array = 3
Test Case 4:
Please Enter the Number of elements in an array :
4
Please Enter 4 elements of an Array
8
8
8
8
The Largest Number in this Array = 8
The Second Largest Number does not exist.
- Written by: RajaSekhar
Algorithm for to calculate monthly bill of newspaper
Algorithm:
- Read the number of days.
- Read the new paper cost.
- Read the Service charge.
- if there is a service charge, calculate price using service charge + (no of days* news paper cost)
- If there is no service charge calculate using, no of days* news paper cost
- print the total news paper bill
C Program to Calculate Monthly Newspaper bill
#include<stdio.h>
int main()
{
int no_of_days;
float paper_per_day_cost, total_bill, service_charge;
char service;
// Read the Number of days paper delivered
printf("Enter the number of days paper delivered:");
scanf("%d", &no_of_days);
//Number of days must be greater than 0 to calculate price
if(no_of_days <= 0 || no_of_days > 31 )
{
printf("No paper bill for the month or wrong data entered.");
}
else
{
printf("Enter the paper cost per day:");
scanf("%f", &paper_per_day_cost);
// Paper cost must be greater than zero
if(paper_per_day_cost<=0)
{
printf("Your paper bill is 0.");
}
else
{
// Service Charge is optional, add service charge if required
printf("Do you have service charge? Enter Y or N :");
scanf("%s", &service);
if(service=='Y' || service=='y')
{
printf("Enter Service Charge : ");
scanf("%f", &service_charge);
total_bill = service_charge + no_of_days * paper_per_day_cost;
}
else
{
total_bill = no_of_days * paper_per_day_cost;
}
printf("Total bill is : $%0.2f", total_bill);
}
}
return 0;
}
output
Enter the number of days paper delivered:30 Enter the paper cost per day:3.50 Do you have service charge? Enter Y or N :Y Enter Service Charge : 30 Total bill is: $135.00
Note: The currency is given in the program only at the last print statement.
- Fizz Buzz Implementation in C
- C program to Convert Number to Words
- GCD of Two Numbers Without using LCM in C
- C Program to Find Nth Fibonacci Number Using Recursion
- C Program for Multiplication Table using Goto Statement
- C Program for Circumference of a Circle
- C Program for Fibonacci Series Using for Loop
- C Program to Print Pascal Traingle
- C Program to Print Pyramid of Numbers
- C Program for Fibonacci Series using While Loop
- C Program to Find Sum of Individual Digits of a Positive Integer Number
- C Program to Find Prime Factors of a Given Positive Number
- Bit Stuffing Program in C
- Implementation of Queue using Array in C
- Implementation of Stack Using Array in C