Click here to Skip to main content
14,875,113 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Why I am not getting any output?

What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
	int n;
	int i = 2;
	int r = 80; 
	printf("Prime Numbers Between 80 & 486 Are:\n");
	while (r <= 486) 
      {
      n = 1;
      while (i < r)
      {
        if (r%i == 0)
        n = 0;
        else
        ++i;
      }
        if (n == 1)
        printf("%d ", r);
      r++;
      }
	return 0;
}
Posted
Updated 30-Mar-21 1:49am
v2

Using the debugger, you could have found easily yourself this fix for your code:
C
include <stdio.h>

int main()
{
  int r = 80;
  printf("Prime Numbers Between 80 & 486 Are:\n");
  while (r <= 486)
  {
    int i = 2; // this should be initialised at every iteration on r
    int n = 1;
    while (i < r)
    {
      if (r % i == 0)
      {
        n = 0;
        break; // the first 'zero-reminder' divison makes the primality test fail
      }
      ++i;
   }
   if (n == 1)
     printf("%d ", r);
   r++;
  }
  return 0;
}



Alternatively, you could write:
C
#include <stdio.h>

int main()
{
  const int First = 80, Last = 486;
  printf("Prime Numbers Between %d & %d Are:\n", First, Last);
  for ( int number = First; number <= Last; ++number)
  {
    int divisor;
    for (divisor = 2; divisor < number; ++divisor)
    {
      int reminder = (number % divisor);
      if ( reminder == 0)
        break;
    }
    if ( divisor == number)
    {
     printf("%d ", number);
    }
  }
  return 0;
}
   
First off, indent your code properly - it makes it a load easier to read! Pick a single indentation method, and stick to it:
C++
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    int n;
    int i = 2;
    int r = 80; 
    printf("Prime Numbers Between 80 & 486 Are:\n");
    while (r <= 486) 
    {
        n = 1;
        while (i < r)
        {
            if (r%i == 0)
                n = 0;
            else
                ++i;
        }
        if (n == 1)
            printf("%d ", r);
        r++;
    }
    return 0;
}
Or
C++
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
    {
    int n;
    int i = 2;
    int r = 80; 
    printf("Prime Numbers Between 80 & 486 Are:\n");
    while (r <= 486) 
        {
        n = 1;
        while (i < r)
            {
            if (r%i == 0)
                n = 0;
            else
                ++i;
            }
        if (n == 1)
            printf("%d ", r);
        r++;
        }
    return 0;
    }
But never "mix and match" indentation.

After that, your problem is pretty obvious - and a very quick check with the debugger would have shown you exactly what the problem is in less time that it took you to post this question!

So fire up the debugger, and start looking at your code while it is running, and it'll be obvious to you as well.
But I'll give you a hint: when do you reset i ?
   
v2
Quote:
Why I am not getting any output?

Because you did it wrong.
C++
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
	int n;
	int i;
	int r = 80;
	printf("Prime Numbers Between 80 & 486 Are:\n");
	while (r <= 486)
	{
		n = 1;
		i = 2; // you forgot to rest i to 2 before checking a new number.
		while (i < r)
		{
			if (r%i == 0)
				n = 0;
			else
				++i;
		}
		if (n == 1)
			printf("%d ", r);
		r++;
	}
	return 0;
}


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.

The debugger is here to show you what your code is doing and your task is to compare with what it should do.

There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.

To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900