0% found this document useful (0 votes)
32 views13 pages

Practice 3

The document contains 14 code snippets that involve loops and conditionals to process input numbers. The snippets perform tasks like printing numbers in ranges, calculating sums and averages, checking for digits, and converting between bases. Common patterns include using while loops to iterate as long as a condition is met, and if/else statements to check for valid inputs or satisfy multiple conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views13 pages

Practice 3

The document contains 14 code snippets that involve loops and conditionals to process input numbers. The snippets perform tasks like printing numbers in ranges, calculating sums and averages, checking for digits, and converting between bases. Common patterns include using while loops to iterate as long as a condition is met, and if/else statements to check for valid inputs or satisfy multiple conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1)

#include <iostream>
using namespace std;

int main() {

int n;
cin >> n;

if (n >= 0 and n < 10) {


while (n >= 0) {
cout << n << " ";
--n;
}
}
else {
cout << "error";
}

return 0;
}

2) #include <iostream>
using namespace std;

int main() {

int n,b;
cin >> n;
cin >> b;

if (n < b) {
while (n < 1 + b) {
cout << n << " ";
n++;
}}
else if (n == b) {
cout << n;
}
else {
cout << "error";
}

return 0;
}

3) #include <iostream>
using namespace std;
int main() {
int n;
cin >> n;

if (n >= 0) {
for (int i = 0; i <= n; i += 2) {
cout << i << " ";
}
}
else {
for (int i = n; i <= 0; i += 2) {
cout << i << " ";
}
}

return 0;
}

4) #include <iostream>
using namespace std;
int main() {
int n;
cin >> n;

if (n <0) {
cout << "error";
return 0;
}

long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;

}
cout << result;

5) #include <iostream>
using namespace std;
int main() {

int n;
cin >> n;
if (n < 1) {
cout << "error";
return 0;
}

double b = 0.0;
for (int i = 1; i <= n; i++) {
b += 1.0 / i;
}
cout << b;
}

6) #include <iostream>
using namespace std;
int main() {
int n;
int sum = 0;
cin >> n;

if (n <= 0) {
cout << "error";
return 1;
}

for (int i = 0; i < n; ++i) {


int term = 2 * (i + 1);
if (i % 2 == 0) {
sum += term;
}
else {
sum = sum - term;
}
}

cout << sum;

return 0;
}
7) #include <iostream>
using namespace std;
int main() {
int n;
double sum = 0.0;
cin >> n;
if (n <= 0) {
cout << "error";
return 0;
}

for (int i = 0; i < n; ++i) {


if (i % 2 == 0) {
sum =sum + 1.0 / (2 * i + 1);
}
else {
sum -= 1.0 / (2 * i + 1);
}
}

cout << sum;

return 0;
}
8) #include <iostream>
using namespace std;

int main() {
int sum = 0;
int count = 0;
int input;
cin >> input;
while (input != 0) {
sum += input;
count++;

cin >> input;


}

double average = (double)sum / count;


cout << sum << endl;;
cout << average;

return 0;
}

9) #include <iostream>
using namespace std;

int main() {
int sum = 0;
int input;
cin >> input;

while (input != 0) {
if (input % 2 != 0) {
sum += input;
}
cin >> input;
}
cout << sum << endl;

return 0;
}

10) #include <iostream>


using namespace std;

int main() {
int N;
cin >> N;

if (N <= 0) {
cout << "error";
return 0;
}

int sum = 0;
int factorial = 1;
for (int i = 1; i <= N; i++) {
factorial *= i;
sum += factorial;
}

cout << sum;

11) #include <iostream>


using namespace std;

int main() {
int A,B;
cin >> A >> B;

if (A <= 0 or B <= 0 or A > B) {


cout << "error" << endl;
return 0;

}
else if (A < B) {
for (int i = A; i <= B; i++) {
for (int j = 0; j < i; j++) {
cout << i << " ";
}
cout << endl;
}
}
}

12) #include <iostream>


using namespace std;
int main() {
int a;
cin >> a;

int sum = 0;
while (a > 0 or a <0) {
sum += a % 10;
a /= 10;
}
cout << sum;
}

13) #include <iostream>


using namespace std;
int main() {
int N;
cin >> N;
bool contain = false;

if (N < 0) {
N *= -1;
}
while (N > 0 ) {
if (N % 10 == 2) {
contain = true;
break;
}
else { N /= 10; }
}
if (contain) {
cout << "yes";
}
else {
cout << "no";
}
}

14) #include <iostream>


using namespace std;
int main() {
int a;
cin >> a;

int decimal = 0;
int power_of_two = 1;

while (a > 0) {
int last_digit = a % 10;
decimal = decimal + last_digit * power_of_two;
power_of_two *= 2;
a /= 10;
}

cout << decimal;


}

You might also like