Skip to content

Commit d28d468

Browse files
authored
Added Solutions for Problem 27-32
1 parent 2396979 commit d28d468

6 files changed

+136
-0
lines changed

C++/27_The_last_2_digits.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//Given a number N. Print numbers from 1 to N in separate lines.
2+
3+
#include <iostream> //iostream is a header file that contains basic input/output functions
4+
using namespace std; //std is the standard namespace
5+
6+
int main() //main function
7+
{
8+
int n; //declaring a variable n
9+
cin >> n; //taking input from user
10+
for (int i = 1; i <= n; i++) //for loop
11+
{
12+
cout << i << endl; //printing the numbers
13+
}
14+
return 0; //returning 0
15+
//end of program
16+
}

C++/28_1_to_N.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//Given a number N. Print all even numbers between 1 and N inclusive in separate lines.
2+
3+
#include <iostream> //iostream is a header file that contains basic input/output functions
4+
using namespace std; //std is the standard namespace
5+
6+
int main() //main function
7+
{
8+
int n; //declaring a variable n
9+
cin >> n; //taking input from user
10+
for (int i = 2; i <= n; i += 2) //for loop
11+
{
12+
cout << i << endl; //printing the numbers
13+
}
14+
return 0; //returning 0
15+
//end of program
16+
}

C++/29_Even_Numbers.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//Given N numbers. Count how many of these values are even, odd, positive and negative.
2+
3+
#include <iostream> //iostream is a header file that contains basic input/output functions
4+
using namespace std; //std is the standard namespace
5+
6+
int main() //main function
7+
{
8+
int n; //declaring a variable n
9+
cin >> n; //taking input from user
10+
int even = 0, odd = 0, positive = 0, negative = 0; //declaring variables
11+
for (int i = 1; i <= n; i++) //for loop
12+
{
13+
int x; //declaring a variable x
14+
cin >> x; //taking input from user
15+
if (x % 2 == 0) //if x is even
16+
{
17+
even++; //incrementing even
18+
}
19+
else //if x is odd
20+
{
21+
odd++; //incrementing odd
22+
}
23+
if (x > 0) //if x is positive
24+
{
25+
positive++; //incrementing positive
26+
}
27+
else if (x < 0) //if x is negative
28+
{
29+
negative++; //incrementing negative
30+
}
31+
}
32+
cout << even << " " << odd << " " << positive << " " << negative << endl; //printing the numbers
33+
return 0; //returning 0
34+
//end of program
35+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//Given multiple lines each line contains a number X which is a password. Print "Wrong" if the password is incorrect otherwise, print "Correct" and terminate the program.
2+
3+
#include <iostream> //iostream is a header file that contains basic input/output functions
4+
using namespace std; //std is the standard namespace
5+
6+
int main() //main function
7+
{
8+
int x; //declaring a variable x
9+
while (cin >> x) //while loop
10+
{
11+
if (x == 2002) //if x is 2002
12+
{
13+
cout << "Correct" << endl; //printing "Correct"
14+
break; //breaking the loop
15+
}
16+
else //if x is not 2002
17+
{
18+
cout << "Wrong" << endl; //printing "Wrong"
19+
}
20+
}
21+
return 0; //returning 0
22+
//end of program
23+
}

C++/31_Fixed_Password.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//Given a number N, and N numbers, find maximum number in these N numbers.
2+
3+
#include <iostream> //iostream is a header file that contains basic input/output functions
4+
using namespace std; //std is the standard namespace
5+
6+
int main() //main function
7+
{
8+
int n; //declaring a variable n
9+
cin >> n; //taking input from user
10+
int max = -1000000000; //declaring a variable max and assigning it a value of -1000000000
11+
for (int i = 0; i < n; i++) //for loop
12+
{
13+
int x; //declaring a variable x
14+
cin >> x; //taking input from user
15+
if (x > max) //if x is greater than max
16+
{
17+
max = x; //assigning x to max
18+
}
19+
}
20+
cout << max << endl; //printing max
21+
return 0; //returning 0
22+
//end of program
23+
}

C++/32_max.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//Given a number N, and N numbers, find maximum number in these N numbers.
2+
3+
#include <iostream> //iostream is a header file that contains basic input/output functions
4+
using namespace std; //std is the standard namespace
5+
6+
int main() //main function
7+
{
8+
int n; //declaring a variable n
9+
cin >> n; //taking input from user
10+
int max = -1000000000; //declaring a variable max and assigning it a value of -1000000000
11+
for (int i = 0; i < n; i++) //for loop
12+
{
13+
int x; //declaring a variable x
14+
cin >> x; //taking input from user
15+
if (x > max) //if x is greater than max
16+
{
17+
max = x; //assigning x to max
18+
}
19+
}
20+
cout << max << endl; //printing max
21+
return 0; //returning 0
22+
//end of program
23+
}

0 commit comments

Comments
 (0)