---
# System prepended metadata

title: Chapter 3 (loops)
tags: [C++ Lab]

---

###### tags: `C++ Lab`
# Chapter 3 (loops)

1. calculate the following sums:
``` 
S1 = 1 + 2 + ... + n
S2 = 1/2 + 2/3 + ... + n/n+1
S3 = -1 +2 -3 + ... + (-1)^n*n
#include <iostream>
#include <cmath>
using namespace std;

int main() {
	int n;
	cout << "Input n: ";
	cin >> n;
	
	double s1;
	for (int i=1;i<=n;i++) {
		s1+=i;
	}
	cout << "s1 = " << s1 << endl;
	
	double s2;
	for (int i=1;i<=n;i++) {
		s2 += (double)i/(i+1);
	}
	cout << "s2 = " << s2 << endl;
	
	double s3;
	for (int i=1;i<=n;i++) {
		s3 += pow(-1,i)*i;
	}
	cout << "s3 = " << s3 << endl;
return 0;
}
```

2-3. Print Fibonacci series
```
#include <iostream>
using namespace std;

int main(){
	int n;
	cout << "Input n: ";
	cin >> n;
	
	int x=1,y=1;
	
	if(n==0) cout << x;
	else if (n==1) cout << x << " " << y;
	else {
		cout << x << " " << y << " ";
		for(int i=1;i<=(n-1);i++){
			cout << x+y<<" ";
			int t=y;
			y=x+y;
			x=t;
	}
}
return 0;
}
```

4. Calculate power not using cmath
```#include <iostream>
#include <iomanip>
using namespace std;

int main(){
	double x,n,t = 1;
	cin>>x>>n;
	for (int i=0;i<n;i++) { t*=x; }
	cout<<fixed<<setprecision(4)<<t<<endl;
return 0;
}
```
5. Print m*n matrix
```
#include <iostream>
using namespace std;

int main() {
	int m, n;
	cin>>m>>n;
	
	for(int i=0;i<m;i++, cout<<endl)
		for(int j=0; j<n; j++)
			cout<<((i*n)+j+1)<<" ";
return 0;
}  
```
6. Print triangle with height n
```
#include <iostream>
using namespace std;

int main() {
	int n;
	cin>>n;
	for (int i=1; i<=n; i++) {
		for(int j=1;j<=i;j++){
			cout<<"*";
			}
		cout<<endl;
	}
return 0;	
}
```

