You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Thread starter Crutoy37
- Start date

Ad

C:

```
int main(void)
{
int number,remainder,diviser = 1000;
printf("%s","Enter a four digit number: ");
scanf("%d",&remainder);
while(remainder != 0) {
number = remainder / diviser;
remainder %= diviser;
newNumber += ((number + 7) % 10) * diviser;
diviser /= 10;
}
printf("%s%d","New formed number: ",newNumber);
return 0;
}
```

Code:

```
int main(void)
{
int number,newNumber = 0,remainder,diviser = 1000;
printf("%s","Enter a four digit number: ");
scanf("%d",&remainder);
while(remainder != 0) {
number = remainder / diviser;
remainder %= diviser;
newNumber += ((number + 7) % 10) * diviser;
diviser /= 10;
}
printf("%s%d\n","New formed number: ",newNumber);
//swap of the digits
remainder = newNumber % 100;
number = newNumber / 100;
remainder = remainder * 100 + number;
if (remainder < 1000) {
if(remainder < 100){
printf("%s%d","Encrypted number is : 00",remainder);
}else {
printf("\n%s%d","Encrypted number: 0",remainder);
}
}else {
printf("\n%s%d","Encrypted number: ",remainder);
}
return 0;
}
```

Code:

```
int main(void)
{
int number = 0,newNumber = 0,remainder,diviser = 1000;
printf("%s","Enter a four digit number: ");
scanf("%d",&remainder);
//breaking up the numbers into left most digit
while(remainder != 0) {
number = remainder / diviser;
remainder %= diviser;
/*
forming a new number by adding 7 to each digit and
getting its remainder, then forming a new
number by multiplying to appropriate
power with each pass
*/
newNumber += ((number + 7) % 10) * diviser;
diviser /= 10;
}
printf("%s%d\n","New formed number: ",newNumber);
//swap of the digits
remainder = newNumber % 100;
number = newNumber / 100;
remainder = remainder * 100 + number;
printf("\n%s%04d","Encrypted number: ",remainder);
return 0;
}
```

Code:`int main(void) { int number = 0,newNumber = 0,remainder,diviser = 1000; printf("%s","Enter a four digit number: "); scanf("%d",&remainder); //breaking up the numbers into left most digit while(remainder != 0) { number = remainder / diviser; remainder %= diviser; /* forming a new number by adding 7 to each digit and getting its remainder, then forming a new number by multiplying to appropriate power with each pass */ newNumber += ((number + 7) % 10) * diviser; diviser /= 10; } printf("%s%d\n","New formed number: ",newNumber); //swap of the digits remainder = newNumber % 100; number = newNumber / 100; remainder = remainder * 100 + number; printf("\n%s%04d","Encrypted number: ",remainder); return 0; }`

There are 2 divisions in the loop, it can be done without division.

What happens when 1000 is entered? Will newNumber be 8777?

4 digits to process, so the the program should always loop 4 times.

You are right, it breaks down. :-(, I am going to have to do more work on it. Thanks.There are 2 divisions in the loop, it can be done without division.

What happens when 1000 is entered? Will newNumber be 8777?

4 digits to process, so the the program should always loop 4 times.

Ad

It was fixed, thanks for pointing it out.There are 2 divisions in the loop, it can be done without division.

What happens when 1000 is entered? Will newNumber be 8777?

4 digits to process, so the the program should always loop 4 times.

Code:

```
int main(void)
{
int number = 0,newNumber = 0,remainder,diviser = 1000;
printf("%s","Enter a four digit number: ");
scanf("%d",&remainder);
//breaking up the numbers into left most digit
while(diviser != 0) {
number = remainder / diviser;
remainder %= diviser;
/*
forming a new number by adding 7 to each digit and
getting its remainder, then forming a new
number by multiplying to appropriate
power with each pass
*/
newNumber += ((number + 7) % 10) * diviser;
diviser /= 10;
}
printf("%s%d\n","New formed number: ",newNumber);
//swap of the digits
remainder = newNumber % 100;
number = newNumber / 100;
remainder = remainder * 100 + number;
printf("\n%s%04d","Encrypted number: ",remainder);
return 0;
}
```

**Want to reply to this thread or ask your own question?**

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.