Question

Q3 [Number Searching] (25%) Given two non-negative integers A (data type: int) and B (data type: int), write a C++ program to
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

C++ code in code snippet :

#include <iostream>

using namespace std;
int numberlen(int i)
{
    int n=0;
    while(i>0)
    {
        i=i/10;
        n++;
    }
    return n;
}

int main()
{
    int a,b;
    cin >>a>>b;
    if(a==b)
    {
    cout <<"A equal to B";
    return 0;
    }
    int n=numberlen(b);
    int an=numberlen(a);
    int temp=1,flag=0;
    for(int i=0;i<n;i++)
    {
        temp=temp*10;
    }
    while(an>=n)
    {
        if(a%temp==b)
        {
           flag=1;
            break;
        }
        else
        {
            a=a/10;
            an--;
        }
    }
    if(flag==1)
    cout <<"A contains B";
    else
    cout<<"A does not contain B";
    return 0;
}

Screenshots of code and output:

File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Settings Help Start here x match.cpOutput in another compiler:

Run Debug Stop Share Save {} Beautify Language C++ main.cpp 23 24 25 26 27 - 28 29 30 31 32 33 - 34 35 36 37 38 - 39 40 41 42

Add a comment
Know the answer?
Add Answer to:
Q3 [Number Searching] (25%) Given two non-negative integers A (data type: int) and B (data type:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function: that, given two non-negative integers A and B, returns the number of bits...

    Write a function: that, given two non-negative integers A and B, returns the number of bits set to 1 in the binary representation of the number A * B. For example, given A = 3 and B = 7 the function should return 3, because the binary representation of A * B = 3 * 7 = 21 is 10101 and it contains three bits set to 1. Assume that: In your solution, focus on correctness. The performance of your...

  • Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...

    Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...

  • write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative...

    write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative integer  n and a character  c and return a String consisting of n identical characters that are all equal to c. Write a method  named  printTriangle that receives two integer  parameters  n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...

  • C++ requirements The store number must be of type unsigned int. The sales value must be...

    C++ requirements The store number must be of type unsigned int. The sales value must be of of type long long int. Your program must properly check for end of file. See the section Reading in files below and also see your Gaddis text book for details on reading in file and checking for end of file. Your program must properly open and close all files. Failure to follow the C++ requirements could reduce the points received from passing the...

  • Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C -...

    Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C - A consecutive group of memory chunks. D - None of the choices. Question 2 How many times is the body of the loop executed? int i=1; while(true) { cout << i; if(++i==5) break; } A - Forever B - 4 C - 5 D - 6 E - 0 Question 3 What is wrong with the following piece of...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • Need help with a number guessing game in java 1) You should store prior guessses in...

    Need help with a number guessing game in java 1) You should store prior guessses in a linked list, and the nodes in the list must follow the order of prior guesses. For example, if the prior guesses are 1000, 2111, 3222 in that order, the nodes must follow the same order 2) You should store the candidate numbers also in a linked list, and the nodes must follow the order of numbers (i.e. from the smallest to the largest)....

  • In this assignment you’ll implement a data structure called a trie, which is used to answer...

    In this assignment you’ll implement a data structure called a trie, which is used to answer queries regarding the characteristics of a text file (e.g., frequency of a given word). This write-up introduces the concept of a trie, specifies the API you’re expected to implement, and outlines submission instructions as well as the grading rubric. Please carefully read the entire write-up before you begin coding your submission. Tries A trie is an example of a tree data structure that compactly...

  • 1 Overview The goal of this assignment is to help you understand caches better. You are...

    1 Overview The goal of this assignment is to help you understand caches better. You are required to write a cache simulator using the C programming language. The programs have to run on iLab machines. We are providing real program memory traces as input to your cache simulator. The format and structure of the memory traces are described below. We will not give you improperly formatted files. You can assume all your input files will be in proper format as...

  • Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin...

    Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin at the University of Washington, Seattle, who originally wrote this assignment (for their CSE 142, in Java) This program focuses on classes and objects. Turn in two files named Birthday.cs and Date.cs. You will also need the support file Date.dll; it is contained in the starter project for this assignment. The assignment has two parts: a client program that uses Date objects, and a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT