Answer to Question #331109 in Java | JSP | JSF for Kei

Question #331109

Write a java program to find the sum of all odd numbers between 1 to n - using do...while loop

1
Expert's answer
2022-04-20T03:45:00-0400



import java.util.Scanner;


class Main {
  public static void main(String[] args) {
  //Write a java program to find the sum of all odd numbers //between 1 to n - using do...while loop


  Scanner cin=new Scanner(System.in);
  System.out.print("Please enter n:");
  int n=cin.nextInt();
    int sum=0;
    //out summ all od numbers using do[]while
    //n--;
    do
      {
        n--;
        if(n==1)
          break;
        if(n%2==1)//if current num is odd
        sum+=n;
      }while(n!=1);
    System.out.println("Sum al odd numbers:"+sum);
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS