CrazyEngineers
  • 3rd c++ Problem

    Manish Goyal

    Manish Goyal

    @manish-r2Hoep
    Updated: Oct 25, 2024
    Views: 1.1K
    Write a Program to check whether no is prime or not
    without using
    any /,% operator and If statement?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Manish Goyal

    MemberDec 31, 2009

    52 views but no replies...
    Give it a try at least...
    otherwise i will give you a clue
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberDec 31, 2009

    Please give a clue.
    Maybe the problem should not allow use of Decision Control System and loops but allow use of %, /, *, etc.
    Are you sure? This action cannot be undone.
    Cancel
  • Sahithi Pallavi

    MemberDec 31, 2009

    Please give us a clue goyal...!
    Are you sure? This action cannot be undone.
    Cancel
  • Hussanal Faroke

    MemberDec 31, 2009

    isdivis(a,b)
    {
    x=a;
    while(x>b)
    x-=b
    a-b?return 0:return 1;
    }
    main:-
    for(int j=2,j<number;j++)
    isdivis(number,j):{cout<<"prime";break;}:continue;
    Are you sure? This action cannot be undone.
    Cancel
  • Hussanal Faroke

    MemberDec 31, 2009

    Hussanal Faroke
    isdivis(a,b)
    {
    x=a;
    while(x>b)
    x-=b
    b-x?return 0:return 1;
    }
    main:-
    for(int j=2,j<number;j++)
    isdivis(number,j)?{cout<<"not prime";exit(0);}:continue;
    cout<<"prime";
    may be there is some errors in extreme... take ur own modification
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJan 1, 2010

    Guys Hussanal Faroke gave you a great clue
    yeah You can use ternary operator
    Hussanal Faroke
    isdivis(number,j):{cout<<"prime";break;}:continue;
    Hussanal this statement ...seems to be illogical for me
    however nice attempt you are very close..I mean very much close
    Good
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberJan 1, 2010

    Use a loop to check the condition and goto to jump.

    int main ()
    {
    int num ;
    char * array[] = {"even", "odd"} ;
    printf ("Enter any num") ;
    scanf ("%d", &num) ;
    while (num)
    {
    while (num == 1)
    goto label1;
    num -= 2 ;
    }
    label1 : printf ("The number is %s", array[num]) ;
    return 0;
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJan 1, 2010

    gaurav.bhorkar
    Use a loop to check the condition and goto to jump.

    int main ()
    {
    int num ;
    char * array[] = {"even", "odd"} ;
    printf ("Enter any num") ;
    scanf ("%d", &num) ;
    while (num)
    {
    while (num == 1)
    goto label1;
    num -= 2 ;
    }
    label1 : printf ("The number is %s", array[num]) ;
    return 0;
    }
    Hey Gaurav My question was to check whether no is prime or not?
    Please read the Question before
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberJan 1, 2010

    gaurav.bhorkar
    Use a loop to check the condition and goto to jump.

    int main ()
    {
    int num ;
    char * array[] = {"even", "odd"} ;
    printf ("Enter any num") ;
    scanf ("%d", &num) ;
    while (num)
    {
    while (num == 1)
    goto label1;
    num -= 2 ;
    }
    label1 : printf ("The number is %s", array[num]) ;
    return 0;
    }
    char * array[] is the array of pointers to strings.

    printf ("The number is %s", array[num]) ;
    When value of num is 0, it prints "even" because the string "even" is present at position 0 in the array.
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberJan 1, 2010

    Oh I am sorry!

    I didn't read the question properly.

    Sorry!
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJan 1, 2010

    I agree with you but that is fine for checking whether no is even or odd
    absolutely correct No doubt

    But I want to check whether no is prime or not...
    I hope you got it

    for eg

    Please enter no
    20
    sory not a prime no

    Please enter no

    3
    Yes it is a prime no

    Take it easy dude.....The clue is already given....
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJan 2, 2010

    Okk Guys ...I think now i should give you the code ...
    so here it is
    #include<iostream.h>
    #include<conio.h>
    int main()
    {
    int i,no,a,b;
    cout<<"Enter any no"<<endl;
    cin>>no;
    a=no;
    for(i=2;i<no;i++)
    {
        while(no>0)
        {
            no=no-i;
        }
        b=(no==0)?-1:1;
        (b==1)?(no=a):(i=no);
    }
    (b!=-1&no!=1)?cout<<"Prime no":cout<<"Not a prime no";
    getch();
    return 0;
    }
    
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberJan 2, 2010

    Oh! That was easy, wasn't that?

    I was thinking too much.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJan 2, 2010

    yeah Garauv
    It was easy..just you were to show some crazIness....
    Anyhow..thanks for giving it a try
    Are you sure? This action cannot be undone.
    Cancel
  • Hussanal Faroke

    MemberMar 15, 2011

    I don't know how to delete one post that is why i edited like this!!!!!!!!!!!!!!

    Are you sure? This action cannot be undone.
    Cancel
  • aicbal

    MemberMar 25, 2011

    int main()
    {
    int num,i,j,flag=FALSE;
    printf("Enter a number: ");
    scanf("%d",&num);
    for(i=2;i<num;i++)
    {
    j=num/i;
    j=0?flag=TRUE:;
    }
    flag=TRUE?printf("Not Prime");printf("Prime");
    }


    #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register