New to C, printing arrays

This is a discussion on New to C, printing arrays within the C Programming forums, part of the General Programming Boards category; I am attempting to write a simple program that prints all factors of a number entered from a user. I ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    New to C, printing arrays

    I am attempting to write a simple program that prints all factors of a number entered from a user. I am trying to limit my printing to
    5 factors per line. I am experimenting with an array, I am new to C, can anyone offer any suggestions? The code follows:

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>

    void main(void)
    {
    int number, decrease, arrFact[1000];

    clrscr();

    printf("PLEASE ENTER A NUMBER: ");
    scanf("%d", &number);
    if ((number % 2 !=0 )&&(number %3 !=0)&&(number%5!= 0))
    {printf("This number is prime.");}
    else
    {printf("\nTHANK YOU, THE FACTORS OF %d ARE: ", number);}


    for(decrease=number; decrease >= 2; decrease--) //Loop Counter for factors
    { if(number % decrease == 0)
    {arrFact[decrease]=decrease;
    printf("%d, ", arrFact[decrease]);
    }

    //attempting to print 5 factors per line Help!!
    }

    printf("\n and 1");

    }

  2. #2
    Unregistered
    Guest

    Smile A suggestion

    Since you are new to C, use a loop to attempt to do it. The move up to using arrays.

    Also, why are you checking in your if statement if it is divisible by 3 or 5. You do not need to.

    Check to make sure you understand how to find the factors of a number.

    I gave my Java students a problem similar to this.

    Mr. C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seperating initialising and printing of arrays into functions
    By Tom Bombadil in forum C Programming
    Replies: 1
    Last Post: 03-11-2009, 12:47 PM
  2. Printing String Arrays
    By kwikness in forum C Programming
    Replies: 6
    Last Post: 10-08-2007, 02:44 AM
  3. Printing arrays to curses screen
    By trev456 in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 01:46 AM
  4. printing arrays with concatenation
    By derek23 in forum C Programming
    Replies: 1
    Last Post: 07-17-2005, 04:02 AM
  5. Printing back arrays
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 01:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21