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
Firlakuza [10]
3 years ago
15

Write a c++ program to print even numbers from 1 to 20​

Computers and Technology
1 answer:
Novay_Z [31]3 years ago
8 0

Answer:

#include <bits/stdc++.h>

using namespace std;

// Function to print even numbers

void printEvenNumbers(int N)

{

cout << "Even: ";

for (int i = 1; i <= 2 * N; i++) {

// Numbers that are divisible by 2

if (i % 2 == 0)

cout << i << " ";

}

}

// Function to print odd numbers

void printOddNumbers(int N)

{

cout << "\nOdd: ";

for (int i = 1; i <= 2 * N; i++) {

// Numbers that are not divisible by 2

if (i % 2 != 0)

cout << i << " ";

}

}

// Driver code

int main()

{

int N = 20;

printEvenNumbers(N);

printOddNumbers(N);

return 0;

}

Explanation:

Note: This will find both odd and even numbers, you have to change the number above to the number of your choice

For even numbers

Even number are numbers that are divisible by 2.

To print even numbers from 1 to N, traverse each number from 1.

Check if these numbers are divisible by 2.

If true, print that number.

For odd numbers

Odd number are numbers that are not divisible by 2.

To print Odd numbers from 1 to N, traverse each number from 1.

Check if these numbers are not divisible by 2.

If true, print that number

You might be interested in
Consider the two computers A and B with the clock cycle times 100 ps and 150 ps respectively for some program. The number of cyc
Kipish [7]

Answer:

Option d) B is 1.33 times faster than A

Given:

Clock time, t_{A} = 100 ps

t_{A} = 150 ps

No. of cycles per instructions,  n_{A} = 2.0

n_{B} = 1.0

Solution:

Let I be the no. of instructions for the program.

CPU clock cycle, f_{A} = 2.0 I

CPU clock cycle, f_{B} = 1.0 I

Now,

CPU time for each can be calculated as:

CPU time, T = CPU clock cycle\times clock time

T_{A} = f_{A}\times t_{A} = 2.0 I\times 100 = 200 I ps

T_{B} = f_{B}\times t_{B} = 1.0 I\times 100 = 150 I ps

Thus B is faster than A

Now,

\frac{Performance of A}{Performance of B} = \frac{T_{A}}{T_{B}}

\frac{Performance of A}{Performance of B} = \frac{200}{150}

Performance of B is 1.33 times that of A

7 0
2 years ago
Read 2 more answers
Consider the following language:
VMariaS [17]

L is a decidable language because the Turing machine accepts it.

L is a recognizable language if  TM M recognizes it.

<h3>How do you know if a language is decidable?</h3>

A language is said to be decidable only when there seems to exists a Turing machine that is said to accepts it,

Here, it tends to halts on all inputs, and then it answers "Yes" on words that is seen in the language and says "No" on words that are not found in the language. The same scenario applies to recognizable language.

So,  L is a decidable language because the Turing machine accepts it.

L is a recognizable language if  TM M recognizes it.

Learn more about programming language from

brainly.com/question/16936315

#SPJ1

8 0
1 year ago
#Write a function called is_composite. is_composite should #take as input one integer. It should return True if the #integer is
malfutka [58]

Answer:

// A optimized school method based C++ program to check  

// if a number is composite.  

#include <bits/stdc++.h>  

using namespace std;  

bool isComposite(int n)  

{  

// Corner cases  

if (n <= 1) return false;  

if (n <= 3) return false;  

// This is checked so that we can skip  

// middle five numbers in below loop  

if (n%2 == 0 || n%3 == 0) return true;  

for (int i=5; i*i<=n; i=i+6)  

 if (n%i == 0 || n%(i+2) == 0)  

 return true;  

return false;  

}  

// Driver Program to test above function  

int main()  

{  

isComposite(11)? cout << " true\n": cout << " false\n";  

isComposite(15)? cout << " true\n": cout << " false\n";  

return 0;  

}

Explanation:

3 0
2 years ago
Write a Python program that uses function(s) for writing to and reading from a file:
swat32

import random

def random_number_file_writer(nums):

   f = open("random.txt", "w")

   i = 0

   while i < nums:

       f.write(str(random.randint(1,500))+"\n")

       i += 1

   f.close()

def random_number_file_reader():

   f = open("random.txt", "r")

   total = 0

   count = 0

   for x in f.readlines():

       total += int(x)

       count += 1

   print("The total of the numbers is "+str(total))

   print("The number of random numbers read from the file is "+str(count))

def main():

   random_number_file_writer(int(input("How many random numbers do you want to generate? ")))

   random_number_file_reader()

main()

I hope this helps!

6 0
3 years ago
Choose all the answers that apply.
den301095 [7]
[1] is false, because we are talking about light reaching Earth's surface as a whole, and not in one specific place.

[2] is false, because even if it was in the middle of the night, the other side of the world will be recieving pretty much just as much light as you did the following day.

[3] is false, because even though the sun comes up lower during Winter months, and higher during Summer months, seasons are not constant throughout the entire world. So for what may be Winter for you, will likely be Summer for someone else, so still, just as much light is reaching the Earth.

[4] is false, because wind doesn't affect how light travels.

[5] is true, because clear skies will allow more light to come through that cloudy skies, for example, meaning the amount of light reaching the Earth's surface would not be consistent.
7 0
3 years ago
Read 2 more answers
Other questions:
  • We have a user Sally Smith who we want to grant the ability to launch applications and browse folders. But we do not want her to
    15·1 answer
  • High quality pages in a task should all get the same Needs Met rating. For example, a high quality page for a common interpretat
    9·1 answer
  • If you plan on operating several applications at s time, the amount of RAM should be considered when purchasing a computer
    10·2 answers
  • In an inheritance situation, the new class that you create from an existing class is known as the:
    5·1 answer
  • The prediction that the number of transistors on a chip would double about every two years is known as ________. Metcalfe's law
    6·1 answer
  • Design an if-then statement ( or a flowchart with a single alternative decision structure that assigns 20 to the variable y and
    5·1 answer
  • Chantelle wants to change the color scheme for her company's web app, and she needs to get the logos updated. What kind of devel
    8·1 answer
  • Future Cell Tech, a company that produces cell phones, is always on top of the newest technology and is the leader in cell phone
    6·1 answer
  • If you want to stop a loop before it goes through all of its iterations, the break statement may be used. Group of answer choice
    5·1 answer
  • Does the source MAC address match your PC interface?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!