Home > front end >  A Program to print the last n, (n 1), (n 2),....(n 31) number of digits of the number input by the u
A Program to print the last n, (n 1), (n 2),....(n 31) number of digits of the number input by the u

Time:10-30


#include <stdio.h>

int main()
{
    signed int x;
    int x1 = 0, x2 = 10, final, loop = 1, y = 10, c;
    printf("Enter the value of X.\n");
    scanf("%d", &x);
    printf("Value Scanned:%d\n", x);
again:
    if (loop <= 32)
    {
        if (x >= x1 && x < x2)
        {
            final = x - x1;
            printf("%d", final);
            y = y * 10;
            x1 = 0;
            x2 = 0;
              loop;
            goto again;
        }

        else
        {
            c = x2 - x1;
            if (x1 == x2)
            {
                x2  = y;
                goto again;
            }
            
            else if (c == y)
            {
                x1  = y;
                x2  = y;
                goto again;
            }
            else
            {
                printf("Error in Process");
                goto ending;
            }
            
        }
    }

    else
    {
        printf("0 error, extra long input");
    }
ending:
    return 0;
}

Flowchart:

How I Imagined it to work

I am a beginner in C-language and only know how to use If-else, Switch, Goto statements, with basic knowledge of how to integrate basic level loops. So please tell me what/where I am wrong instead of telling me how to use arrays because I don't know them, etc. This is my most complex code until now.

Now for Explanation of Coding, I wrote X1 as the lower value and X2 as the upper value while first keeping a difference = Y(initially 10) between them. Continuously increasing the value of X1 and X2 by Y(10) together simultaneously, I will arrive in between an intersection where my x(input) lies. Eg- x=568 then X1 and X2 will keep on increasing until they reach X1 = 560 and X2 = 570, then they will do Final = X(568) - X1(560) and print it. since it can only happen for 32-digits long, so I wrote loop = 0 and only processing my main statement till loop is smaller than or equal to 32, otherwise printing "0 error". then I put Y = Y * 10 every time the value was within my specified range. It should give me the values like Last digit, then last 2 digits, then last 3 digits,etc. but after scanning the value, it isn't exciting at all.

CodePudding user response:

Evaluating what you are attempting to do, I reworked your code to make it a bit more structured without utilizing arrays at that seemed to be something you wanted to avoid at this time. However, these days, the use of the goto statement is usually avoided since functionality such as for loops, do/while loops, and while loops offer better clarity to coding. With that in mind, following is a snippet of code that provides the functionality it looks like you want.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x, x1, x2, y = 10, counter = 0, last_digit;

    printf("Please enter a number: ");
    scanf("%d", &x);

    if (x < 0)          /* Just in case a negative integer is entered */
    {
        x = x * -1;
    }

    while (1)           /* Use a while loop with associated break statements to avoid goto and label statements */
    {
        x1 = 0;
        x2 = 10;
        counter  = 1;

        while (1)
        {
            if (x >= x1 && x <= x2)
            {
                last_digit = x - x1;
                if (counter == 1)
                {
                    printf("The last digit is: %d\n", last_digit);
                }
                else
                {
                    printf("The next digit is: %d\n", last_digit);
                }
                break;
            }
            x1  = y;
            x2  = y;
        }

        x = x / 10;     /* Perform integer division by ten to get to the next digit in the entered number */

        if (x == 0)     /* Once all digits have been processed the outer while loop can be exited */
        {
            break;
        }

    }

    return 0;
}

Following are some key points.

  • As noted, the loop process using goto statements is replaced by two while loops; one while loop nested inside another while loop.
  • Utilizing integer division by ten, each digit can be ascertained and printed.
  • Utilizing the nested while loops with break statements allows for a more compact program.

Utilizing this code snippet, following is a sample test from the terminal.

@Dev:~/C_Programs/Console/LastDigit/bin/Release$ ./LastDigit 
Please enter a number: 479824385
The last digit is: 5
The next digit is: 8
The next digit is: 3
The next digit is: 4
The next digit is: 2
The next digit is: 8
The next digit is: 9
The next digit is: 7
The next digit is: 4

Back in the day, the goto statement had its place in coding, but today it is pretty much an artifact.

Give that a try and see if it meets the spirit of your project.

  • Related