Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
write a program to perform the addition of all odd numbers in the given range. The sum should be displayed as 0 if the first integer is greater than second integer.



C#
Test Cases: 1
Sample input:

2
10

Expected Output: 
24


Test Cases: 2
Sample Output:

8
2

Expected Output:
0



C#
int number;
int min, max;
int sum = 0;

Console.WriteLine("Enter the minimum number");
min = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the maximum number");

max = Convert.ToInt32(Console.ReadLine());


for (number = min; number <= max; number++)
{
    if (number % 2 != 0)
        sum = sum + number;

}
Console.WriteLine(sum);



I am able to do first sample input. but i am not able to acheive second desired output?


Please help.
Thanks in advance.
Posted
Updated 13-Jan-16 0:16am
v6
Comments
Jochen Arndt 13-Jan-16 4:40am    
What have you tried so far and where you got stuck?
If you give us that information you will probably get help.

But nobody here will do your homework.
suhel_khan 13-Jan-16 4:59am    
jochen I agree with you but this is the portal where developer can post there queries we are no one to stop any one from posting anything. Try to motivate the people buddy not to demotivate.
Jochen Arndt 13-Jan-16 5:15am    
I did not tried to stop him from posting. I tried to motivate him to update his post with the code he has produced so far and explain where he has problems.

Then we can help and he will learn something.

But if he got the whole work done here he will not learn anything. See also Griff's answer.

But I can't understand your above comment when reading the one below. Above you are saying that everybody can post anything while in the below post you are telling him to use Google instead of posting here.
suhel_khan 13-Jan-16 5:20am    
Its just a suggestions for him to use google. What is the issue with you if he is asking a silly question. I already told in last he is loosing if he is not doing his own homework.

Its better you don't answer instead of downvoting the answer.
Aditakumar2311 13-Jan-16 5:36am    
I have done test cases: 1 with desired output, but i am unable to do the second test case in same code?
Thanks

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
dan!sh 13-Jan-16 4:41am    
We rarely do our own homework. Why do you think Mrs. Wife is annoyed?
Just a little change in your code.
C#
int number;
            int min, max;
            int sum = 0;

            Console.WriteLine("Enter the minimum number");
            min = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the maximum number");

            max = Convert.ToInt32(Console.ReadLine());

            if (max > min)
            {                              
               
                for (number = min; number <= max; number++)
                {
                    if (number % 2 != 0)
                        sum = sum + number;
                }
            }

            else
            {
                sum = 0;
            }
            Console.WriteLine(sum);

the else is enough and the WriteLine can be moved to the end to avoid dupliating.
Nice to see you were able to solve your problem by yourself.
 
Share this answer
 
Comments
Richard Deeming 13-Jan-16 10:01am    
Why would you need the if (max > min) test? If max < min, the loop condition will be false, and the loop won't execute.

The only condition you're excluding is where max == min. The specification is unclear on what happens in that case, but it seems reasonable that if the single value in the range is odd, the output should be that number rather than 0.
Patrice T 13-Jan-16 11:44am    
Your comment is for me or for the OP ?
Richard Deeming 13-Jan-16 11:47am    
For you and Jochen. The OP's code didn't include that test.
Patrice T 13-Jan-16 11:56am    
I just took the code provided by Aditakumar2311 and changed the else if to a simple else.
But you are right, the if is not mandatory in this case, but it can be justifyed for readabiloity.
Aditakumar2311 29-Jan-16 12:37pm    
Thanks alot ppolymorphe.
use below code in c#

C#
Random rnd = new Random();
Console.WriteLine("\n20 random integers from 1 to 10:");

int total=0;
for (int X = 1; X <= 20; X++)
{
    int y = rnd.Next(1, 10);
    if (y % 2 == 1)
    {
        // Add all the odd # and display sum.
total+=total+y;
    }
    else
    {
    }
    Console.WriteLine("{0}", y);
}
Console.ReadLine();
 
Share this answer
 
Comments
dan!sh 13-Jan-16 4:42am    
Bad idea. For one you did someone elses homework and two, there are better ways than this.
suhel_khan 13-Jan-16 4:50am    
@danish i agree with you and with other people comments, but this portal is for developer to post there queries. Its his fault if he dont do his homework at the last he is loosing.
Aditakumar2311 13-Jan-16 6:06am    
Thanks Suhel. Here is the code which fulfill the both test cases.


int number;
int min, max;
int sum = 0;

Console.WriteLine("Enter the minimum number");
min = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the maximum number");

max = Convert.ToInt32(Console.ReadLine());

if (max > min)
{

for (number = min; number <= max; number++)
{
if (number % 2 != 0)
sum = sum + number;

}
Console.WriteLine(sum);
}

else if (min > max)
{
sum = 0;
Console.WriteLine(sum);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900