473,661 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

program to find divisors of 1-50

8 New Member
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 2774
Ganon11
3,652 Recognized Expert Specialist
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 New Member
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 Recognized Expert Specialist
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
8799
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 feedback on how I might do things differently to make it either more consice, readable, or faster. ie... are there better ways to do it in Python? It won't break any records for calculating pi, that wasn't my goal, learning Python was. But it might...
15
2462
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 single line program as "select.py" and try to run it. Then exit IDLE and try to restart IDLE.
6
1772
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: python name of my program / the libary to search and what kind of file it is example a .pdf file So if my program name was test.py and the library name was library1 and the test type i wanted to find was, a .pdf file I should write python...
5
3222
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 factors/powers)
7
7386
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 integer is prime if it is not divisible by an odd integer less than or equal to the square root of the number.) I was given this assignment in my c++ class and I'm having trouble with it. I've looked at the professors lab solutions but I just...
9
4525
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 on linux platform and my target is ARM processor. But i guess it should not matter. Actually i need to know both RAM & ROM usage.
0
701
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
1987
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 which I can later copy and past into a python program. I will eventually expand the program to also output an SQL script to create a SQL file in MySQL The program still need a little work, it does not handle the following items
3
1766
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 greater than zero, the program is supposed to carry on and calculate the log, etc. HELP?! #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <math.h> #define PI 3.14159
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.