0
votes
  1. Write a program that prompts the user to input two integers: num1 and num2 (where num1 must be less than num2), then making use of a while loop:

 Output (ie display) all odd numbers between num1 and num2 (inclusive).  Output the sum of all even numbers between num1 and num2 ( " ).  Output the sum of the square of odd numbers between num1 and num2 ( " ).

Eg:

Enter 2 numbers (num1 < num2) : 2 9 The odd numbers between 2 and 9 are : 3 5 7 9 The sum of even numbers are : 20 The sum of squares of odd numbers are : 164

I've tried solving this for about 2 hours now and here is my progress so far and I dont know where to go from here:

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#include<math.h>


int main() {

    int num1,num2;

    printf("Enter 2 integers = ");
    scanf("%d%d", &num1, &num2);

    
        while (num1 < num2)

        num1++;

        if (num1 % 2 == 0) {
            printf("The even number are =%d\n", num1);


            }

        else{
            printf("The odd numbers are =%d\n", num1);
        
        }


    return 0;

}
2
This appears to be a homework assignment. These assignments are designed so that you practice and reinforce useful concepts while solving them. If you ask for the solution and copy it, you will not get the same benefit. Please show us what you have tried so far, as well as any outputs (incorrect results, error messages), or describe in more detail which specific step you are having trouble with. This information should be added to your post using the edit link. - nanofarad
Thanks for adding the code--which particular functionality is giving you trouble? What does this code do when you try to run it? Does it give an error message at compile- or run-time? Does it freeze unexpectedly? Does it run but give unexpected output? - nanofarad
The output is : The odd numbers are = 2 The trouble im facing is that I do not know how to code this using loop and if functions - Adrian Joshua
Please clean up your code. There's an utterly absurd number of blank lines here that serve no purpose other than to make your code hard to read. - tadman
tadman, that really depends on his coding style. His professor may require it. As long as he is consistent, it is fine. - mreff555

2 Answers

0
votes

you are missing {} to wrap the contents of your loop, try:

 while (num1 < num2)
 {
        num1++;

        if (num1 % 2 == 0) {
            printf("The even number are =%d\n", num1);
        }

        else{
            printf("The odd numbers are =%d\n", num1);
        
        }
}
0
votes

Your while-loop doesn't enclose the correct scope. Notice that it reads:

 while (num1 < num2)
     num1++;
     if ...

There are no braces around the region that you want to repeat in the loop, so the loop body is just the statement num1++. If you wrap the entirety of your intended loop body in braces, you get output that looks more consistent with the expected output:

while (num1 < num2) {
    num1++;
    if (num1 % 2 == 0) {
        printf(...)
    } else {
        printf(...)
    }
}

There are still some remaining problems in your code. First of all, you immediately increment num1 so the first value to be output is num1 + 1, and there may be further issues that you will come across as you debug.

This should get you well on your way to solving the exercise. You will need to create a few additional variables to track the sums that you are asked to print (notice that you must print the sum of the evens and squares of odds, not the individual values). You can compute those sums within the loop with the help of a few more int variables, and print those results after the loop.