Write an algorithm to print the first 10 odd numbers.
Answers
Answer:
Basically the concept is;
1. Given is first 10 odd numbers.
2. Initialize a integer type variable with value equal to 1. say , int i=1;
3. Start a loop for the variable and run it twice the value of 10 = 20
4. Print the value initialised in step 2+ 2. e.g; i+2;
5. Update the value of i as i+=2.
Program :-
Java
class Odd Output
{ i i<=20 Output
void main() 1 true 1
{ 3 true 3
int i=1; . 5
for( i ; i<=20 ; i+=2) . .
{ 19 true 19
System.out.println(i); 21 false
}
}
}
Python
i=1
for i in range(20):
print(i)
i+=2
Answer:
Algorithm to print the first 10 odd numbers.
Explanation:
Step 1: Start
Step 2: Declare variable c of integer type
Step 3: Set z=0
Step 4: Repeat step 4.1 to 4.3 while (z<=10)
Step 4.1: if (z%2 != 0)
Step 4.2: then print c
Step 4.3 : z=z+1
Step 5: Stop
To analyze a mathematical problem or data step by step in such a way that it becomes accessible to the computer and the computer can present the appropriate solution to the mathematical problem by taking the available data in the program; is called an algorithm.
In Computer Programming, the algorithm is written before writing the program. If you are a student of Computer Sc, IT, BCA and MCA then you have to write a program. Like Check Whether the Number is Prime and Not? If you start writing this program without thinking, then you can probably see many errors in the program. You can reduce these errors if you make the algorithm first.
#SPJ2