twin prime numbers problem

This is a discussion on twin prime numbers problem within the C Programming forums, part of the General Programming Boards category; hi all well this my first ever post here and i wanna learn from here as much i can.. i ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    twin prime numbers problem

    hi all well this my first ever post here and i wanna learn from here as much i can.. i had many chances to learn c but took it for granted this time i really wanna do it.
    still i am a begginer i am not able to develop logics i.e i know things how to do them but i can implement them in C language plz help me....

    from last two days i struggling through a program i dont know how should i solve it

    program is:- two prime no.s are said to be twin if they differ by 2 such as 11 and 13,17-19,etc
    WAP to print all TWO digit twin prime numbers

    I cud just try how to check whether a number is prime or not plz check my code
    Code:
    #include<stdio.h>
    main()
    {
              int n,i;
              printf("\n please enter the number to check whether a  prime or not);
              scanf("%d",&n);
              for(i=2;i<n;i++)
                    {
                    if(n%i==0)
                    {
                     printf("\n no. is not prime");
                    return;
                     }   
                    else 
                         {
                          if(n==i)
                          printf(" no is prime);
    }}}


    please do tell me how can i make problems solving simpler. do guide me please i am dying to learn c

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    Code:
              for(i=2;i<n;i++)
                    {
                    if(n%i==0)
                    {
                     printf("\n no. is not prime");
                    return;
                     }   
                    else 
                         {
                          if(n==i)   ///////////// Will this ever be true?? 
                          printf(" no is prime);
    Jim

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    rechecking

    Code:
    #include<stdio.h>
    main()
    {
              int n,i;
              printf("\n please enter to check whether a  prime or not);
              scanf("%d",&n);
              for(i=2;i<n;i++)
                    {
                    if(n%i==0)
                    {
                     printf("\n no. is not prime");
                    break;
                     }   
                    else 
                         {
                          if(n=i)
                          printf(" no is prime);
    }}}
    Last edited by singh751; 10-17-2010 at 10:37 AM. Reason: syntax mistake

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    singh751, it is good that you posted in code bbcode tags, but you really need to format your code properly, e.g.,
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int n, i;
        printf("\n please enter to check whether a  prime or not);
        scanf("%d", &n);
        for (i = 2; i < n; i++)
        {
            if (n % i == 0)
            {
                printf("\n no. is not prime");
                break;
            }   
            else 
            {
                if (n == i)
                    printf(" no is prime);
            }
        }
        return 0;
    }
    I added the return type of the main function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    My point in my fist post is:
    Code:
    #include <stdio.h>
    
    int main()
    {
       int a, n;
       for(a= 0; a < n;n++)
       {
          if(a == n)
          {
             printf("Hi.");
          }
       }
       return(0);
    }
    Does this print "Hi"?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    First, read this Sieve of Eratosthenes - Wikipedia, the free encyclopedia

    Second write a function with this prototype
    int isPrime ( int n );
    It returns 1 if the number is prime, and 0 if it is not prime.

    Before doing the second part of your assignment, make sure this function works by just printing each 2...n as being prime or non-prime
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Quote Originally Posted by Salem View Post
    First, read this Sieve of Eratosthenes - Wikipedia, the free encyclopedia

    Second write a function with this prototype
    int isPrime ( int n );
    It returns 1 if the number is prime, and 0 if it is not prime.

    Before doing the second part of your assignment, make sure this function works by just printing each 2...n as being prime or non-prime
    dear i am just a beginner i have not yet started functions
    but i will try to do this with functions when i am done with basic concepts so please help me

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Or just try to do the same thing in main() - print each number and whether it is prime or not.

    Divide the problem into steps, and tackle each one in turn.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with adding up numbers from a txt file
    By Leo12 in forum C Programming
    Replies: 1
    Last Post: 02-18-2009, 01:50 PM
  2. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 09:36 AM
  3. Prime Numbers and Array...
    By Deux in forum C Programming
    Replies: 12
    Last Post: 12-20-2004, 04:12 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. Prime numbers
    By Lazy Student in forum C Programming
    Replies: 12
    Last Post: 05-14-2002, 06:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21