2. Write a program to print the even numbers from 1 to n. And their squares, where n
is provided by the user.
Answers
Answered by
0
Explanation:
Before attempting any question try to make an algorithm for it.
In this question you have to take a limit as an input from user and then run a loop until that limit reached.After that in the loop try to find which number is even or odd.
I will tell you 3 ways to how to find even number.
1.Suppose first number is i=2.Now in if condition you can write if( (i/2)*2 == i) which gives you even number then print i*i.
2.Second method is a common method you can use modulus function like if( i%2==0) then print i*i.
3.Another method is using AND (&). To check number is even or not. If( (i & 1) ==0) then you can print i*i.
Answered by
5
- Input upper limit to print even number from user.
- Input upper limit to print even number from user. Run a loop from first even number i.e. 2 (in this case), that goes till n and increment the loop counter by 2 in each iteration.
- Input upper limit to print even number from user. Run a loop from first even number i.e. 2 (in this case), that goes till n and increment the loop counter by 2 in each iteration. Finally inside loop body print the value of i .
Similar questions