hii,I'm a new programer.. and now I have a homework. And I stuck with this,, This homework about find 100 first number.. I have one solution but,this solution is using syntax goto().. and the problem is my homework is restricted using syntax goto()..this my first solution

#include<conio.h>
#include<stdio.h>
#include<stdlib>
main()
{
int a,b,c,d;
a=2;
c=1;
printf("Prime number %d is %d",c,a);
loop1:
if(c==100)
exit(0);
{
b=2;
a=a+1;
loop2:
if(b==a)
{
c=c+1;
printf("Prime number %d is %d",c,a);
goto loop1;
}
else
{
d=a%b;
if(d==0)
goto loop1;
else
{
b=b+1;
goto loop2;
}
}
getch();
}
}
this is sucess to answer my homework but it is restricted to use goto. So help me to change goto with while or do while,,because I'm not really know about using while or do while,,and please give some information about the different between while and do-while..

Recommended Answers

All 8 Replies

Member Avatar for iamthwee

This is where flow charts would be useful.

Start with

int main () {
  int a = 2, c = 1, b, d;

  while (c < 100) {
    b = 2;
    a++;

    /* Implement your next loop here changing the logic a bit */

  }
  return 0;
}

This is how you get rid of loop1. Use the same principal now to get rid of loop2, but you will have to change the logic of your conditionals to make it work. I can't do much more than this without doing the whole thing, so let's see what you come up with

So help me to change goto with while or do while,,because I'm not really know about using while or do while,,and please give some information about the different between while and do-while..

The main difference between the while and do-while loops is that the condition in a while loop is checked before the inner code is executed, whereas the do-while loop's condition is checked after the inner code is executed. This means that the do-while loop will execute at least once whereas the while loop will only execute for the first time if its condition is met.

hehe, i just wrote a counter that will go all the way up to 10001 instantly, though i havnt tested it for higher values.

super simple and absolutely NO goto's.

#include <iostream>
#include <math.h>
int main( void )
{
    // count to the 10001'st prime
    int primeCount = 2;
    // only count odd numbers: 15, 17, 19...etc
    for(int count = 5; primeCount <= 10001 ;count += 2)
    {
        for (int test = 2; test <= (int) sqrt((double) count);test++ )
        {
            if ((count % test) == 0)
                // number divides in, cannot be prime
                break;
            if (test == ((int) sqrt((double) count)))
                // final loop and no match
                primeCount++;
        }
        if (primeCount == 10001) 
        {
            std::cout << "Prime Count\t\tCurrent Prime" << std::endl;
            std::cout << "-----------\t\t-------------" << std::endl;
            std::cout << primeCount << "\t\t" << count << std::endl;
            std::cin.get(); // hold before exiting application
            break;
        }
    }
    return 0;
}

Just look at how many times sqrt() is called inside the loop.
Use another variable, and save the result ONCE outside the loop.

Just look at how many times sqrt() is called inside the loop.
Use another variable, and save the result ONCE outside the loop.

lol sure, that works i wrote that in about ~5min before because i just wanted to solve a problem real fast, optimizations will come later when i get to the much harder problems on projecteuler.net :(!

also note (wouldnt let me edit) and i need to add that the number being square rooted needs to be rounded up and incrimented by one to work in all cases, my little mistake will cause accuracy errors in the math.

Member Avatar for iamthwee

>before because i just wanted to solve a problem real fast

But it is not your homework.

>before because i just wanted to solve a problem real fast

But it is not your homework.

yeah...okay fine. i did this for another site buddy ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.