Menu

Read a number from the user and Display sum of odd numbers upto n;


import java.util.*;
class Sum_of_odd_numbers_upto_n{
public static void main(String args[]){

System.out.print("Upto which number, you want to add add odd numbers?");
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int sum=0;
if(n>=1)
{

for(int i=1; i<=n;i++)
{

if(i%2!=0)
sum=sum+i;
}

}
System.out.println("The sum of odd numbers upto " +n+" is "+sum);


}}