sum of odd no and even no (blue j ) by using single loop ?explain ful
Answers
Answered by
2
void main()
{
int i=0,n,sume=0,sumo=0;
n=10;
for(i=0;i less than n; i++){
if(i%2==0)
sume=sume+i;
else
sumo=sumo+i;
}
System.out.println(sume);
System.out.println(sumo);
}
{
int i=0,n,sume=0,sumo=0;
n=10;
for(i=0;i less than n; i++){
if(i%2==0)
sume=sume+i;
else
sumo=sumo+i;
}
System.out.println(sume);
System.out.println(sumo);
}
visheshs3257:
It is for 1st 10 natural numbers only you can take input for n and modify the code .
Answered by
1
public class Demo
{
public static void main(String[] args)
{
int n, even = 0, odd = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number :");
n = scan.nextInt();
int[] a = new int[n];
System.out.println("Enter some numbers:");
for(int i = 0; i < n; i++)
{
a[i] = scan.nextInt();
}
for(int i = 0; i < n; i++)
{
if(a[i] % 2 == 0)
{
even = even + a[i];
}
else
{
odd = odd + a[i];
}
}
System.out.println("Even numbers sum is :"+even);
System.out.println("Odd numbers sum is :"+odd);
}
}
Hope this helps!
{
public static void main(String[] args)
{
int n, even = 0, odd = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number :");
n = scan.nextInt();
int[] a = new int[n];
System.out.println("Enter some numbers:");
for(int i = 0; i < n; i++)
{
a[i] = scan.nextInt();
}
for(int i = 0; i < n; i++)
{
if(a[i] % 2 == 0)
{
even = even + a[i];
}
else
{
odd = odd + a[i];
}
}
System.out.println("Even numbers sum is :"+even);
System.out.println("Odd numbers sum is :"+odd);
}
}
Hope this helps!
Similar questions