+5 votes

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;

}

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

0 votes

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.

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.

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.

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

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. 1^{2} + 3^{2} + 5^{2} + 7^{2} = 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.

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.

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

#include<stdio.h>intmain() {intfirstnum = 0;printf("Input firstnum value : ");scanf("%d", &firstnum);intsecondnum = firstnum - 1;// ensuring that the sanity check below would fail if the user does not enter a valid numberprintf("Input secondnum value : ");scanf("%d", &secondnum);// // Sanity check //if(firstnum > secondnum) {printf("Invalid Input!\n");return-1; }// // Calculate and print the results //intsum = 0;inteven = 0;intsOdd = 0;for(inti = 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(inti = 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 evenprintf("%d ", i);printf("\nSum of all even numbers : %d\n", even);printf("Sum of square all odd numbers : %d\n", sOdd);return0; }

commented
Nov 22
by
Lexus Guevara
(560 points)

Thank you very much

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!

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. :)

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. :)

...