Answer to Question #302587 in C for Shyam

Question #302587

A college has to generate the roll number of the students for the internal examination. The college admin authority has to generate the roll numbers from the registration number of the students. Every student has a registration number. The registration number is a numeric number. The authority has devised a method for generating the roll numbers from the registration number. To generate the roll number, the pairs of adjacent digits are formed from the start of the roll number. From each pair the lexicograpically smallest digit will be removed. If a digit will be left unpaired then that digit will be taken as it is in the roll number.




Write an algorithm for implementing the devised method for the college system to find the roll number from the registration number.

1
Expert's answer
2022-02-25T03:39:35-0500
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char regnumber[100];
    char rezcode[100];
    char ch[5];
    int digit1, digit2, len;    
    printf("Enter registration number: ");
    scanf("%[^\n]", regnumber);        
    
    len = strlen(regnumber);    
    for(int i = 0; i < len - 1; i+=2)
    {                
        digit1 = regnumber[i] - '0';
        digit2 = regnumber[i + 1] - '0';
        if (digit1 < digit2)
            itoa(digit1, ch, 10);
        else itoa(digit2, ch, 10);
        strcat(rezcode, ch);    
    }
    if (len % 2 != 0)
    {
        digit1 = regnumber[len - 1] - '0';
        itoa(digit1, ch, 10);
        strcat(rezcode, ch);    
    }
    printf("\nRoll number: %s\n", rezcode);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS