Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Please Help me! need help urgent

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Ask the user to enter a positive integer number (call it num1).
2. The program will add all ODD numbers in the range from 0 to num1.
3. Make another FOR loop (outside the first loop) to print all numbers in the same range that are divisible by another number entered by the user (call it num2).

Can someone help me on question 3 (from above this line). I got question 1 and 2 done, help me on question 3

 
Marshal
Posts: 70718
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We only use words like urgent on 1st April.

Why, if you want a positive input, did you declare your number as int (line 3‑4), and why did you use %d in lines 6 and 8?
Don't declare multiple variables on the same line. The compiler might not mind, but it makes the code harder to read. Also space your code a bit; it is difficult to read with all the code squashed together.
Why are you writing all that code in main() rather than using input() and summate() functions or similar?
Please explain the logic behind the loop in line 10, which looks incorrect to me. Once you have explained that logic to yourself correctly, you should have little trouble working out part 3.
Please confirm that the ranges for your loops are inclusive.
Try to put a line end last when using printf().
 
lowercase baba
Posts: 12916
64
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
start small.  first, just write a for loop that does nothing but prints 1-10 or something like that.

Then, once (and only once) that works, have it print the numbers "in the same range" as the first.

Then, once (and only once) that works, try and get another number from the user, calling it num2...and so on.

also...are you sure you want to have the same scanf("%d", $num1); line?  won't the second line clobber the value input in the first line?
 
olu idowu
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:

also...are you sure you want to have the same scanf("%d", $num1); line?  won't the second line clobber the value input in the first line?



I changed it
 
Campbell Ritchie
Marshal
Posts: 70718
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I got question 1 and 2 done

No, you haven't.
I tried your code in its original version and entered 22 twice:-

./a.out
Enter a number: 22
Enter another number: 22
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43
Sum of odd numbers = 253

The correct answer is 121 The numbers you showed add up to 484.
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic