Programming Tutorials

C Program to Read a Set of Real Numbers from Keyboard & Find the Maximum

Pinterest LinkedIn Tumblr

To write a C program to read a set of real numbers from the keyboard and find the maximum among them, you can use a function that takes an array of real numbers and their size as arguments and return the maximum.

Using this function you can write a program to read a set of real numbers from the keyboard and find the maximum number in the array.

C Program to Read a Set of Real Numbers and Find the Maximum

  1. Define a function max()
  2. Under the main() function, declare two integers i and n.
  3. Declare an array a.
  4. Prompt the message to the user to insert how many elements they want to enter using printf() and allow to enter using scanf().
  5. Prompt the message and allow it to enter the elements.
  6. Find out the maximum number among them using the max() function.
  7. Print the maximum number along with the message.

Code:

#include<stdio.h>
#include<conio.h>

max(float a[], int n);
void main()
{
int i,n;
float a[100];
 printf("n How many elements you want to enter:n");
 scanf("%d",&n);
 printf("n Enter the elements:");
 for(i=0;i<n;i++)
 scanf("%f",&a[i]);
 max(a,n);
 getch();
}
max(float a[], int n)
{
 int i;
 float k, large;
 large=a[0];
 for (i=1;i<n;i++)
 {
  if (a[i]>large)
  {
   k=a[i];
   a[i]=large;
   large=k;
  }
 }
 printf("Largests element is :       %f", large);
return 0;
}
Author

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 8 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Comments are closed.