C++ Program to find out if a person has won lottery

C++Server Side ProgrammingProgramming

Suppose, there is a jackpot lottery going on where there are 100 tickets, each ticket numbered within a number from 1 to 100. Now, the lottery company has decided only the player with ticket number 20 will win the jackpot prize, and ticket holders of number 11 to 21 will win a consolation prize each. So, we have to design the software for that. Given the ticket number, we have to print any one of these three messages, "Sorry, you have lost.", "You have won the jackpot!!!", and "You have won the consolation prize." The ticket number is supplied by the player, we have to print the message based on the ticket number.

Problem Category

Various problems in programming can be solved through different techniques. To solve a problem, we have to devise an algorithm first, and to do that we have to study the particular problem in detail. A recursive approach can be used if there is a recurring appearance of the same problem over and over again; alternatively, we can use iterative structures also. Control statements such as if-else and switch cases can be used to control the flow of logic in the program. Efficient usage of variables and data structures provides an easier solution and a lightweight, low-memory-requiring program. We have to look at the existing programming techniques, such as Divide-and-conquer, Greedy Programming, Dynamic Programming, and find out if they can be used. This problem can be solved by some basic logic or a brute-force approach. Follow the following contents to understand the approach better.

So, if the input of our problem is like n = 12, then the output will be You have won the consolation prize.

Steps

To solve this, we will follow these steps −

if n <= 10 or n >= 22, then:
print("Sorry, you have lost.")
Otherwise
if n is same as 20, then:
print("You have won the jackpot!!!")
Otherwise,
print("You have won the consolation prize.")

Example

Let us see the following implementation to get better understanding −

#include<bits/stdc++.h>
using namespace std;
void solve(int n) {
if(n <= 10 || n >= 22)
cout<<"Sorry, you have lost.";
else{
if(n == 20)
cout<<"You have won the jackpot!!!";
else
cout<<"You have won the consolation prize.";
}
}
int main() {
int n = 12;
solve(n);
return 0;
}

Input

12

Output

You have won the consolation prize.
Updated on 07-Apr-2022 10:23:07

Advertisements