Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed how can I make the firstnum be the first one to appear in "List of All Odd numbers"

+5 votes
asked Nov 22, 2021 by Lexus Guevara (1,010 points)
closed Nov 23, 2021 by Admin
I don't know how to make a code so that in the printf("List of All Odd numbers : "); the one that should appear in the first number is either the inputted first number (if it is odd) or the next number which is odd (if it is even)

because when the number is less than 10, the value starting from less than 10 is not printed, ALSO the integers greater than 10 is always starting from 11 until it reaches the second number

#include <stdio.h>

int main()
{
    int firstnum, secondnum, x, sum = 0, even = 0, sOdd = 0, list = 0;
    
    printf("Input firstnum value          : ");
    scanf("%d", &firstnum);
    printf("Input secondnum value         : ");
    scanf("%d", &secondnum);
    
    while (x = 0);
        if (firstnum < secondnum){
            x = 0;
        }
        else{
            printf("Invalid Input!\n");
        }
        {
        while (firstnum <= secondnum){
            sum = sum + firstnum;
                if(firstnum % 2 == 0){
                    even = even + firstnum;
                }
                if(firstnum % 2 != 0){
                    sOdd = sOdd + ((firstnum * secondnum) - sum);
                }
                firstnum++;
        }
            printf("Sum of all numbers            : %d\n", sum);
            {
            printf("List of all Odd Numbers       : ");
                for (firstnum = 10; firstnum <= secondnum; firstnum++){
                    if (firstnum % 2 != 0){
                        printf("%d ", firstnum);
                    }
                }
            printf("\nSum of all even numbers       : %d", even);
            printf("\nSum of square all odd numbers : %d", sOdd);
            }
        }
    return 0;
}
closed with the note: answered

1 Answer

0 votes
answered Nov 22, 2021 by Peter Minarik (84,720 points)

Problems With Your Code

Code That Does Nothing

while (x = 0);

The above line sets x to be 0, then checks if the condition of while is true. Since x is 0, the logical expression evaluates to false (anything that's not 0 evaluates to true). So the body of the while will never be executed, which is by the way nothing, an empty statement, a semicolon.

You should just remove this line.

Signalling Error, But Not Terminating

printf("Invalid Input!\n");

This tells the user that the input is wrong, but at this point there's no point continuing the function, you should as well exit:

return -1;

0 means all OK to the Linux shell, non-zero usually means an error, for this return something non-zero when your program terminates with n error.

Unnecessary Scopes (Braces)

There's an opening brace just before the marked lines below.

{
while (firstnum <= secondnum)
// ...
{
printf("List of all Odd Numbers       : ");

While it makes sense sometimes to create a new scope, I don't see why you'd need on there.

Incorrect Indentations

Please, indent your code correctly to make it more human-readable.

Misleading Output

printf("\nSum of square all odd numbers : %d", sOdd);

So the user would expect (I would expect XD) the summary of the squares of all the odd numbers (in a given range). E.g. 12 + 32 + 52 + 72 = 1 + 9 + 25 + 49 = 84. However, what you calculate is something different:

sOdd = sOdd + ((firstnum * secondnum) - sum);

I'd fix either the formula (sOdd += firstnum * firstnum;) or fix the output message to tell what is actually printed.

Overwriting Important Values

In your code you change the value of firstnum in your first while loop, so you won't be able to know what was the lower value of your original range. You should keep this value and use it later when you want to list the numbers and not start from 10.

Everything Fixed

So all the above fixed would create a code like this:

#include <stdio.h>

int main()
{
    int firstnum = 0;
    printf("Input firstnum value          : ");
    scanf("%d", &firstnum);
    int secondnum = firstnum - 1; // ensuring that the sanity check below would fail if the user does not enter a valid number
    printf("Input secondnum value         : ");
    scanf("%d", &secondnum);
    
    //
    // Sanity check
    //
    if (firstnum > secondnum)
    {
        printf("Invalid Input!\n");
        return -1;
    }

    //
    // Calculate and print the results
    //
    int sum = 0;
    int even = 0;
    int sOdd = 0;
    for (int i = firstnum; i <= secondnum; i++)
    {
        sum += i;
        if (i % 2 == 0)
        {
            even += i;
        }
        else
        {
            sOdd += i * i;
        }
    }
    
    printf("Sum of all numbers            : %d\n", sum);
    printf("List of all Odd Numbers       : ");
    for (int i = firstnum % 2 == 1 ? firstnum : firstnum + 1; i <= secondnum; i += 2) // The iteration starts from firstnum, if it is odd, or from firstnum + 1, if it is even
        printf("%d ", i);
    printf("\nSum of all even numbers       : %d\n", even);
    printf("Sum of square all odd numbers : %d\n", sOdd);

    return 0;
}
commented Nov 22, 2021 by Lexus Guevara (1,010 points)
Thank you very much
commented Nov 22, 2021 by Lexus Guevara (1,010 points)
also, I want to ask one last question, how about a code on Sum of squares of all even numbers? I really appreciate the help!
commented Nov 22, 2021 by Peter Minarik (84,720 points)
I'm not sure what you're asking (because it seems pretty simple, so not sure why you're asking it as I'm pretty sure you could do it yourself).

    for (int i = firstnum; i <= secondnum; i++)
    {
        sum += i;
        if (i % 2 == 0)
        {
            even += i;
        }
        else
        {
            sOdd += i * i;
        }
    }

The above code sums the square of odds. That's the `else` (or false) branch of the `if`. So all you have to do is add your even square summarizer in the true branch. Looks identical to the one in the false branch: evenSquares += i * i;

This should be simple. So simple that I doubt that you were asking this and maybe I misunderstood what you're after. :)
commented Nov 22, 2021 by Lexus Guevara (1,010 points)
I just didn't realize it yet but I already figured it out haha. Again, thank you very much
commented Nov 22, 2021 by Peter Minarik (84,720 points)
One good piece of advice: if you can do something on your own, you learn way more from it than asking someone else to give you the final solution. ;)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...