An e‐commerce company is planning to start a new automated order I'd generator system fo book orders. For this purpose a random steing consisting of only digits is only fed to the system. From this random number, the order I'd is generated as a string which is a concatenation of sum of digits at extreme left and extreme right position, then the sum of digits at second left and second last right position and so on,until there is no digit left for consideration. Where there is a single digit then add zero to it .write an algorithm to find the order I'd for the book order
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char sequence[100];
char code[100];
char ch[3];
int digit, sumdig, len;
printf("Enter sequence of digits: ");
scanf("%[^\n]", sequence);
len = strlen(sequence);
for(int i = 0; i < len / 2; i++)
{
sumdig = 0;
digit = sequence[i] - '0';
sumdig += digit;
digit = sequence[(len-1) - i] - '0';
sumdig += digit;
itoa(sumdig, ch, 10);
strcat(code, ch);
}
if (len % 2 != 0)
{
digit = sequence[(len / 2)] - '0';
itoa(sumdig, ch, 10);
strcat(code, ch);
}
printf("\nCode: %s\n", code);
}
Comments
Leave a comment