473,388 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

program to find divisors of 1-50

8
I have written the program for factors of #'s 1-50 but the factors print out below the number being factored. I can't figure out how to make it all on the same line.
i.e. (now) divisors of 25 are
1
5
25
(need) divisors of 25 are 1, 5, 25.
here is my code;
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3.  
  4. int main() 
  5.  
  6. {
  7.     int i;
  8.     int x=50;
  9.     int j;
  10.     int k=50;
  11.  
  12.     for (i=1; i<=x; i++)
  13. {    printf("The divisors of %d are ",i);
  14.     for (j=1; j<=k;  j++)
  15.         if(i%j==0)
  16.              printf("%d",j);    
  17.  
  18. }
  19. }
Feb 13 '08 #1
3 2758
Ganon11
3,652 Expert 2GB
Actually, this is what I get as output from your program:

Expand|Select|Wrap|Line Numbers
  1. $ gcc -o test test.c
  2. $ ./test
  3. The divisors of 1 are 1The divisors of 2 are 12The divisors of 3 are 13The divis
  4. ors of 4 are 124The divisors of 5 are 15The divisors of 6 are 1236The divisors o
  5. f 7 are 17The divisors of 8 are 1248The divisors of 9 are 139The divisors of 10
  6. are 12510The divisors of 11 are 111The divisors of 12 are 1234612The divisors of
  7.  13 are 113The divisors of 14 are 12714The divisors of 15 are 13515The divisors
  8. of 16 are 124816The divisors of 17 are 117The divisors of 18 are 1236918The divi
  9. sors of 19 are 119The divisors of 20 are 12451020The divisors of 21 are 13721The
  10.  divisors of 22 are 121122The divisors of 23 are 123The divisors of 24 are 12346
  11. 81224The divisors of 25 are 1525The divisors of 26 are 121326The divisors of 27
  12. are 13927The divisors of 28 are 12471428The divisors of 29 are 129The divisors o
  13. f 30 are 12356101530The divisors of 31 are 131The divisors of 32 are 12481632The
  14.  divisors of 33 are 131133The divisors of 34 are 121734The divisors of 35 are 15
  15. 735The divisors of 36 are 123469121836The divisors of 37 are 137The divisors of
  16. 38 are 121938The divisors of 39 are 131339The divisors of 40 are 12458102040The
  17. divisors of 41 are 141The divisors of 42 are 12367142142The divisors of 43 are 1
  18. 43The divisors of 44 are 124112244The divisors of 45 are 13591545The divisors of
  19.  46 are 122346The divisors of 47 are 147The divisors of 48 are 12346812162448The
  20.  divisors of 49 are 1749The divisors of 50 are 125102550$
You never print a newline character, so all the numbers are jumbled up on the same line.
Feb 13 '08 #2
jimix
8
o.k. I tried it several ways. I hope I can just insert a \n in the right spot to solve the problem but I can't figure out where or if it is possible.
Feb 13 '08 #3
Ganon11
3,652 Expert 2GB
Well, when do you want a newline to appear? If you want all the divisors to appear on one line, then you should only print a newline character once all the divisors have been displayed.
Feb 14 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
15
by: Aubrey Hutchison | last post by:
### this will test if your version of IDLE on windows is a MATURE program design ### print "Hello world, If Python IDLE is a mature program design it will output this" Save this simple...
6
by: Peter Hansen | last post by:
Greetings. Im trying to write a program that can be run from the command line. If I want to search for example after a file with the ending .pdf, I should be able to write in the command line:...
5
by: Philp Smith | last post by:
Hi Does anyone have suggested code for a compact, efficient, elegant, most of all pythonic routine to produce a list of all the proper divisors of an integer (given a list of prime...
7
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd...
9
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working...
0
by: Niklas Norrthon | last post by:
On 24 Juli, 00:30, Samir <spytho...@gmail.comwrote: 'int' object is not callable... hmmm... Traceback (most recent call last):
10
by: len | last post by:
I have created the following program to read a text file which happens to be a cobol filed definition. The program then outputs to a file what is essentially a file which is a list definition...
3
by: strangetorpedo | last post by:
The problem lies under where if (choice == '2') is...I need the program to keep prompting for num and den values as long as the user enters values less than or equal to 0. Once they are values...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.