Hacker Rank C Challenges #001
Write a function int max_of_four(int a, int b, int c, int d)
which reads four arguments and returns the greatest of them.
here is the code I programmed
#include <iostream>
#include <cstdio>
using namespace std;
/*
Add `int max_of_four(int a, int b, int c, int d)` here.
*/
int max_of_four(int a ,int b ,int c,int d){
int max=0;
int i;
for (i=1 ; i<4 ; i++){
if (max < a) {
max = a;
}
if (max < b) {
max = b;
}
if (max < c) {
max = c;
}
if (max < d) {
max = d;
}
}
return max;
}
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);
return 0;
}
for further details :-
For Loop
When you know exactly how many times you want to loop through a block of code,use the for loop instead of a while loop:
Syntax
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
ex :
int i, j;
// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i); // Executes 2 times
// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i); // Executes 2 times
// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
}
Comments
Post a Comment