C programme

Discussion in 'C++' started by archana_198224, Feb 1, 2007.

  1. archana_198224

    archana_198224 New Member

    Joined:
    Feb 1, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    I wanna create a c program that the menu to be displayed to the user. On selecting a choice display appropriate result. Number should be accepted from the user.

    Menu

    1. Prime Factors

    2. Leap Year

    3. Sum of all digits

    4. Number in reverse order
     
  2. friendsforniraj

    friendsforniraj New Member

    Joined:
    Nov 24, 2006
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    studying
    use switch and case statement
     
  3. piyush_1502

    piyush_1502 New Member

    Joined:
    Feb 7, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    4. Number in reverse order[/QUOTE]

    Source Code
    Code:
    #include <stdlib.h>
    #include <iomanip.h>
    #include <iostream.h>
    #include <math.h>
    
    int number;
    int reverse(int);
    
    main()
    {
        cout << "Please enter an Integer value of 3 digits or less = ";
        cin >> number;
        cout << "The number with its digits reversed is = ";
        number=abs(number);
    
        if (number<10)
            cout << setw(1) << setfill('0') << reverse(number) << endl;
        else if (number<100)
            cout << setw(2) << setfill('0') << reverse(number) << endl;
        else if (number<1000)
            cout << setw(3) << setfill('0') << reverse(number) << endl;
        else
            cout << "Sorry, number is not valid.";
            return 0;
    }
    
    int reverse(int number)
    {
        int digit1, digit2, digit3, digit4;
    
        if (number<10)
            return number;
        else if (number<100)
        {
            digit1=div(number,10).quot;
            digit2=div(number,10).rem;
            return (digit2*10 + digit1);
        }
        else if (number<1000)
        {
            digit1=div(number,100).quot;
            number=div(number,100).rem;
            digit2=div(number,10).quot;
            digit3=div(number,10).rem;
            return (digit3*100 + digit2*10 + digit1);
        }
    
        return 0;
    }
    --------------------------------------------------------------------------------

    Sample Run
    Please enter an Integer value of 3 digits or less = 123
    The number with its digits reversed is = 321


    --------------------------------------------------------------------------------

    Program Notes
    This program ask the user to enter a number of 3 digit or less, it read in the number, send it through a nested If statement, than the number and its condition is sent to the reverse function that return it in its reversed order. It works perfectly without any errors or warnings
     
    Last edited by a moderator: Feb 8, 2007
  4. ishathkaja

    ishathkaja New Member

    Joined:
    Feb 4, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hai this is ishathkaja..
    Another prog for reversing of number(no restriction for giving number)
    you may give any digit of number.
    Code:
    //program to find the reverse for given string of number
    #include<conio.h>
    #include<stdio.h>
    void main()
    {
    int n,a=0,b;
    clrscr();
    printf("Enter ur number");
    scanf("%d",&n);
          while(n>0)
              {
                 b=n%10;
                 a=a*10+b;
                 n=n/10;
               }
      printf("The reverse of given string is %d",a);
      getch();
    }
    try this and give me a reply
     
    Last edited by a moderator: Feb 8, 2007
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    I would suggest that some of you discover code tags and their usage. Personally, I won't read unformatted ugly.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What he meant is the point No 7 in the [thread=168]Before you make a query[/thread] thread.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice