4 Replies - 572 Views - Last Post: 20 May 2008 - 03:14 AM Rate Topic: -----

#1 arslan_09  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 14-April 08

Need help in this program

Post icon  Posted 19 May 2008 - 02:17 PM

Write a C++ program which will display first 100 composite numbers. Composite integers are those integers which have more than two factors. In this program, you are not
allowed to use for loop.

#include <iostream>
using namespace std;
void main()
{
	int x,y;
	x=2;
	while(x<=100)
	{
		y=2;
		while(y<=x)
		if((x%1==0)&&(x%y==0))
		{
			cout<<x;
			x++;
			y++;
		}
		cout<<"\n";
	}
}



i don't understand where is the problem please help me.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help in this program

#2 skyhawk133  Icon User is online

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,283
  • Joined: 17-March 01

Re: Need help in this program

Posted 19 May 2008 - 02:20 PM

What errors are you getting?

What is the program doing that it shouldn't be doing?

What isn't the program doing that it should be?
Was This Post Helpful? 0
  • +
  • -

#3 arslan_09  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 14-April 08

Re: Need help in this program

Posted 19 May 2008 - 06:53 PM

View Postskyhawk133, on 19 May, 2008 - 02:20 PM, said:

What errors are you getting?

What is the program doing that it shouldn't be doing?

What isn't the program doing that it should be?


the program does not show any error but the loop runs for infinite times.
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Need help in this program

Posted 19 May 2008 - 07:37 PM

Hi arslan ^_^

Here's your code, commented:
#include <iostream>

using namespace std;

int main() // main should ALWAYS return an int
{
    int x,y; // int x and y
    x=2;
    while(x<=100) 
    {
        y=2; // set y to 2 at the beginning of each loop
        while(y<x) // if it is <= it will reach == where it is always divisible
        {
            if (x % y == 0) // if y can go into x with NO remainder
            {
                cout << x; // then print x
                break; // and break from the loop
            }
            y++; // continue inside your nested loop
        }
        x++; // continue your outer loop
        cout<< endl; // print a new line
    }
    
    cin.get ();
    return EXIT_SUCCESS;
}

I'm not sure if it's the result you're after, but it certainly isn't an infinite loop anymore :P

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#5 arslan_09  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 14-April 08

Re: Need help in this program

Posted 20 May 2008 - 03:14 AM

View Postgabehabe, on 19 May, 2008 - 07:37 PM, said:

Hi arslan ^_^

Here's your code, commented:
#include <iostream>

using namespace std;

int main() // main should ALWAYS return an int
{
    int x,y; // int x and y
    x=2;
    while(x<=100) 
    {
        y=2; // set y to 2 at the beginning of each loop
        while(y<x) // if it is <= it will reach == where it is always divisible
        {
            if (x % y == 0) // if y can go into x with NO remainder
            {
                cout << x; // then print x
                break; // and break from the loop
            }
            y++; // continue inside your nested loop
        }
        x++; // continue your outer loop
        cout<< endl; // print a new line
    }
    
    cin.get ();
    return EXIT_SUCCESS;
}

I'm not sure if it's the result you're after, but it certainly isn't an infinite loop anymore :P

Hope this helps :)



thanks Danny that's work fine and i got my output. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1