Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT SWIFT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS

Practice C++ Coding Problems


Sign in required

Sign in to your W3Schools account to unlock all problems, earn XP, and track your progress.

Sign in Create account

Improve your skills by solving real C++ coding problems.

Write code, submit, and get instant feedback.

Weekly Problem

Double or Nothing

Double a number if it is positive, otherwise print zero.

Easy 20 XP

main.py
n = int(input())

# Double if positive, otherwise print 0

Tip: Sign in to track your progress.


Free Practice Problems

Problem Description Difficulty XP
Say Hello Read a name from input and print a greeting. Easy 20 XP
Celsius to Fahrenheit Convert a temperature from Celsius to Fahrenheit. Easy 20 XP
Even or Odd Check if a number is even or odd. Easy 20 XP
Make a Username Create a username and initials from a first and last name. Easy 20 XP
Voting Age Determine if a person is old enough to vote. Easy 20 XP
Grade Calculator Convert a score into a letter grade. Easy 20 XP
Multiplication Table Print the multiplication table for a given number. Easy 20 XP
Factorial Create a function that calculates the factorial of a number. Easy 20 XP
Sum of Numbers Read a list of numbers and calculate their sum. Easy 20 XP
Area Calculator Calculate the area of a rectangle, triangle, or circle. Easy 20 XP

More Practice Problems

Sign in to unlock all Practice Problems.

