Page 499 - Cs_withBlue_J_C11_Flipbook
P. 499
i int Loop variable
s int Sum of the digits
f int Counter of the digits
sum1 int Stores the sum of the digits
sum2 int Stores the sum of the prime numbers
Program 5 Write a program to input a single dimensional array and print the sum of its odd digits at
even positions.
1 import java.util.*;
2 class odd
3 { //start of class
4 public static void main(String args[])
5 { //start of main
6 int n,s=0;
7 Scanner sc=new Scanner(System.in);
8 System.out.println("enter the size of the array");
9 n=sc.nextInt();
10 int a[]=new int[n];
11 System.out.println("enter the array elements");
12 for(int i=0;i<n;i++)
13 {
14 a[i]=sc.nextInt();
15 }
16 for(int i=0;i<n;i=i+2)//taking only the even positions
17 {
18 if(a[i]%2!=0)//checking if the digit at that position is odd
19 {
20 s=s+a[i];
21 }
22 else
23 continue;
24 }
25 System.out.println("the sum of odd numbers in the even positions is "+s);
26 } //end of main
27 } //end of class
497
Internal Assessment 497

