Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,943 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,731 people online right now. Registration is fast and FREE... Join Now!




Need help in this program

 
Reply to this topicStart new topic

Need help in this program

arslan_09
19 May, 2008 - 01:17 PM
Post #1

New D.I.C Head
*

Joined: 14 Apr, 2008
Posts: 49


My Contributions
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.

CODE
#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.
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: Need Help In This Program
19 May, 2008 - 01:20 PM
Post #2

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 14,920



Thanked: 47 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
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?


User is online!Profile CardPM
+Quote Post

arslan_09
RE: Need Help In This Program
19 May, 2008 - 05:53 PM
Post #3

New D.I.C Head
*

Joined: 14 Apr, 2008
Posts: 49


My Contributions
QUOTE(skyhawk133 @ 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?


the program does not show any error but the loop runs for infinite times.
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Need Help In This Program
19 May, 2008 - 06:37 PM
Post #4

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,514



Thanked: 96 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Hi arslan happy.gif

Here's your code, commented:
cpp
#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 tongue.gif

Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

arslan_09
RE: Need Help In This Program
20 May, 2008 - 02:14 AM
Post #5

New D.I.C Head
*

Joined: 14 Apr, 2008
Posts: 49


My Contributions
QUOTE(gabehabe @ 19 May, 2008 - 07:37 PM) *

Hi arslan happy.gif

Here's your code, commented:
cpp
#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 tongue.gif

Hope this helps smile.gif



thanks Danny that's work fine and i got my output. biggrin.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:44AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month