Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9000215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:16:59+00:00 2026-06-16T00:16:59+00:00

I have wrote a program that takes two integers(m and n) as input and

  • 0

I have wrote a program that takes two integers(m and n) as input and then writes to a file all the prime numbers between them. I wanted the output to look neat so I print each number 7 spaces wide using the field-width modifier and only print 10 per line. The output is as expected apart from the first line, where only 9 numbers are printed.
Here is the code:

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

#define SIZE 100000000

char primes[SIZE];

void seive(void)
{
    int i, j;
    primes[1] = primes[0] = 1;
    for (i=3;i<=sqrt(SIZE);i+=2)           
        if (primes[i] == 1)
            continue;
        else 
            for (j=i*i;j<SIZE;j+=2*i)
                primes[j] = 1;
}

int main()
{
    int n, m, count;
    FILE *fp;
    fp = fopen("test.txt", "w");
    seive();
    scanf("%d %d", &m, &n);
    count = 0;
    if (m <= 2) {
       fprintf(fp, "%7d ", 2);
       count++;
       }
    if (!(m & 1))
       m++;
    for (m;m<=n;m+=2) {
        if (!primes[m]) {
           count++;
           if (count == 10) {
              fprintf(fp, "\n");
              count = 0;
           }
           fprintf(fp, "%7d ", m);
        }
    }
    return 0;
}

This is the output for the input 1 300:

      2       3       5       7      11      13      17      19      23 
     29      31      37      41      43      47      53      59      61      67 
     71      73      79      83      89      97     101     103     107     109 
    113     127     131     137     139     149     151     157     163     167 
    173     179     181     191     193     197     199     211     223     227 
    229     233     239     241     251     257     263     269     271     277 
    281     283     293   

As you can see only nine numbers are printed on the first line, but after it does as it should. I’m baffled. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:16:59+00:00Added an answer on June 16, 2026 at 12:16 am

    I think it has something to do with the fact that you print your newline before you print your output in the loop.

    change it to:

    for (m;m<=n;m+=2) {
        if (!primes[m]) {
           count++;
    
           fprintf(fp, "%7d ", m);
    
           if (count == 10/*&& you're going to print more numbers*/) {
              fprintf(fp, "\n");
              count = 0;
           }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that takes two files as an argument. The first file
I have a program that either takes data from a file or from the
I have been assigned wit the task to write a program that takes a
I have a program I wrote in Windows with this piece of code that
I wanted to write a program that test if two files are duplicates (have
Purpose: Create a program that takes two separate files, opens and reads them, assigns
I have a file that has two columns of floating point values. I also
I wrote a GUI-based Java program that takes 25 screenshots per second and saves
I have a simple parsing program that takes a few files and combines them.
I wrote a console program in c# that takes up to three files as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.