The 9th complement of a positive integer A is a positive integer B obtained by subtracting each digit in A from 9.
Write a program that asks the user to enter a positive integer (a negative number should be ignored and the user should be asked to enter a new integer) then calculates and prints its 9th complement.


Difficulty level
Video recording
This exercise is mostly suitable for students
#include <conio.h>
#include <stdio.h>
void main()
{
	int n, comp = 0, p = 1;
	do {
		printf("Entrer un nombre positif: ");
		scanf("%d", &n);
	} while (n<0);

	while (n) {
		comp += p*(9 - n % 10);
		p = p * 10;
		n /= 10;
	}
	printf("Complement= %d", comp);
	getch();
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Deepest element in a binary tree