Q.1 Write a program to print first 20 odd numbers with the help of for loop?
Answers
Answered by
1
Explanation:
Wrote.,
In this example, we will write a Java program to display odd numbers from 1 to n which means if the value of n is 100 then the program will display the odd numbers between 1 and 100.
Program to print odd numbers from 1 to n where n is 100
In the following example we have provided the value of n as 100 so the program will print the odd numbers from 1 to 100.
The logic we are using in this program is that we are looping through integer values from 1 to n using for loop and we are checking each value whether the value%2 !=0 which means it is an odd number and we are displaying it. To understand this program, you should have the basic knowledge of for loop and if statement.
Answered by
0
Iɴ ᴛʜᴇ ғᴏʟʟᴏᴡɪɴɢ ᴇxᴀᴍᴘʟᴇ ᴡᴇ ʜᴀᴠᴇ ᴘʀᴏᴠɪᴅᴇᴅ ᴛʜᴇ ᴠᴀʟᴜᴇ ᴏғ ɴ ᴀs 100 sᴏ ᴛʜᴇ ᴘʀᴏɢʀᴀᴍ ᴡɪʟʟ ᴘʀɪɴᴛ ᴛʜᴇ ᴏᴅᴅ ɴᴜᴍʙᴇʀs ғʀᴏᴍ 1 ᴛᴏ 100.
Iɴ ᴛʜᴇ ғᴏʟʟᴏᴡɪɴɢ ᴇxᴀᴍᴘʟᴇ ᴡᴇ ʜᴀᴠᴇ ᴘʀᴏᴠɪᴅᴇᴅ ᴛʜᴇ ᴠᴀʟᴜᴇ ᴏғ ɴ ᴀs 100 sᴏ ᴛʜᴇ ᴘʀᴏɢʀᴀᴍ ᴡɪʟʟ ᴘʀɪɴᴛ ᴛʜᴇ ᴏᴅᴅ ɴᴜᴍʙᴇʀs ғʀᴏᴍ 1 ᴛᴏ 100.Tʜᴇ ʟᴏɢɪᴄ ᴡᴇ ᴀʀᴇ ᴜsɪɴɢ ɪɴ ᴛʜɪs ᴘʀᴏɢʀᴀᴍ ɪs ᴛʜᴀᴛ ᴡᴇ ᴀʀᴇ ʟᴏᴏᴘɪɴɢ ᴛʜʀᴏᴜɢʜ ɪɴᴛᴇɢᴇʀ ᴠᴀʟᴜᴇs ғʀᴏᴍ 1 ᴛᴏ ɴ ᴜsɪɴɢ ғᴏʀ ʟᴏᴏᴘ ᴀɴᴅ ᴡᴇ ᴀʀᴇ ᴄʜᴇᴄᴋɪɴɢ ᴇᴀᴄʜ ᴠᴀʟᴜᴇ ᴡʜᴇᴛʜᴇʀ ᴛʜᴇ ᴠᴀʟᴜᴇ%2 !=0 ᴡʜɪᴄʜ ᴍᴇᴀɴs ɪᴛ ɪs ᴀɴ ᴏᴅᴅ ɴᴜᴍʙᴇʀ ᴀɴᴅ ᴡᴇ ᴀʀᴇ ᴅɪsᴘʟᴀʏɪɴɢ ɪᴛ. Tᴏ ᴜɴᴅᴇʀsᴛᴀɴᴅ ᴛʜɪs ᴘʀᴏɢʀᴀᴍ, ʏᴏᴜ sʜᴏᴜʟᴅ ʜᴀᴠᴇ ᴛʜᴇ ʙᴀsɪᴄ ᴋɴᴏᴡʟᴇᴅɢᴇ ᴏғ ғᴏʀ ʟᴏᴏᴘ ᴀɴᴅ ɪғ sᴛᴀᴛᴇᴍᴇɴᴛ
Attachments:
Similar questions
Accountancy,
21 days ago
English,
21 days ago
Chemistry,
1 month ago
Political Science,
7 months ago
English,
7 months ago