Lab 1 Part 1 : Imploding An Integer

Submission opens on: Sunday 05 September, 00:00:00 hours.
Deadline for Lab 1 Part 1: Tuesday 07 September, 23:59:59 hours.

This part of the lab is worth 1 mark.

For the first lab assignment, your task is to design a C program that reads in a number, and produce as output a corresponding number imploded by value.

For a given number, say, d1d2d3..dN where each di represents a digit from 0 to 9, the corresponding number imploded by value is given as

d1...d1 d2...d2 d3...d3 ...... dN...dN




d1 times d2 times d3 times dN times

where each di is reproduced di times.

For example, the number 123 when imploded gives 122333. The number 305 when imploded gives 33355555 (since 0 is reproduced zero times).


Objective

Write a C program implode.c that repeatedly accepts as input a number that does not exceed nine digits. We shall assume that the user obeys instructions and you do not have to explicitly check the validity of the input. For each input number, the program constructs the following: The program terminates when the user enters a negative number or zero. A skeleton code and sample output is given in the sections that follow. Remember to submit your program before the stipulated deadline of Tuesday 07 September, 23:59:59 hours. The file that you submit must be named implode.c, any other name (e.g. Implode.c, explode.c) is not acceptable.

You must submit your program by logging in to the sunfire UNIX system. The instructions on logging in and submitting are given in Lab 0.

You may wish to know that the Secure Shell Client also has a user-friendly program that allows you to transfer files between your home PC or laptop to your UNIX account. The File Transfer program can be accessed from Secure Shell Client under "Window -> New File Transfer".

Note that you are NOT to use any form of arrays in your program.


Program Skeleton

You may use the following skeleton code if you wish. If you do not find it helpful, do not use it.

#include <stdio.h>

int main()
{
    int number;
    /** Fill in and declare other variables here **/

    /* Input a number */
    printf("Enter a number (not exceeding 9 digits; 0 or less to exit): ");
    scanf("%d", &number);

    while (number > 0)
    {
        /** Fill in your code to implode and print the number here **/

        /** Fill in your code to implode and print the number in REVERSE here **/

        /* Input another number */
        printf("\nEnter a number (not exceeding 9 digits; 0 or less to exit): ");
        scanf("%d", &number);
    }

    /* Terminate program */
    printf("\nProgram terminated\n");
    return 0;
}


Sample Output

The following is a sample session. User input is denoted in bold.

Enter a number (not exceeding 9 digits; 0 or less to exit): 123
Imploding number... 122333
Imploding number in reverse... 333221

Enter a number (not exceeding 9 digits; 0 or less to exit): 0405060
Imploding number... 444455555666666
Imploding number in reverse... 666666555554444

Enter a number (not exceeding 9 digits; 0 or less to exit): -1

Program terminated