Thread: Programming newbie seeking help!

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    27

    Programming newbie seeking help!

    I'm REALLY not good at programming. Sometimes its hard for people to fully explain how to do something. I'm supposed to write a program that prints the integers in the range of 1 to 100 that are evenly divisible by 3 but not evenly divisible by 6. I managed to create:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i;
    	int w = 0;
    
    	for (i=1; i<=100; i++)
    	{
    		
    		if(i%3 == 0 && i%6 != 0)
    			printf("The numbers that are evenly divisible by 3 and not 6 are: %2d/3\n", i);
    			
    	}
    
    }
    The output repeats "The numbers '' '' '' for each found integer. How do I get it to only list it ONCE, and how do I get the program to keep count until the last found number?

    And then I have to write a similar program that, beginning with the integer 1, finds the integers that are evenly divisible by 3 or 4 and/or evenly divisible by 5 or 6. The program is supposed to stop after its found 5 integers that are divisible by 5 or 6.

    Can someone please explain to me, with examples, as to what I'm missing? And how I'm supposed to do it?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jaja009 View Post
    I'm REALLY not good at programming. Sometimes its hard for people to fully explain how to do something. I'm supposed to write a program that prints the integers in the range of 1 to 100 that are evenly divisible by 3 but not evenly divisible by 6. I managed to create:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i;
    	int w = 0;
    
    	for (i=1; i<=100; i++)
    	{
    		
    		if(i%3 == 0 && i%6 != 0)
    			printf("The numbers that are evenly divisible by 3 and not 6 are: %2d/3\n", i);
    			
    	}
    
    }
    The output repeats "The numbers '' '' '' for each found integer. How do I get it to only list it ONCE, and how do I get the program to keep count until the last found number?

    And then I have to write a similar program that, beginning with the integer 1, finds the integers that are evenly divisible by 3 or 4 and/or evenly divisible by 5 or 6. The program is supposed to stop after its found 5 integers that are divisible by 5 or 6.

    Can someone please explain to me, with examples, as to what I'm missing? And how I'm supposed to do it?
    You have the notice "The numbers that are evenly divisible...", inside the inner part of the loop. Why not move that printing to someplace just before the loop starts, and then it will print, just once?

    You should not need to divide i by anything, at any time, in your program. You want to print the numbers that meet your specifications, and those numbers will always be the value of i.

    If you want to count how many numbers you find that meet your criteria, then:

    Code:
    int count;
    . . . 
    count = 0;
    if((i % 3 == 0) && (i % 6 != 0)) {
       print i here;
       count++;
    }
    When you combine multiple logical statements, like here, it's a great idea to encapsulate each expression, into it's own parenthesis (), so the terms are evaluated as you'd expect, without relying on C's standard precedence table. That table can throw you some curves.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Hey THANKS A LOT!! I managed to get it! :
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i;
    	int count = 1;
    
    			printf ("The numbers that are evenly divisible by 3 and not 6 are: \n");
    			
    
    	for (i=1; i<=100; i++)
    
    	{
    		
    		if(i%3 == 0 && i%6 != 0){
    			printf("%2d  . %2d\n", count, i);
    			count++;
    		}
    			
    	}
    
    }
    It works perfectly!! =D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Aeres seeking interested hoobyists!
    By Akkernight in forum Projects and Job Recruitment
    Replies: 13
    Last Post: 03-19-2009, 11:50 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM