For each question, you are required to draw a flowchart diagram
and a C++ program. For flow charts you are required to use a
Computer Aided Software Engineering (CASE) tool for example Dia
(Download from http://diainstaller.de/).
a. Write a program that prints the first 107 prime numbers.
b. Write a program that determines whether a number given by the user is a leap year or not.
Please write a program in c++.. Thank you and add the comments for the lines
a)
#include<iostream>
using namespace std;
//method to check if number is prime
bool isPrime(int n) {
//loop to run from 2 to half of n
for(int i = 2; i <= n / 2; ++i)
{
//if number is divisible by any number completely return false
if(n % i == 0)
{
return false;
}
}
//else return true
return true;
}
//method to run the program
int main() {
int countPrimes = 1;//counter holding number of primes printed
int prime = 3;
//printing first prime number
cout<<countPrimes<<": "<<2<<endl;
//printing first 107 prime numbers starting from 3
while(countPrimes < 107) {
//checking if number is prime
if(isPrime(prime)) {
//if its prime, then printing the number and increment the counter.
cout<<(++countPrimes)<<": "<<prime<<endl;
}
//increment the number.
prime++;
}
return 0;
}
Output
1: 2
2: 3
3: 5
4: 7
5: 11
6: 13
7: 17
8: 19
9: 23
10: 29
11: 31
12: 37
13: 41
14: 43
15: 47
16: 53
17: 59
18: 61
19: 67
20: 71
21: 73
22: 79
23: 83
24: 89
25: 97
26: 101
27: 103
28: 107
29: 109
30: 113
31: 127
32: 131
33: 137
34: 139
35: 149
36: 151
37: 157
38: 163
39: 167
40: 173
41: 179
42: 181
43: 191
44: 193
45: 197
46: 199
47: 211
48: 223
49: 227
50: 229
51: 233
52: 239
53: 241
54: 251
55: 257
56: 263
57: 269
58: 271
59: 277
60: 281
61: 283
62: 293
63: 307
64: 311
65: 313
66: 317
67: 331
68: 337
69: 347
70: 349
71: 353
72: 359
73: 367
74: 373
75: 379
76: 383
77: 389
78: 397
79: 401
80: 409
81: 419
82: 421
83: 431
84: 433
85: 439
86: 443
87: 449
88: 457
89: 461
90: 463
91: 467
92: 479
93: 487
94: 491
95: 499
96: 503
97: 509
98: 521
99: 523
100: 541
101: 547
102: 557
103: 563
104: 569
105: 571
106: 577
107: 587
b)
#include <iostream>
using namespace std;
//method to run the program
int main()
{
int year;
//getting number from user
cout << "Enter a year: ";
cin >> year;
// checking if number is leap year
if (year % 4 == 0)
{
if (year % 100 == 0)
{ //if satisfies all conditions, then its leap year
if (year % 400 == 0)
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
}//each else means its not a leap year
else
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";
return 0;
}
Output
Enter a year: 1992
1992 is a leap year.
For each question, you are required to draw a flowchart diagram and a C++ program. For...
For each of the following questions, you are required to draw a flow chart diagram using the right notation that you have learnt regarding the flow chart symbols. Draw a flow chart that can be used to write a program that:(a). Will represent the logic of a program that allows the user to enter a value. The program divides the value by 2 and outputs the result. (b). Will represent the logic of a program that allows the user to...
Develop a functional flowchart and then write a C++ program to
solve the following problem.
1. Create a text file named c1.txt and write your brand of
computer (like Dell, HP, etc) in the file. You will be reading the
name of the file from the keyboard as a string, using the string
class. Your program will also read the brand of your computer from
the keyboard. The process of the file creation (name of the file,
mode for opening...
Jubail University College Computer Science & Engineering Department Assessmen Assignment Course Code CS120/C5101 t Type: 1 Semester: 403 Course Title Programming Submission 27-06-2020 Total Points 8 Date Submission Instructions: • This is an individual assignment. • Please submit your program (Java fle) in Blackboard. You can create one java project, named as Assignment1_id and add separate java file for each question. You can name your javá files as 01.02.... etc. • Make sure that you include your student ID name...
C
program
flow char
Make flow-charts to do the following. Each flowchart element can only pertora one fundamental operation (such as add, compare, input, etc) at each step. Write/print your answers on paper that is stapled together with your CS256 user-nane at the top of each page. 1) (1pt) Input two numbers (N, N), compare then and output the larger of the tvo. 2) (1pt) Get a number (N) as input and output that number squared and cubed. 3) (2pts)...
INFO1112 A11Lab 6March 18, 2021 Note: It is important that you do this lab exercise properly. Please read and follow the instructions very carefully. You need to understand what you are required to do, and complete the program. It uses a one-dimensional array. You can refer to the PowerPoint slides, many of the tasks can be found there, but you need to apply them for this program. Note that the array will be filled with the integers when the user enters them. Consider...
Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...
Write a C++ code based this question n this assignment you will create three additional functions for each one of the data structures created in class. You will be provided with a header file which you will modify and a demo program for each data structure. If your functions are implemented correctly the demo programs should compile and execute correctly. Part 0 (Comments) 1. Over the course of the semester we have discussed comments for each data structure. Please add...
flowchart
Help with c please
Part of college is balancing your studies with your social life. You can't study all the time, but if you don't study at all then you don't stay in college very long. As with any other problem in life, people are always coming up with computerized solutions to help people with these decisions. A flowchart (downloadable below) provides a recommended answer to the question of "do I need to study?" for college students. This program...
NOTE: All C++ programs that you write must have comments at the top with the program name, your name, and the pseudocode describing what the program will do. 1. Create a new program that will calculate the total cost for software purchased from a store. Each software package costs $99.00, but discounts are given on the total cost, based on the number of packages purchased. a. Ask the user for the number of packages they want to buy b. Calculate...