3 Replies - 4847 Views - Last Post: 24 June 2012 - 12:42 PM Rate Topic: -----

#1 vr561600  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-June 12

Display all prime numbers from 50-100

Posted 24 June 2012 - 11:58 AM

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include <math.h>

main()
{
      int n , isprime, i, j;
      
      printf ("This program will print a list of prime numbers from 50-100.\n");
      for(i = 2;i <= sqrt(n);i++)
      {
      isprime = 1;
      }
      for(j = 2;j <= i;j++);

            isprime = 0;
            if(isprime)
      printf ("%d", i);
      }

      getchar();
}




My error here is that when I try to compile the program, I'm not even allowed to run and debug for errors because the compiler tells me that there is a "[Warning] data definition has no type or storage class" and the compiler highlights the piece of code " getchar();" just at the end of the program. I'm new to C++, I'm working on learning on my own, and I'm slightly confused as to manipulating nested for loops to achieve my purpose. Any input from anyone would be greatly appreciated by me.

This post has been edited by modi123_1: 24 June 2012 - 12:05 PM
Reason for edit:: fixed botched code tag


Is This A Good Question/Topic? 0
  • +

Replies To: Display all prime numbers from 50-100

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,681
  • Joined: 25-December 09

Re: Display all prime numbers from 50-100

Posted 24 June 2012 - 12:20 PM

You need to find an indentation style that you like and use it consistently. If you were using consistent indentation you could easily see that you have a brace mismatch.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 vr561600  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-June 12

Re: Display all prime numbers from 50-100

Posted 24 June 2012 - 12:30 PM

View Postjimblumberg, on 24 June 2012 - 12:20 PM, said:

You need to find an indentation style that you like and use it consistently. If you were using consistent indentation you could easily see that you have a brace mismatch.

Jim


Thank you so much for bringing that to my attention! I watched a couple of youtube videos on how to use for loops, and I amended my code to the following:


main()
{
int num1;
for(num1=2; num1<=97; num1++)
{
int x=2, prime=1;
while(x<num1 && prime==1) // USING WHILE LOOP INSIDE FOR LOOP
{
if(num1%x==0)
prime=0;
else
x++;
}
if(prime==1)
printf ("%d\n", num1);
}
}



I am using Dev C++, and when I run the program, the output box just flashes then disappears. How can I get it to stay so that I can view it?

This post has been edited by jimblumberg: 24 June 2012 - 12:35 PM
Reason for edit:: Fixed Code tags.

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,681
  • Joined: 25-December 09

Re: Display all prime numbers from 50-100

Posted 24 June 2012 - 12:42 PM

First I recommend that you think about getting a different IDE/compiler. The IDE you are currently using is very old, outdated and buggy.

Next you had the correct idea in your first post (getchar()) you just had a brace mismatch. Move the getchar() into your code right before the last brace and your program should wait.

Also C no longer allows functions with default return values, therefore main should be defined to return an int and you should return an int from this function:

int main
{
   return 0;
}



You still need to find that indentation style I mentioned, if you ever plan to become a programmer. The proper, consistent indentation will make following your logic much easier and should help you avoid errors of mismatched braces. Also you will find fewer and fewer people willing to help you as your programs get larger. You should consider proper indentation a requirement and start doing it now.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1