0% found this document useful (0 votes)
36 views3 pages

C Style Find The Largest Number Among Three Numbers and Calculate The Sum of Natural Numbers

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

C Style Find The Largest Number Among Three Numbers and Calculate The Sum of Natural Numbers

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

// Find the Largest Number Among Three Numbers

// Using if Statement

#include <stdio.h>

int main() {

double n1, n2, n3;

printf("Enter three different numbers: ");


scanf("%lf %lf %lf", &n1, &n2, &n3);

// if n1 is greater than both n2 and n3, n1 is the largest


if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);

// if n2 is greater than both n1 and n3, n2 is the largest


if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);

// if n3 is greater than both n1 and n2, n3 is the largest


if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);

return 0;
}

// Using if...else Ladder

#include <stdio.h>

int main() {

double n1, n2, n3;

printf("Enter three numbers: ");


scanf("%lf %lf %lf", &n1, &n2, &n3);

// if n1 is greater than both n2 and n3, n1 is the largest


if (n1 >= n2 && n1 >= n3)
printf("%.2lf is the largest number.", n1);

// if n2 is greater than both n1 and n3, n2 is the largest


else if (n2 >= n1 && n2 >= n3)
printf("%.2lf is the largest number.", n2);

// if both above conditions are false, n3 is the largest


else
printf("%.2lf is the largest number.", n3);

return 0;
}

// Using Nested if...else

#include <stdio.h>

int main() {
double n1, n2, n3;

printf("Enter three numbers: ");


scanf("%lf %lf %lf", &n1, &n2, &n3);

// outer if statement
if (n1 >= n2) {

// inner if...else
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
}

// outer else statement


else {

// inner if...else
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}

return 0;
}

// Calculate the Sum of Natural Numbers


// Sum of Natural Numbers Using for Loop

#include <stdio.h>
int main() {
int n, i, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &n);

for (i = 1; i <= n; ++i) {


sum += i;
}

printf("Sum = %d", sum);


return 0;
}

// Sum of Natural Numbers Using while Loop

#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
i = 1;

while (i <= n) {
sum += i;
++i;
}
printf("Sum = %d", sum);
return 0;
}

// Read Input Until a Positive Integer is Entered

#include <stdio.h>
int main() {
int n, i, sum = 0;

do {
printf("Enter a positive integer: ");
scanf("%d", &n);
} while (n <= 0);

for (i = 1; i <= n; ++i) {


sum += i;
}

printf("Sum = %d", sum);


return 0;
}

You might also like