Computer Science, asked by vanshika1393, 1 year ago

Write a program to calculate the sum of all odd numbers and even numbers between a range of n7mbers from m to n (both inclusive) where m<n. Input m and n (where m<n).

Answers

Answered by BrainlyTech
3

Hello Friend!

How are you today?

I'm here to help you with your question!

So lets get started!

__________________________________

Your question is:

Write a program to calculate the sum of all odd numbers and even numbers between a range of n7mbers from m to n ( both inclusive) where m<n. input m and n (where m<n).

___________________________________

Here is my answer/code ↓

#include <stdio.h>


 void main()


{


   int i, num, odd_sum = 0, even_sum = 0;


 

   printf("Enter your value num\n");


   scanf("%d", &num);


   for (i = 1; i <= num; i++)


   {


       if (i % 2 == 0)


           even_sum = even_sum + i;


       else


           odd_sum = odd_sum + i;


   }


   printf("Sum of all (odd) numbers  = %d\n", odd_sum);


   printf("Sum of all (even) numbers = %d\n", even_sum);


}

_______________________________

Hope this is what your looking for!

If not let me know!

This is a C program, If you need it in java I will do my best to do it!

Have a good day my friend!

\boxed{ Be Brainly! }


vanshika1393: please give me the answer in Java
BrainlyTech: ok you will need to make a new question
vanshika1393: I am sending you this question again.
vanshika1393: Write a program to calculate the sum of all odd numbers and even numbers between a range of numbers from m to n (both inclusive) where m
Answered by dollshivanee
11

import java.util.*;

class sum

     {

     public void main()

            {

             Scanner sc=new Scanner(System.in);

             System.out.println("Enter a large number");

             int m=sc.nextInt();

             System.out.println("Enter a small number");

             int n=sc.nextInt();

             int s1=0;

             int s2=0;

             for(int i=n;i<=m;i++)

                   {

                   if(i%2==0)

                         {

                            s1=s1+i;

                         }

                   else

                         {

                            s2=s2+i;

                         }

                   }

             System.out.println("The sum of even numbers = "+s1);

             System.out.println("The sum of odd numbers = "+s2);

            }

     }


Similar questions