[go: up one dir, main page]

0% found this document useful (0 votes)
40 views4 pages

C/C++ - Problem Statement: Constraints

The library department at NCE College of Technology was facing problems issuing and returning books. They decided to develop a new system to assign unique book numbers. The system takes an existing book number as input and outputs a new number that is the minimum next number with the same sum of digits. It also changes the starting alphabet according to the sum of digits in the input number. The C/C++ code solution defines a function that takes the input number, calculates the sum of digits, finds the next number with the same sum, determines the new starting alphabet, and returns the new book number. It returns "INVALID" if certain validation checks fail.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

C/C++ - Problem Statement: Constraints

The library department at NCE College of Technology was facing problems issuing and returning books. They decided to develop a new system to assign unique book numbers. The system takes an existing book number as input and outputs a new number that is the minimum next number with the same sum of digits. It also changes the starting alphabet according to the sum of digits in the input number. The C/C++ code solution defines a function that takes the input number, calculates the sum of digits, finds the next number with the same sum, determines the new starting alphabet, and returns the new book number. It returns "INVALID" if certain validation checks fail.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

C/C++ - Problem Statement

NCE College of Technology is a popular college in the city. In their library department they were facing some problems in issuing and returning the books. They decided to make some changes in the books order and their unique number. So they are in need of developing new system for avoiding their problems. They will give the existing book number; you have to give the new number such a way that, a new book numbers the minimal next one with the same sum of digits and change the alphabet according to the sum and give the new book numbers. Constraints: The input is a combination of one alphabet and numerical values The input should always start with an alphabet. The output is INVALID if there is more than one alphabet or no alphabet. The output numerical combinations should be greater than the input numerical combinations and the output consists equal number of digits as in the input including the alphabet.

Examples
Input1 = J0921 Ouput1 = M0930. As the sum of the digits in the input is 12 the output is the next nearest digit whose sum is 12 The alphabet is obtained in a such a way that J(0+9+2+1)=J(12) J(1+2)=J(3)=J+3=M. Therefore the output is M0930.

Input2 = 67867 Ouput2 = INVALID Input3 = U992 Ouput3 = INVALID Input4 = U0992 Ouput4 =

As there is no numerical combinations greater than the input.

W1199

C/C++ Solution
#include <stdio.h> #include <string.h> #include "Booknumber.h" char* getNewNumber(char* number){ char* alpha = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; char result[20]; char valpha, ralpha[10]; char temp[10]; int len,rlen,i,j,val, rval; int sum=0, tsum=0, vtmp,flag=0; len = strlen(number); for(i=0;i<26;i++) { if(number[0] == alpha[i]) flag = 1; } if(flag == 0) return "INVALID"; for(i=1,j=0;i<len;i++,j++) temp[j] = number[i]; } val = atoi(temp); vtmp = val; for(;vtmp>0;) { sum = sum + (vtmp%10); vtmp = vtmp / 10; } if(sum == 0) return "INVALID"; for(i=val+1;;i++) { vtmp = i; tsum = 0; for(;vtmp >0;) { tsum = tsum + (vtmp%10); {

} END: rval = i; valpha = number[0];

vtmp = vtmp / 10; } if(tsum == sum) goto END;

if(sum > 9) { vtmp = sum; tsum = 0; for(;vtmp > 0;) { tsum = tsum + (vtmp%10); vtmp = vtmp / 10; } sum = tsum; } flag=0; for(i=0,j=0;i<26,j<sum;i++) if(flag == 1) j++; if(alpha[i] == valpha) flag = 1; if(i == 26) i=0; } itoa(rval, result, 10); rlen = strlen(result) + 1; rlen = len - rlen; if(rlen == -1) return "INVALID"; ralpha[0] = alpha[i-1]; ralpha[1] = '\0'; for(i=1;i<=rlen;i++) { ralpha[i] = '0'; ralpha[i+1] = '\0'; } strcat(ralpha, result); char* ptr = (char*)malloc(sizeof(ralpha));; {

strcpy(ptr, ralpha); return ptr;

int main() { char number[] = "U0992"; char* newNo = getNewNumber(number); printf ("\nNew Number : %s",newNo); }

You might also like