
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ code to find out if an image is B/W or color
Suppose, we are given an image that contains n pixels. The pixels can be of the following color −
'C' (cyan)
'M' (magenta)
'Y' (yellow)
'W' (white)
'G' (grey)
'B' (black)
The color of the i-th pixel is given in the string 'pixels'. Given the string, we have to find out if the given photograph is colorful or black and white. If it is a color photograph it will contain at least one pixel of color 'C', 'M' and 'Y' and we will print 'Color'; otherwise, it will contain pixels of color 'W', 'G', 'B' only and we will print 'BW'.
So, if the input is like n = 10, pixels = "GBWYM", then the output will be Color.
Steps
To solve this, we will follow these steps −
for initialize i := 0, when i < n, update (increase i by 1), do: if pixels[i] is not equal to 'B' and pixels[i] is not equal to 'W' and pixels[i] is not equal to 'G', then: print("Color") return print("BW")
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; #define N 100 void solve(int n, string pixels ) { for (int i = 0 ; i < n; i++) { if(pixels[i]!='B' && pixels[i]!='W' && pixels[i]!='G') { cout<<"Color"; return; } } cout<<"BW"; } int main() { int n = 10; string pixels = "GBWYM"; solve(n, pixels); return 0; }
Input
10, "GBWYM"
Output
Color
- Related Articles
- C++ code to find out if a name is male or female
- C++ code to find out if a grid is fully accessible
- C++ code to find out if everyone will get ice cream
- How to find out if the GPS of an Android device is enabled or not?
- C++ code to find out who won an n-round game
- How to find out if an element is hidden with JavaScript?
- How to convert 3-digit color code to 6-digit color code using JavaScript?
- How to check if an image contour is convex or not in OpenCV Python?
- C++ code to find out number of battery combos
- An electric bulb is rated $220\ V$ and $100\ W$. When it is operated on $110\ V$, the power consumed will be$(a)$ 100 W$(b)$ 75 W$(c)$ 50 W$(d)$ 25 W
- C++ code to find out which number can be greater
- How to Invert the color of an image using JavaFX?
- How to find out if capslock is on inside an input field with JavaScript?
- How to apply pseudo color schemes to an image plot in Matplotlib?
- Resistor Types and Color Code

Advertisements