Algorithm
Odd Numbers
Adapted by Neilor Tonin, URI Brazil
Read an integer value X (1 <= X <= 1000). Then print the odd numbers from 1 to X, each one in a line, including X if is the case.
Input
The input will be an integer value.
Output
Print all odd values between 1 and X, including X if is the case.
Input Sample | Output Sample |
8 |
1 |
Code Examples
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
int main()
{
int n;
scanf("%i", &n);
for (int i = 0; i < = n; ++i)
{
if(i % 2 == 1)
printf("%in", i);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
import java.util.Scanner;
public class Main {
public static void main(String[] args){
int X;
Scanner input =new Scanner(System.in);
X = input.nextInt();
System.out.print(1+"n");
for (int i = 1; i < X-1; i+=2) {
int oddNumber = i + 2;
System.out.print(oddNumber+"n");
}
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
numero = int(raw_input())
i = 1
while(i <= numero):
if(i%2 != 0):
print i
i = i + 1
Copy The Code &
Try With Live Editor
Input
Output