7. Print rectangle with n rows * m column
```
#include <iostream>
using namespace std;

int main() {
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++) {
    	for(int j=1;j<=m;j++) {
    			if (i==1||i==n||j==1||j==m)        
                	cout<<"*";            
            	else
                	cout<<" ";
        }
        cout<<endl;
	}
return 0;
}
```
8. Print pascal triangle with height n+1
```
#include <iostream>
using namespace std;

int main() {
    int rows,num=1;
    cin>>rows;
	
	for(int i=0;i<=rows;i++) {
        for(int j=0;j<=i;j++) {
            (j==0||i==0)?num=1:num=num*(i-j+1)/j;
			cout<<num<<"   ";
        }
        cout<<endl;
    }
return 0;
}
```
9. Calculate sum and avg(input n members's value)
```
#include <iostream>
using namespace std;

int main() {
	double n,time,sum;
	cout<<"Enter the number of athletes in the team: ";
	cin>>n;
	for(int i=1;i<=n;i++){
		cout<<"Input athete #"<<i<<": ";
		cin>>time;
		sum+=time; 
	}
	cout<<"Total: "<<sum<<endl;
	cout<<"Avg: "<<sum/n<<endl; 	
return 0;	
}
```

10. Calculate avg, highest(subject), lowest(subject)
```
#include <iostream>
using namespace std;

int main() {
	int i,t,n,high_t,low_t;
	double temp,score,sum,high=0.0,low=10.0;
	cout<<"Enter the number of subjects in the semester: ";
	cin>>n;
	for(i=1;i<=n;i++){
		cout<<"Input subject #"<<i<<": ";
		cin>>score;
		sum+=score;
		if (score>=high) {
			high=score;
			high_t=i;
			}
		if(score<=low){
			low=score;
			low_t=i;
			}
	}
	cout<<"Avg: "<<sum/n<<endl;
	cout<<"Highest: "<<high<<" ";
	cout<<"Subject: "<<high_t<<endl;
	cout<<"Lowest: "<<low<<" ";
	cout<<"Subject: "<<low_t<<endl;
return 0;	
}
```
11. Find k in 2^k^>n
```
#include <iostream>
#include <cmath>
using namespace std;

int main() {
	int n,k=0;
	cin>>n;
	while(pow(2,k)<=n) k++;
	cout<<k;	
return 0;
}
```
12. Find n in 1/2+1/3+...+1/n<"S"
```
#include <iostream>
using namespace std;

int main() {
	int n;
	double i=2,sum=0;
	cin>>n;	
	while(sum<n) {
		sum+=1/i;
		i++;
	}
	cout<<i-1;	
return 0;
}
```

13. Print 1-100, every 10 numbers have newline
```
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 100; i++) {
        cout << i<<" ";
        if (i % 10 == 0) {
            cout<<endl;
        }
    }
return 0;
}
```

14. Print multiplication table
```
#include <iostream>
using namespace std;

int main() {
	for(int i=1;i<=10;i++,cout<<endl)
		for(int j=1;j<=10;j++)
		cout<<i<<" x "<<j<<" = "<<i*j<<endl;	
return 0;
}
```
15. Calculate following sums:
S0: n! = 1*2*...*n
S1: 1 + 1/2 + ... + 1/n
S2: 1+ 1/2! + ... + 1/n!
```
#include <iostream>
using namespace std;

int main() {
	double n,s0=1,s1=1,s2=1;
	cin >> n;	
	
	for(int i=1;i<=n;i++) {
		s0=s0*i;
		s1=s1+(1/i);
		s2=s2+(1/s0);
	}		
	cout<<s0<<endl;
	cout<<s1<<endl;
	cout<<s2<<endl;
return 0;
}
```
16. find 3-digit numbers, abc=a^3^+b^3^+c^3^ (100*a+10*b+c=a^3^+b^3^+c^3^)
```
#include <iostream>
#include <cmath>
using namespace std;


int main() {
	int a=0,b=0,c=0;
	
	for(a=0;a<10;a++) 
		for(b=0;b<=10;b++) 
			for(c=0;c<=10;c++) 		
				if(100*a+10*b+c==pow(a,3)+pow(b,3)+pow(c,3)) {
					cout<<"a="<<a<<" "<<"b="<<b<<" "<<"c="<<c<<" "<<endl;
				}
return 0;		
}
```
17. Calculate sum of even numbers and average of odd numbers
(Input prime number(encounter) to stop input)
```
#include <iostream>
using namespace std;

int main() {
    int num,e_sum=0,o_sum=0,i,t=0,j=1;
    double avg=0;

    do {
        cin>>num;

        for(i=2; i<=num; i++) {
            if(num%i==0) {
                t++;
                break;
            }
            if(num/i==1) {
                t=0;
                break;
            }
        }

        if(num%2==0) {
            e_sum=e_sum+num;
        }
        else if(num%2!=0&&t!=0){
            o_sum=o_sum+num;
            avg=o_sum/j;
            j++;
        }

    } while(t!=0);

    cout<<"sum of even numbers: "<<e_sum<<endl;
    cout<<"average of odd numbers: "<<avg<<endl;
    return 0;
}
```
18. How many digits in the number and sum the digits
```
#include <iostream>
#include <string>
using namespace std;

int main() {
    string num;
    int sum=0,i;
    cin>>num;
    for(i=0;i<num.length();++i) {
        sum+=num[i]-'0';
    }
    cout<<"Number of digits: "<<i<<endl;
    cout<<"Sum of digits: "<<sum;
return 0;
}
```
19. Print all prime numbers from 2 to N
```
#include<iostream>
using namespace std;
int main() {
    int i,t=0,j,num;
    cin>>num;

    for(i=1; i<=num; i++) {
        for(j=2; j<i; j++) {
            if(i%j==0) {
                t++;
                break;
            }
        }
        if(t==0 && i!=1) {
            cout<<i<<" ";
        }
        t = 0;
    }
return 0;
}
```
20. Factor a number into prime factors
```
#include <iostream>
using namespace std;

int main() {
    int num,i=2;
    cin>>num;
    cout<<num<<" = ";
    while(num!=1) {
        if(num==i) {
            num=num/i;
			cout<<i;
		}	
		if(num%i==0) {
            num=num/i;
            cout<<i<<" x ";
        } else {
            i++;
        }
    }
return 0;
}
```