Computer Applications

Write three different programs using for, while, and do-while loops to find the product of series 3, 9, 12,… 30.

Java Iterative Stmts

2 Likes

Answer

for loop

public class KBoatSeries
{
    public static void main(String args[]) {
        int pro = 1;
        for(int i = 3, t = 1; i <= 30; t++) { 
            pro *= i;
            if(t % 2 == 0)
                i += 3;
            else
                i += 6;
        }
        System.out.println("Product = " + pro);
    }
}
Output
Write three different programs using for, while, and do-while loops to find the product of series 3, 9, 12,... 30. Logix Computer Applications ICSE Class 10 Solutions.

while loop

public class KBoatSeries
{
    public static void main(String args[]) {
        long pro = 1;  
        int num = 3, t = 1;
        
        while(num <= 30)    {
            pro *= num;
            if(t % 2 == 0)
                num += 3;
            else
                num += 6;
            t++;
        }            
        System.out.println("Product = " + pro);
    }
}
Output
Write three different programs using for, while, and do-while loops to find the product of series 3, 9, 12,... 30. Logix Computer Applications ICSE Class 10 Solutions.

do-while loop

public class KBoatSeries
{
    public static void main(String args[]) {
        long pro = 1;  
        int num = 3, t = 1;
        
        do {
            pro *= num;
            if(t % 2 == 0)
                num += 3;
            else
                num += 6;
            t++;
        } while(num <= 30);
                    
        System.out.println("Product = " + pro);
    }
}
Output
Write three different programs using for, while, and do-while loops to find the product of series 3, 9, 12,... 30. Logix Computer Applications ICSE Class 10 Solutions.

Answered By

3 Likes


Related Questions