224k views
0 votes
Write a program that prints the U.S. presidential election years from 1792 to present day, knowing that such elections occur every 4 years.

1 Answer

3 votes

Answer:

The cpp program to print US presidential years is given.

#include <iostream>

using namespace std;

int main()

{

// variable to hold presidential election year declared

int elec_year;

// variable to hold presidential election year initialized to 1792

elec_year=1792;

// the value displayed on the console output

std::cout << "The U.S. presidential election years from 1792 to present day are listed. " << std::endl;

// loop executes till year is less than or equals 2020

do

{

// initial value is printed

std::cout << elec_year << std::endl;

// value is incremented by 4

elec_year = elec_year + 4;

}while(elec_year<=2020);

return 0;

}

OUTPUT

The U.S. presidential election years from 1792 to present day are listed.

1792

1796

1800

1804

1808

1812

1816

1820

1824

1828

1832

1836

1840

1844

1848

1852

1856

1860

1864

1868

1872

1876

1880

1884

1888

1892

1896

1900

1904

1908

1912

1916

1920

1924

1928

1932

1936

1940

1944

1948

1952

1956

1960

1964

1968

1972

1976

1980

1984

1988

1992

1996

2000

2004

2008

2012

2016

2020

Step-by-step explanation:

The program works as described.

1. An integer variable, elec_year, is declared and initialized with the first US presidential year value, 1792.

2. Inside the do-while loop, the variable, elec_year, is first printed to the console followed by a new line inserted using, endl.

3. The variable, elec_year, is then incremented by 4.

4. The do-while loop continues till the value of the variable, elec_year, does not exceeds the current year, 2020.

5. The program ends with a return statement since main() has return type int.

6. Only one variable is used in the program.

7. The program consists of only main() method since cpp is not completely object oriented programming language.

8. The same program, if written in object oriented programming language like java or c# will be written inside a class and executed through main() method.

User Myfunnyfella
by
2.9k points