Q:

write a java program that computes the product of odd numbers that are in the range [a,b] where a and b are given by the user (without nesting the control statements)

0

write a java program that computes the product of odd numbers that are in the range [a,b] where a and b are given by the user (without nesting the control statements)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Scanner;
public class NewClass {
     public static void main(String args[]) throws Exception
    {
        Scanner input=new Scanner(System.in);
        System.out.println("enter the start numer of range : ");
        int a=input.nextInt();
        System.out.println("enter the end number of range : ");
        int b=input.nextInt();
        int i=a;
        int result=1;
        while(i<=b)
        {
            if(i%2!=0)//i is an odd number
                result=result*i;
            i++;
        }
        System.out.println("product of odd numbers that are in the range ["+a+","+b+"] is "+result);
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now