Thread: My first C program

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    3

    My first C program

    Hello, today I started learning C language on my own. I have no previous background in programming.

    I wrote this simple program to reverse a given number (for 1234 it will return 4321).

    I am very proud to announce that it actually works!

    Code:
    void main ()
    
    
    {
    int num;
    int remainder;
    int reversenum=0;
    
    
    printf ("Please enter a number\n");
    scanf ("%d", &num);
    
    
    while (num!=0)
    {
    remainder = num%10;
    num = num/10;
    reversenum = reversenum*10+remainder;
    }
    printf ("\nThe reverse form of your number is %d\n", reversenum);
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Well done! Now let's fix a few things:
    * main returns an int, it should never be void
    * printf, scanf etc need "#include <stdio.h>" at the beginning of the file. It may work as-is, but it's a really bad practice to not include the header files
    * pick a proper indentation to use for your code. Having everything on the same column is terrible for readability

    Don't be discouraged though, if only more newbies wrote code as good as yours. Keep it up!
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-15-2016, 03:29 PM
  2. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  3. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  4. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  5. Replies: 5
    Last Post: 08-16-2007, 11:43 PM

Tags for this Thread