#include <stdio.h>
int main(void){
int a,b,c;
printf("Enter the numbers ");
scanf("%d %d %d",&a,&b,&c); // Enter numbers
if(a>b){
if(a > c )
{ printf(" Highest number is a = %d",a);}
else
{ printf(" Highest number is c = %d",c);}
}
else
{
if(b > c )
{ printf( "highest number is b = %d",b);}
else
{printf(" highest number is c= %d",c);}
}
return 0;
}
#include <stdio.h>
int main(void){
int a,b,c;
printf("Enter the number ");
scanf("%d %d %d",&a,&b,&c); // Enter numbers
if(a>b && a>c){
printf("Greatest number is a: %d",a);
}else if(b>c && b>a){
printf("Greatest number is b: %d",b);
}else if (c>a && c>b){
printf("Greatest number is C: %d",c);
}
return 0;
}
Here’s an example to find the highest number in an array of floating-point numbers:
#include <stdio.h>
int main() {
float arr[] = {1.2, 3.5, 5.7, 2.8, 4.6};
int n = sizeof(arr) / sizeof(arr[0]);
float max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("The highest number is: %.2f\n", max);
return 0;
}
Encapsulate the logic to find the highest number in a function for reusability:
#include <stdio.h>
float findMax(float arr[], int n) {
float max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
int main() {
float arr[] = {3.1, 4.5, 2.9, 6.7, 5.5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("The highest number is: %.2f\n", findMax(arr, n));
return 0;
}
Allow the user to input the array and find the maximum value:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &arr[i]);
}
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("The highest number is: %d\n", max);
return 0;
}
22-03-2022 | |
Thanks so much for this detailed c program, it's a good journey |