+ 1
Can some one help me with C# for loop Odd numbers
Display odd numbers from input to 0 https://code.sololearn.com/cx89eCtnkY5M/?ref=app
9 ответов
+ 2
I'll kicking myself bc I only had to change two things
using System;
public class Program
{
public static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
//your code goes here
for(int i = 1 ; i <= number; i++){ if(i%2 !=0){
Console.Write(quot;{i} ");
}
}
}
}
+ 5
At the moment, I see 3 issues:
1. The Console.WriteLine part needs to be inside the {} of the if-statement – we want to print output only when the condition is met.
2. =- 1 and -= 1 are different! For your loop, you need -= 1 because we need to change i
3. in the if-statement, we need to check the current integer – that is "i" and not "number"
(remove the "number =- 1" part)
+ 5
Do not replace "number" everywhere! Only in the if-statement.
In your loop, you always test if number %2 != 0. But "number" never changes! Actually, you want to tedt the current integer, the thing that changes in the loop: that's "i".
And then reconsider what you want to give as output. "i" would be the thing you test, so "i" would also be the output.
+ 1
Thank you, I got it sololearn showed me the solution
+ 1
Deneice Jackson you are trying to find all the odd numbers in a range of the number you inputted so say 35 or range of 35 from 0 to 35...
So number is inside the for loop as i <= number
i is the initializer of the loop as 0 or i = 0. The left hand is i++
for(int i = 0 ; i <= number ; i++)
{
Now as to the if statement you are comparing the initializer not the number itself which is the range of all numbers between 0 and number
if ((i % 2) != 0){
And then ofcourse printing to the screen the results.
Console.WriteLine(quot;{i}");
}
* Lisa I hope this helps
+ 1
Deneice Jackson though you got it via AI it is good to practice this further on your own as you will revisit this many times in practice and in real life situations.
0
I updated the code
using System;
public class Program
{
public static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
//your code goes here
for(int i = number ; i >= 0 ; i-=1){ if(number%2 !=0){
Console.WriteLine(number);
}
}
}
}
0
It gives me an error if I change number to i
the task is display all odd number from input number to 0
0
Sorry it still isn't working