Sitemap

Challenge Code

3 min readOct 30, 2023

--

Greeting! Everyone:)
I am going to join you all on this website ad share my views based on coding. This platform excited me to share my journey with you all. I would share my coding language skills of C++ with you all at the moment. I hope you enjoy it!!

Press enter or click to view image in full size

In this programming code, you will get to know how to use for loop statement n the code. For loop works when we know a statement will iterate specific number of times, its starting and ending point. When you want to run a specific function or statement a number of times, one should use for loop statements.

The syntax of for loop is mentioned below:

for ( <expression_1> ; <expression_2> ; <expression_3> )
<statement>

In this syntax,

expression 1 decides the starting point of the loop

expression 2 is the condition which must be satisfied during the process

expression 3 is used for increment and decrement of any variable.

For example:-

for(int i = 0; i < 5; i++) {
...
}

This particular loop runs 5 times starting with n=0 and succeeding as n=1, n=2, n=3 and n=4.

Here we solve a particular solution also mentioned in HackerRank. One can solve it to practice implementation of for loop.

Task :

For each integer in the interval [a, b] (given as input) :

  • If 1 ≤ n ≤ 9 , then print the English representation of it in lowercase. That is “one” for 1 , “two” 2 for , and so on.
  • Else if n>9 and it is an even number, then print “even”.
  • Else if n>9 and it is an odd number, then print “odd”.

Input Format

Get Manyata’s stories in your inbox

Join Medium for free to get updates from this writer.

The first line contains an integer, a.
The second line contains an integer, b.

Constraints

1 ≤ a ≤ b ≤ 10⁶

Output Format

Print the appropriate English representation,even, or odd, based on the conditions described in the 'task' section.

Sample Input 1:

8
11

Sample Output 1:

eight
nine
even
odd

Sample Input 2:

3 15

Sample Output 2:

three
four
five
six
seven
eight
nine
even
odd
even
odd
even
odd

Solution:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

void number(int num1,int num2){
int n;

for(n=num1;n<=num2&&n<=9;n++){

if(n==1){
printf("one\n");
}
else if(n==2){
printf("two\n");
}
else if(n==3){
printf("three\n");
}
else if(n==4){
printf("four\n");
}
else if(n==5){
printf("five\n");
}
else if(n==6){
printf("six\n");
}
else if(n==7){
printf("seven\n");
}
else if(n==8){
printf("eight\n");
}
else if(n==9){
printf("nine\n");
}
}

}
void check(int num1,int num2){
int n;

for(n=10;n<=num2&&n>9;n++){
if(n%2==0){
printf("even");
}
else{
printf("odd");
}
printf("\n");
}

}
int main()
{
int a, b;
scanf("%d\n%d", &a, &b);
// Complete the code.
number(a,b);
check(a,b);

return 0;
}

Happy Learning! :)

--

--