1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Gnom [1K]
3 years ago
8

Lucky Sevens. Given a whole number, compute and display how many digits in the number are 7s. For example, the number of 7s in 3

482 is 0, while the number of 7s in 372771 is 3. Note: whole numbers are non-negative integers starting at zero {0, 1, 2, 3, 4, …}
Computers and Technology
1 answer:
Sveta_85 [38]3 years ago
6 0

Answer:

Code is in both c++ and java

Explanation:

C++ Code

#include<iostream>  

#include <sstream>  // for string streams  

#include <string>  // for string  

using namespace std;  

int main()  

{  

   int num;  

   cout<<"Enter Non-Negative number :";

  cin>> num;

   // declaring output string stream  

   ostringstream str1;  

   // Sending a number as a stream into output  

   // string  

   str1 << num;  

   // the str() coverts number into string  

   string geek = str1.str();  

   int count =0;

  for (int i = 0; i < geek.size(); i++){

    if(geek[i]=='7'){

     count++;  

 }

}

   // Displaying the string  

   cout << "the number of 7s in "<<num<< "is"<<count;

 

   return 0;  

}

Java Code

import java.util.Scanner;

public class LuckySeven{

   public static void main(String[] args){

       Scanner scanner = new Scanner(System.in);

       System.out.print("Enter Number :");

       int number = scanner.nextInt();

       String numberString = number+"";

       int count =numberString.length()- numberString.replaceAll("7","").length();

       System.out.println("Number of 7's in "+numberString+" are "+count);

   }

}

Code Explanation

First we need to take integer number as input from user.

As we need to iterate through all of the digits in number, that's why we need to convert integer to string as string is an array of character so we will iterate through number array and match the total count of 7 on every index.

At the end we will display the total count of 7 in number.

Sample Output

Case 1

Enter Non-Negative number :3482

the number of 7s in 3482 is 0

Case 2

Enter Non-Negative number :372771

the number of 7s in 372771 is 3

You might be interested in
When societies and governments make decisions, choices have to be made. Which choice is an example of opportunity cost?
ollegr [7]

Answer:

working

Explanation:

im typing you a paragraph.and i made this an answer so i can edit it.

4 0
4 years ago
Explain the working principle of computer? can anyone tell​
Crazy boy [7]

Answer:

input process and output hehe

4 0
3 years ago
In this exercise, first run the code as it is given to see the intended output. Then trace through each of the 3 variables to se
dlinn [17]

Answer:

The memory with variable names str1, str2, and str3 all have equal and the same value after the first if-statement.

Explanation:

The str1 was first assigned a null value while the str2 and str3 were assigned the string value "Karen" with the String class and directly respectively. On the first if-statement, the condition checks if the str1 is null and assigns the value of the variable str2 to str1, then the other conditional statement compares the values of all the string variables.

3 0
3 years ago
What word does<br> this pattern spell?<br> d.
Advocard [28]

Answer:

BEG

Explanation:

In traditional music theory, pitch classes are typically represented by first seven Latin alphabets (A,B,C,D,E ,F and G) . And in the below music notes attachment we can understand that the answer is option (a) BEG

7 0
4 years ago
Write a function called middle(string str) that returns a string containing the middle character in str if the length of str is
jenyasd209 [6]

Answer:

Note: a) If the length of the string is odd there will be two middle characters.

Explanation:

b) If the length of the string is even there will be one middle character. There was a problem connecting to the server. Please check your connection and try running the trinket again.

3 0
2 years ago
Other questions:
  • Suppose you are the security manager of a company and one of your goals is to design security mechanisms based on three security
    10·1 answer
  • Having plug and play support means you can plug in a device, turn on the computer, and then immediately begin using the device t
    11·1 answer
  • What is the problem with the code snippet below? public class Test { public static void main(String[] args) { System.out.println
    6·1 answer
  • Write a method called makeStars. The method receives an int parameter that is guaranteed not to be negative. The method returns
    14·1 answer
  • Rick is teaching a photographic worship about sun color and image. He shows the students a few images to teach them about contra
    12·1 answer
  • How network diagram help in scheduling a project? Draw activity network diagram as per given
    7·1 answer
  • 2 4. What is one way to prepare for building a project budget? (1 point)​
    9·1 answer
  • Do you know the energy unit question?
    10·1 answer
  • Priortization is an example of a skill that helps you reach long term goals because
    14·1 answer
  • Five real world objects with similar characteristics​
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!