Home > Software engineering >  The full output is not being printed
The full output is not being printed

Time:02-03

im not getting an output for this program, please help THE QUESTION: Twin primes are consecutive odd numbers both of which are prime numbers. Write a program which inputs two positive integers A and B and outputs all twin primes in range A to B.

The code is:

#include <stdio.h>

int main(){
    int num1,num2,temp,i,p1,p2;
    printf("Enter 2 numbers: ");
    scanf("%d %d",&num1,&num2);
    p1=0;
    p2=0;
    printf("Twin prime numbers are:");
    if(num1>num2){
        temp=num1;
        for(i=num2;i<temp;i=i 1){
            int j;
            for(j=2;j<i;j=j 1){
                if(i%j!=0){
                    p1=1;
                }
            }
            int k;
            for(k=2;k<(i 2);k=k 1){
                if((i 2)%k!=0){
                    p2=1;
                }
            }
            if(p1==1 && p2==1 && (i 2)<=num2){
                printf("\n%d and %d",i,i 2);
            }
        }
    }
    else{
        temp=num2;
        for(i=num1;i<temp;i=i 1){
            int j;
            for(j=2;j<i;j=j 1){
                if(i%j==0){
                    p1=1;
                }
            }
            int k;
            for(k=2;k<(i 2);k=k 1){
                if((i 2)%k==0){
                    p2=1;
                }
            }
            if(p1==1 && p2==1 && (i 2)<=num1){
                printf("\n%d and %d",i,i 2);
            }
        }
    }
    return 0;
}

OUTPUT:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\shaun\OneDrive\Desktop> cd "c:\Users\shaun\OneDrive\Desktop\c programming\programs\" ; if ($?) { gcc twin_primes.c -o twin_primes } ; if ($?) { .\twin_primes }
Enter 2 numbers: 8 19
Twin prime numbers are:
PS C:\Users\shaun\OneDrive\Desktop\c programming\programs> 

CodePudding user response:


if(p1==1 && p2==1 && (i 2)<=num2)
if(p1==1 && p2==1 && (i 2)<=num1)

You've mixed up with num2 & num1 in above 2 conditions.

If you use num1 instead of num2 and num2 instead of num1, you'll get the output.


Apart from this, there're several logical errors in code.

  1. Logic to find prime number
  2. Initializing variable p1 and p2 in proper place

To solve above 2 problems, I would recommend to learn more about finding prime numbers. I'll add some resources at below, please check those.


Another thing, you're basically writing same code twice. You could make it more simple by just swapping 2 given values when first one is larger than the second one.


Lastly, I'm giving a sample code here. It's one of the way you could follow to write simple but easily understandable solution for your problem.

Sample code:

#include <stdio.h>
#include <stdbool.h>
bool isPrime(int num)
{
    for(int i = 2; i * i <= num; i  )
    {
        if (num % i == 0) return false;
    }
    return true;
}

int main()
{
    int num1,num2,temp,i,p1,p2;
    printf("Enter 2 numbers: ");
    scanf("%d %d",&num1,&num2);
    p1=0;
    p2=0;
    printf("Twin prime numbers are:");
    if (num1 > num2)
    {
        // swap the numbers. always keeping the first as the smaller one
        int temp = num1;
        num1 = num2;
        num2 = temp;
    }

    if (num1 == 1) num1  ;  // ignoring 1

    for(int i = num1; i <= num2 - 2; i  )   // running the loop till num2 - 2, so we won't have to check whether i   2 <= num2
    {
        if (isPrime(i) && isPrime(i 2))
        {
            printf("\n%d and %d",i,i 2);
        }
    }
    return 0;
}

Please check below URLs to learn more about prime number and swapping values

  •  Tags:  
  • Related