Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//Im using Lynx operating system Riddler is planning his next caper somewhere an

ID: 3839027 • Letter: #

Question

//Im using Lynx operating system

Riddler is planning his next caper somewhere an Pennsylvania Avenue. In his usual sporting fashion, he has left the address in the form af a puzzle. The address on Pennsylvania is a four-digit nauber where:

All four digits are different

The digit in the thousands place is three times the digit in the tens place

The number is odd

The sum of the digits is 27.

write a C++ program the uses a loop or loops to find the address where the Riddler plans to strike.

Requirments:

Before you start writing the program, think aboat these questions:

How many variables will you need to use?

Do the variables need to be Iintiialized?

What data Type should each varioble have?

// E-Text please

Explanation / Answer

//the code has been written in Dev C++

//the comments will answer questions regarding no. of variables required and their initialization as you //progress through the code

//each data type should be int type because we are required to find address that is an integer.

//sum of digits will be integer as well as sum of integers is an interger

__________________________________________________________________________________________

#include <iostream>
int main()
{
   /*
   *d_th=digit at thousands place
   *d_ten=digit at tens place
   *temp, sum--> required for calculation of sum of digits where 'temp' is temporarily required.
   *num-->taking number to be the first 4 digit odd number
   *num_digit_arr[4]-->to store 4 digits of number for traversing later to check unique digits
   *flag--> a variable initialszed as 0, which will be set as 1 if number's digits are not unique
   */
   int d_th,d_ten,temp, sum,flag, num=1001;
   int num_digit_arr[4];//to store digits of number to check for unique digits later
   while(num<9999)//checking all numbers less than largest 4 digit odd number for given conditions
   {
      
       d_th=num/1000;
       d_ten=(num/10)%10;
       //verifying the second condition specified
       //i.e., digit in tens place should be thrice the digit in tens place
       if(d_th==3*d_ten)
       {
           temp=num;
       sum=0;
       flag=0;//must be inside the loop
           //note: sum should initialised to zero everytime checking for a new number hence
           //it is written inside the loop rather than during variable declaration
           int i=0;
          
           //loop for calculation of sum of digits and storing digits in an array
       while(temp>0)
       {
           num_digit_arr[i]=temp%10;
               sum=sum + temp%10;
           temp=temp/10;
               i++;  
       }
      
       //check unique digits:
       for(int j=0;j<3;j++){
           for(int k=j+1;k<4;k++){
               if(num_digit_arr[j]==num_digit_arr[k]){
                   flag=1;//flag set to 1 indicates digits are not unique
                   }
               }
           }
          
           //verifying first and third conditions specified
       //i.e., sum of digits should be 27  
       if(sum==27&&flag==0)
       {
      
           std::cout<<" The required 4 digit number is : "<<num;
          
       }
          
       }
      
       num=num+2;
       //note: we increment num by 2 because we need to ensure that only odd numbers are checked
       //which is the second condition specified
   }

}

____________________________________________________________________________________________

output:

The required 4 digit number is : 9837