Computer Science, asked by Sana111111, 1 year ago

sum of odd no and even no (blue j ) by using single loop ?explain ful

Answers

Answered by visheshs3257
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);
}


visheshs3257: It is for 1st 10 natural numbers only you can take input for n and modify the code .
siddhartharao77: Bro! but you have written void main().
visheshs3257: Then wht ?
siddhartharao77: In the end, you have written system.out.println. Is it java code or c code?
visheshs3257: Dude its Java
visheshs3257: I used shortcut , as the the one who is asking the ques. Only need the code not the whole syntax. Thanks.
siddhartharao77: Okkkk...Thanks bro for the clarification..
Answered by siddhartharao77
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!

visheshs3257: Hey Sid can you please answer my question , it is on my profile. Please check .
Similar questions