Problem Description Difficulty XP
Shopping Receipt Read item details from input and print a short receipt. Easy 20 XP
Personal Info Read personal details from input and display them. Easy 20 XP
Swap Values Read two values and print them in swapped order. Easy 20 XP
Rectangle Border Print a rectangle border made of stars. Easy 20 XP
Repeat Message Read a message and a number, then print the message that many times. Easy 20 XP
Currency Exchange Calculate a currency exchange from an amount and rate. Easy 20 XP
BMI Calculator Calculate Body Mass Index from weight and height. Easy 20 XP
Circle Properties Calculate the circumference and area of a circle. Easy 20 XP
Simple Calculator Perform basic arithmetic on two numbers. Easy 20 XP
Discount Price Calculate a discounted price from a price and discount percentage. Easy 20 XP
Split the Bill Split a total amount equally among a group of people. Easy 20 XP
Digit Extractor Extract the individual digits of a 3-digit number. Easy 20 XP
Word Counter Count the number of words in a sentence. Easy 20 XP
Shout It Out Convert a string to uppercase and print its length. Easy 20 XP
First and Last Print the first and last character of a word. Easy 20 XP
Repeat String Read a string and a number, then print the string repeated that many times. Easy 20 XP
Range Checker Check if a number falls within a given range. Easy 20 XP
Password Check Check if a password is long enough. Easy 20 XP
Ticket Price Determine the ticket type and price based on age. Easy 20 XP
Positive Negative Zero Check if a number is positive, negative, or zero. Easy 20 XP
Smallest of Three Find the smallest of three numbers. Easy 20 XP
Countdown Count down from a number to 1 and print Go! Easy 20 XP
FizzBuzz Implement the classic FizzBuzz challenge. Easy 20 XP
Sum 1 to N Calculate the sum of all numbers from 1 to N. Easy 20 XP
Star Triangle Print a right triangle made of stars. Easy 20 XP
Tip Calculator Create a tip calculator function for a restaurant bill. Easy 20 XP
Power Function Write a function that calculates the power of a number. Easy 20 XP
Average Score Read a list of scores and calculate their average. Easy 20 XP
Count Matches Count how many numbers in a list match a target value. Easy 20 XP
Min and Max Find the smallest and largest numbers in a list. Easy 20 XP
Absolute Difference Calculate the absolute difference between two numbers. Easy 20 XP
Round Up or Down Round a decimal number to the nearest whole number. Easy 20 XP
Double or Nothing Double a number if it is positive, otherwise print zero. Easy 20 XP
Leap Year Determine if a given year is a leap year. Medium 50 XP
Day of Week Convert a day number to a day name. Medium 50 XP
Triangle Classifier Classify a triangle based on its side lengths. Medium 50 XP
Time Converter Convert a number of seconds into hours, minutes, and seconds. Medium 50 XP
Reverse Words Reverse the order of words in a sentence. Medium 50 XP
Palindrome Check Check if a word reads the same forwards and backwards. Medium 50 XP
Count Vowels Count the number of vowels in a string. Medium 50 XP
Title Case Capitalize the first letter of each word in a sentence. Medium 50 XP
Remove Duplicates Remove consecutive duplicate characters from a string. Medium 50 XP
Longest Word Find the longest word in a sentence. Medium 50 XP
Access Control Determine if a person gets access based on role, age, and membership. Medium 50 XP
Digit Sum Calculate the sum of all digits in a number. Medium 50 XP
Number Pyramid Print a pyramid of numbers. Medium 50 XP
Collatz Steps Count the steps to reach 1 using the Collatz sequence. Medium 50 XP
Prime Check Check if a number is a prime number. Medium 50 XP
Reverse Number Reverse the digits of a number. Medium 50 XP
Is Palindrome Function Write a function that checks if a word is a palindrome. Medium 50 XP
Min of Three Write a function that returns the smallest of three numbers. Medium 50 XP
Count Digits Write a function that counts the number of digits in a number. Medium 50 XP
Recursive Sum Write a recursive function to sum numbers from 1 to N. Medium 50 XP
Array Map Function Write a function that doubles every number in a list. Medium 50 XP
Find the Largest Read a list of numbers and find the largest one. Medium 50 XP
Reverse a List Read a list of numbers and print them in reverse order. Medium 50 XP
Second Largest Find the second largest number in a list. Medium 50 XP
Running Sum Print the running (cumulative) sum of a list of numbers. Medium 50 XP
Count Above Average Count how many numbers are above the average. Medium 50 XP
Longest Streak Find the longest consecutive streak of the same number. Medium 50 XP
Greatest Common Divisor Find the greatest common divisor (GCD) of two numbers. Medium 50 XP
Fibonacci Print the first N numbers of the Fibonacci sequence. Medium 50 XP
Prime Factors Find all prime factors of a number. Medium 50 XP
Power of Two Check if a number is a power of 2. Medium 50 XP
Sum of Squares Calculate the sum of squares from 1 to N. Medium 50 XP
LCM Calculator Calculate the least common multiple of two numbers. Medium 50 XP
Perfect Number Check if a number is a perfect number. Medium 50 XP
Sort Three Numbers Read three numbers and print them in ascending order. Medium 50 XP
Sort Numbers Read a list of numbers and print them sorted. Medium 50 XP
Sort Words Read a list of words and print them sorted alphabetically. Medium 50 XP
Letter Frequency Count how many times each letter appears in a word. Medium 50 XP
Most Common Char Find the character that appears most often in a string. Medium 50 XP
Digit Frequency Count how many times each digit appears in a number. Medium 50 XP
Caesar Cipher Encrypt a message by shifting each letter in the alphabet. Hard 100 XP
Anagram Check Check if two words are anagrams of each other. Hard 100 XP
Run Length Encoding Compress a string using run-length encoding. Hard 100 XP
Date Validator Check if a given date is valid. Hard 100 XP
Diamond Pattern Print a diamond pattern of stars. Hard 100 XP
Number to Binary Convert a decimal number to binary. Hard 100 XP
Pascal Triangle Row Print a specific row of Pascal's triangle. Hard 100 XP
Remove Duplicates Remove duplicate numbers from a list while keeping order. Hard 100 XP
Rotate Left Rotate a list of numbers to the left by K positions. Hard 100 XP
Merge Sorted Merge two sorted lists into one sorted list. Hard 100 XP
Pair Sum Find the first pair of numbers that add up to a target. Hard 100 XP
GCD Function Write a recursive function to find the GCD of two numbers. Hard 100 XP
Number Base Convert Convert a number from one base to another. Hard 100 XP
Bubble Sort Implement bubble sort and count the number of swaps. Hard 100 XP
Selection Sort Implement selection sort and print each step. Hard 100 XP
Insertion Sort Implement insertion sort and print each step. Hard 100 XP
Word Frequency Count how many times each word appears in a sentence. Hard 100 XP
Unique Elements Count Count how many unique numbers appear in a list. Hard 100 XP

Sign in to access all Practice Problems and track your progress.

Sign In

How it Works

Each challenge gives you starter code in C++. Your program reads input and prints the correct output.

When you submit, your code is tested against multiple hidden test cases with different inputs.

Your score is based on how many tests pass — partial credit is given for partially correct solutions.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.