Example 19: RGB to CMYK Color Conversion

Learn how to convert RGB color to CMYK color.


In the digital world, colours are specified in Red-Green-Blue (RGB) format, with R, G, and B values varying on an integer scale from 0 to 255. On the other hand, in print publishing, the colours are mentioned in Cyan-Magenta-Yellow-Black (CMYK) format, with C, M, Y, and K values varying on a real scale from 0.0 to 1.0.

Write a program that converts RGB color to CMYK color as per the following formulae:






Remember to print the output on the console in the following manner:
  • Invalid RGB Values
  • CMYK = cyan, magenta, yellow, black

Note: If the RGB values are all 0, then the CMY values are all 0, and the K value is 1.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.