icse-promo

Question

Write a menu driven program to perform the following . (Use switch-case statement)

(a) To print the series 0, 3, 8, 15, 24 ā€¦ā€¦. n terms (value of ā€˜nā€™ is to be an input by the user).

(b) To find the sum of the series given below:
S = 1/2+ 3/4 + 5/6 + 7/8 ā€¦ 19/20

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;

public class program9
{
    public static void main(String[] args)
    {
        int choice=0,n=0,i=0,term=0;
        double sum=0,d=2;
        Scanner sc = new Scanner(System.in);
        System.out.println("enter 1 To Print 0, 3, 8, 15, 24... n tersm");
        System.out.println("enter 2 to print  Sum of series 1/4 + 3/4 + 7/8 + ... n terms");
        System.out.print("Enter your choice: ");
        choice = sc.nextInt();

        switch(choice) 
        {
            case 1:
            System.out.print("Enter no. of terms: ");
            n = sc.nextInt();
            for (i = 1; i <= n; i++)
            {
                term = i * i - 1;
                System.out.print(term + " ");
            }
            break;

            case 2:
            d=2;
            for (i = 1; i <= 19; i=i+2)
            {
                sum = sum +i /d;
                d=d+2;
            }
            System.out.println(sum);
            break;
            default:
            System.out.println("Invalid choice");
        }
    }
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *