Java program to Calculate Sum of Odd or Even numbers | Learn Coding
ฝัง
- เผยแพร่เมื่อ 29 ก.ย. 2024
- Java Full Course for Beginners...!👇👇
• Java Programming
Please Like | Share | SUBSCRIBE our Channel..!
Learn Coding
🙏🙏🙏
Like our Facebook Page...!
Learn Coding
Don't forget to tag our Channel...!
#AddOddEven
#LearnCoding
#ask4help
#JavaProgramSumOfOddEven
Instead of two for loops I used single common *for loop* as follows:
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
int n,sum=0;
System.out.print("Enter the range: ");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
for (int i=n; i>=0; i-=2)
{
sum=sum+i;
}if(n%2==0)
System.out.println("Sum of even numbers in the range: "+sum); else
System.out.println("Sum of odd numbers in given range: "+sum);
}
}
What if you don't want to use if else
Nice
Nicely explanationed
Pranam guruji...
My question 9 is not odd number but you write
Bhai ye ek hee for loop me mene kar diya .. for loop me if else liya aur if k liye evensum aur else k liye oddsum .... Iske wajah se program easy hogya hai jabki tumne jo liya hai usme i =i+2 woh unusual hai aur samjhna thoda mushkil hai
Bro why you use if else 🤷🏻♂️ if you had to manipulate for loop according to you 😮
Explaining in very easy language. Thank you so much....
can you make a video on-Given maximum of 100 digit numbers as input, find difference between sum of odd and even position
Sir ap jab copy ka video shoot karte hai to extra bahar ka background nahi show karta hai sir mujhe bhi please bataea kese karte hai sir please.
😊😊😊
full Spring boot padha do sir
Sir telegram link not working sir
Sir u r hero of coding sir
U r helping a lot sir
good job
Sir ap kha se h bihar ya jharkhand??
What if we have to do odd and even at a same time?
Than u have to remove only the following if and else condn from the above program and u r ready to go
If(n%2==0)
adavance cograts for 1m family
Thank you so much.
We can't increment +2 in for loop
We can, example- i=i+2