Computer Science, asked by TheAnimetors, 1 month ago

Write a program to enter five numbers and print the sum of all the odd numbers if any (using for loop)

Answers

Answered by samarthkrv
0

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

    int[] arr = new int[5];

    int sum = 0;

 Scanner sc = new Scanner(System.in);

     for(int i = 0;i < 5; i++){

         System.out.print("Enter number " + (i+1) + ":");

         int x = sc.nextInt();

         arr[i] = x;

     }

     for(int j = 0; j < arr.length;j++){

         if(arr[j] % 2 == 1){

             sum = sum + arr[j];

         }

     }

     System.out.println("The sum of the odd numbers is " + sum);

}

}

Explanation:

Similar questions