Solved Problems

Since developing a love for programming, I have always tried to solve any problem on my own. In this pursuit, I have solved several problems, including those from Toph, Project Euler, Dimik OJ, some books and from the preparation stages for the Informatics Olympiad. I have tried to include all the problems that I have solved on my own approach up to this day.


Problem 1-



Problem Statement

Solve-


#include <stdio.h>

int main(void){

    int a;
    
    scanf("%d", &a);
    printf("%d", a);
    
    return 0;
}
        




Problem 2-



Problem Statement

Solve-


#include <stdio.h>

int main(void){

    int a, b;
    
    scanf("%d", &a);
    
    int sum = a + b;
    
    printf("%d", sum);
    
    return 0;
}
        




Problem 3-



Problem Statement

Solve-


#include <stdio.h>

int main(void){

    int a;
    
    scanf("%d", &a);
    
    for(int i = 1; i <= a; i++){
      if(a % i == 0){
        printf("%d\n", i);
      }
    }
    
    return 0;
}
        




Problem 4-



Problem Statement

Solve-


#include <stdio.h>

int main(void){

    int a, b;
    
    scanf("%d %d", &a, &b);
    
    int remainder = b % a;
    int c = a - remainder;
    
    printf("%d", c);
    
    return 0;
}
        




Problem 5-



Problem Statement

Solve-


#include <stdio.h>
#include <math.h>

int main(void){

    int r;
    
    scanf("%d", &r);
    
    double area = acos(-1) * pow(r, 2);
    
    printf("%0.10lf", area);
    
    return 0;
}
        




Problem 6-



Problem Statement

Solve-


#include <stdio.h>

int main(void){

    int sum, a, b, c, d;
    
    scanf("%d %d %d %d", &sum, &a, &b, &c);
    
    d = sum - (a + b + c);
    
    printf("%d", d);
    
    return 0;
}
        




Problem 7-



Problem Statement

Solve-


#include <stdio.h>

int main(void){

    int num1, num2;
    int a, b, c;
    
    scanf("%d %d", &num1, &num2);
    
    a = num1 / num2;
    b = num1 % num2;
    
    printf("%d %d %d", a, b, num2);
    
    return 0;
}
        




Problem 8-



Problem Statement

Solve-


#include <stdio.h>

int main(void){

    int year;
    
    scanf("%d", &year);
    
    if(year % 4 == 0 && year % 400 != 0){
      printf("Yes");
    }
    else{
      printf("No");
    }
    
    return 0;
}
        




Problem 9-



Problem Statement

Solve-


#include <stdio.h>

int main(void){

    int a;
    
    scanf("%d", &a);
    
    int area = a * a;
    
    printf("%d", area);
    
    return 0;
}
        




Problem 10-



Problem Statement

Solve-


#include <stdio.h>

int main(void){

    int m, n;
    scanf("%d %d", &m, &n);
    if(1 <= n && m <= 11 && m != n && m > n){
      printf("Champion\nRunners Up");
    }
    else{
      printf("Runners Up\nChampion");
    }
    return 0;
}
        




Problem 11-



Problem Statement

Solve-


#include <stdio.h>

int main(void){
  int m;
  scanf("%d", &m);
  int array[m];
  for(int i = 0; i < m; i++){
    scanf("%d", &array[i]);
  }
  int max = array[0];
  int size = sizeof(array) / sizeof(array[0]);
  for(int i = 1; i < size; i++){
    if(array[i] > max){
        max = array[i];
    }
    else{
      max = max;
    }
}
  printf("%d", max);
  return 0;
}
        




Problem 12-



Problem Statement

Solve-

Here is the solution


#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int x[n], y[n], z[n];

    for (int i = 0; i < n; i++) {
        cin >> x[i] >> y[i] >> z[i];
    }

    for (int i = 0; i < n; i++) {
        cout << "Case " << i + 1 << ": ";
        
        if (x[i] < y[i] && y[i] > z[i] && x[i] > z[i]) {  
            cout << z[i] << " " << x[i] << " " << y[i] << endl;  
        }  
        else if (x[i] > y[i] && y[i] < z[i] && x[i] < z[i]) {  
            cout << y[i] << " " << x[i] << " " << z[i] << endl;  
        }  
        else if (x[i] < y[i] && y[i] > z[i] && x[i] < z[i]) {  
            cout << x[i] << " " << z[i] << " " << y[i] << endl;  
        }  
        else if (x[i] < z[i] && x[i] < y[i] && y[i] < z[i]) {  
            cout << x[i] << " " << y[i] << " " << z[i] << endl;  
        }  
        else if (x[i] > y[i] && y[i] > z[i] && x[i] > z[i]) {  
            cout << z[i] << " " << y[i] << " " << x[i] << endl;  
        }  
        else if (x[i] > z[i] && x[i] > y[i] && y[i] < z[i]) {  
            cout << y[i] << " " << z[i] << " " << x[i] << endl;  
        }
    }
    
    return 0;
}
        




Problem 13-



Problem Statement

Solve-


#include <stdio.h>

int main() {
    int m, n;
    scanf("%d", &m);
    for(int i = 1; i <= m; i++){
      scanf("%d", &n);
      if(n % 2 == 0){
        printf("Bangla\n");
      }
      else if(n == 1971){
        printf("Joy Bangla\n");
      }
      else{
        printf("Joy\n");
      }
    }
    return 0;
}
        




Problem 14-



Problem Statement

Solve-


#include <stdio.h>
#include <string.h>

int main() {
    char str[101];
    scanf("%s", str);
    int count = strlen(str);
    printf("%d\n", count);
    return 0;
}
        




Problem 15-



Problem Statement

Solve-

Here is the solution


#include <iostream>

using namespace std;

int main(void){
  int n;
  cin >> n;
  int array[n];
  for(int i = 1; i <= n; i++){
    cin >> array[i];
  }
  for(int i = 1; i <= n; i++){
    if(array[i] % 19 == 0){
      cout << "Lucky" << "\n";
    }
    else{
      cout << "Unlucky" << "\n";
    }
  }
  return 0;
}
        




Problem 16-



Problem Statement

Solve-


#include <stdio.h>

int main(){
  int n;
  scanf("%d", &n);
  int array[n];
  for(int i = 1; i <= n; i++){
    scanf("%d", &array[i]);
  }
  for(int i = 1; i <= n; i++){
    if(array[i] % 2 == 0){
      printf("even\n");
    }
    else{
      printf("odd\n");
    }
  }
  return 0;
}
        




Problem 17-



Problem Statement

Solve-

Here is the solution


#include <stdio.h>

int main(void) {
    int n, count = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        if (n % i == 0) {
            count++;
        }
    }
    printf("%d", count - 1);
    return 0;
}
        




Problem 18-



Problem Statement

Solve-


#include <stdio.h>

int main(void) {
    int n;
    int x, y;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
      scanf("%d %d", &x, &y);
      int avg = (x + y) / 2;
    if(avg % 2 == 0){
      printf("Sadia will be happy.\n");
    }
    else{
      printf("Oops!\n");
    }
    }
    
    return 0;
}
        




Problem 19-



Problem Statement

Solve-

Here is the solution


#include <stdio.h>

int main(void) {
    int n, sum = 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
      sum += i;
    }
    printf("%d", sum);
    return 0;
}
        




Problem 20-



Problem Statement

Solve-

Here is the solution


#include <stdio.h>
#include <math.h>

int main(void) {
    int n, sum = 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
      int square = pow(i, 2);
      sum += square;
    }            
    printf("%d", sum);
    return 0;
}
        




Problem 21-



Problem Statement

Solve-

Here is the solution


#include <stdio.h>
#include <stdlib.h>

int main() {
  int n, num;
  scanf("%d", &n);
  for(int i = 1; i <= n; i++){
    scanf("%d", &num);
    printf("%d\n", abs(num));
  }
  
  return 0;
}
        




Problem 22-



Problem Statement

Solve-

Here is the solution


#include <stdio.h>

int F(int n) {
    if (n <= 1) {
        return n;
    } else {
        return F(n - 1) + F(n - 2);
    }
}

int main() {
    int m;
    scanf("%d", &m);
    printf("%d\n", F(m));
    return 0;
}
        




Problem 23-



Problem Statement

Solve-

Here is the solution


#include <stdio.h>

int main(void){
  int n = 1000;
  int sum = 0;
  for(int i = 1; i < n; i++){
    if(i % 3 == 0 || i % 5 == 0){
      sum += i;
    }
  }
  printf("%d", sum);
  return 0;
}
        




Problem 24-



Problem Statement

Solve-

Here is the solution


#include <iostream>

using namespace std;

int main(void){
	int m, n, count = 0;
	cin >> m >> n;
	for(int i = m; i <= n; i++){
		if(i % 2 == 1){
			count++;
		}
	}
	cout << count;
	return 0;
}
        




Problem 25-



Problem Statement

Solve-

Here is the solution


#include <iostream>

using namespace std;

int main(void){
  int m, n;
  cin >> m >> n;
  if(1 <= n && m <= 11 && m != n && m > n){
      cout << "Champion\n" << "Runner up" << endl;
    }
    else{
      cout << "Runner up\n" << "Champion" << endl;
      }
  return 0;
}
        




Problem 26-



Problem Statement

Solve-

Here is the solution


#include <iostream>

using namespace std;

int main() {
	int m, n;
	cin >> m >> n;
	if(m > n){
		cout << "Alice";
	}
	else{
		cout << "Bob";
	}
	return 0;
}
        




Problem 27-



Problem Statement

Solve-

Here is the solution


#include <iostream>
#include <iomanip>

using namespace std;

int main(void){
  int n;
  cin >> n;
  double array[n];
  for(int i = 1; i <= n; i++){
    cin >> array[i];
  }
  for(int i = 1; i <= n; i++){
    double pi = 3.1416;
    double width, height, area_of_circle, area_of_rectangle;
    width = array[i] * 4;
    height = array[i] * 2;
    area_of_rectangle = width * height;
    area_of_circle = 2 * array[i] * array[i] * pi;
    double green_region = area_of_rectangle - area_of_circle;
    cout << "Case " << i << ": " << fixed << setprecision(2) << green_region << "\n";
  }
  return 0;
}
        




Problem 28-



Problem Statement

Solve-

Here is the solution


#include <iostream>
#include <cmath>

using namespace std;

int main() {
	int n;
	cin >> n;
	int a[n], b[n];
	for(int i = 0; i < n; i++){
		cin >> a[i] >> b[i];
	}
	for(int i = 0; i < n; i++){
		int sum = pow(a[i], 2) + pow(b[i], 2);
		cout << "Case " << i + 1 << ": " << sum << endl;
	}
	return 0;
}
        




Problem 29-



Problem Statement

Solve-

Here is the solution


#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
int main(){
    int n;
    cin >> n;
    double array_a[n], array_b[n], array_c[n];
    for(int i = 0; i < n; i++){
        cin >> array_a[i] >> array_b[i] >> array_c[i];
    }
    for(int i = 0; i < n; i++){
        double s = (array_a[i] + array_b[i] + array_c[i]) / 2;
        double m = (s - array_a[i]) * (s - array_b[i]) * (s - array_c[i]);
        double n = s * m;
        double area = sqrt(n);
        cout << "Area = " << fixed <<  setprecision(3) << area << endl;
    }
    return 0;
}




Problem 30-



Problem Statement

Solve-

Here is the solution


#include <iostream>

using namespace std;

int main(void){
    int n;
    cin >> n;
    int array[n];
    for(int i = 0; i < n; i++){
        cin >> array[i];
    }
    for(int i = 0; i < n; i++){
        long long int factorial = 1; 
        for(int j = 1; j <= array[i]; j++){
            factorial *= j;
        }
        cout << factorial << "\n";
    }
    return 0;
}
 




Problem 31-



Problem Statement

Solve-

Here is the solution


#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);    
    int array[n];
    for (int i = 0; i < n; i++) {
        scanf("%d", &array[i]);
    }
    for (int i = 0; i < n; i++) {
        printf("Case %d:", i + 1);
        printf(" 1");
        for (int j = 2; j <= array[i]; j++) { 
            if (array[i] % j == 0) {
                printf(" %d", j);
            }
        }
        printf("\n");
    }    
    return 0;
}

Different approach through C++-


#include <iostream>
#include <vector>

using namespace std;

int main(void){
    int n;
    cin >> n;
    vector<int> array(n);
    for(int i = 0; i < n; i++){
        cin >> array[i];
    }
    for(int i = 0; i < n; i++){
        cout << "Case " << i + 1 << ": ";
        for(int j = 1; j <= array[i]; j++){
            if(array[i] % j == 0){
                cout << j << " ";
            }
        }
        cout << endl;
    }
    return 0;
}




Problem 32-



Problem Statement

Solve-

Here is the solution


#include <iostream>

using namespace std;

int main(void) {
    int n;
    cin >> n;
    int array[n];
    for(int i = 0; i < n; i++){
      cin >> array[i];
    }
    for(int i = 0; i < n; i++){
      if(array[i] % 2 == 0){
        cout << "even\n";
      }
      else{
        cout << "odd\n";
      }
    }
    return 0;
}




Problem 33-



Problem Statement

Solve-

Here is the solution


#include <iostream>

using namespace std;

int main(void){
    int n;
    cin >> n;
    long long array[n];
    for(int i = 0; i < n; i++){
        cin >> array[i];
    }
    for(int i = 0; i < n; i++){
        long long int m = 0;
        for(long long j = 1; j < array[i]; j++){
            if(array[i] % j == 0){
                m += j;
            }
        }
        if(m == array[i]){
            cout << "YES, " << array[i] << " is a perfect number!" << endl;
        }
        else{
            cout << "NO, " << array[i] << " is not a perfect number!" << endl;
        }
    }
}
return 0;
}




Problem 34-



Problem Statement

Solve-

Here is the solution


#include <iostream>
#include <iomanip>

using namespace std;

int main(void){
    int n;
    cin >> n;
    double x[n], y[n], z[n];
    for(int i = 0; i < n; i++){
        cin >> x[i] >> y[i] >> z[i];
    }
    for(int i = 0; i < n; i++){
        double remained_over = (z[i] / 6);
        double left_over = 50 - remained_over;
        double run_rate = y[i] / left_over;
        int target_run = x[i] + 1;
        int remained_run = target_run - y[i];
        double desired_run = remained_run / remained_over;
        cout << fixed << setprecision(2) << run_rate << " " << fixed << setprecision(2) << desired_run << endl;
    }
    return 0;
}




Problem 35-



Problem Statement

Solve-

Here is the solution


#include <iostream>
#include <cmath>

using namespace std;

int main(void){
    long long int whole_square, square, subtraction, n = 100, sum = 0, addition = 0;
    for(int i = 1; i <= n; i++){
      square = pow(i, 2);
      sum += square;
      addition += i;
      whole_square = pow(addition, 2);
      subtraction = whole_square - sum;
    }
    cout << subtraction << endl;
    return 0;
}




Problem 36-



Problem Statement

Solve-

In order to solve this problem, we will go through three approaches. First of all, we will find out all the divisors of \(600851475143\) by using the simple program below. Then we will check their divisors. If it shows void after each case, then it would be a prime factor. If not, then it will show divisors which proves that those numbers are Compound Number. Where the void test case will end, we have to assume the test case as our desired prime factor.


#include <iostream>

using namespace std;

int main(void){
    long long int n = 600851475143;
    for(int i = 2; i <= n / 2; i++){
    	if(n % i == 0){
    		cout << i << endl;
    	}
    }
    return 0;
    }

Now, our task is copying all the divisors in order to check if those divisors have any other divisors, if so, then it won't be a prime factor. We can check by using the given program below although the program is not efficient to process large computational task. However, it is absolutely my learning limitation.


#include <iostream>

using namespace std;

int main(void){
    long long int n;
    cin >> n;
    int array[n];
    for(int i = 0; i < n; i++){
      cin >> array[i];
    }
    for(long long int i = 0; i < n; i++){
      cout << "Case " << i + 1 << ": ";
      for(long long int j = 2; j <= array[i] / 2; j++){
        if(array[i] % j == 0){
          cout << j << " ";
        }
      }
      cout << endl;
    }
    return 0;
    }

Again, we need to check if \(8462696833\) is a prime number or not since the program was unable to provide Case \(14\), we can check it by running this program-


#include <iostream>

using namespace std;
int prime(long long int n){
	if(n < 2){
		return 0;
	}
	if(n == 2){
		return 1;
	}
	if(n % 2 == 0){
		return 0;
	}
	for(long long int i = 3; i <= n / 2; i += 2){
		if(n % i == 0){
			return 0;
		}
	}
	return 1;
}
int main(void){
    long long int n;
    while(1){
    	cin >> n;
    	if(n == 0){
    		break;
    	}
    	if(1 == prime(n)){
    		cout << "Prime Number";
    	}
    	else{
    		cout << "Compound Number";
    	}
    }
    return 0;
    }

Our program shows us that \(8462696833\) is a compound number. So the largest prime factor of \(600851475143\) is \(6857\) which was previously in Case \(4\).





Problem 37-



Problem Statement

Solve-

Here is the solution


#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int x[n], y[n];

    for (int i = 0; i < n; i++) {
        cin >> x[i] >> y[i];
    }

    for (int i = 0; i < n; i++) {
        if (x[i] < y[i]) {
            for (int j = x[i]; j <= y[i]; j += x[i]) {
                cout << j << endl;
            }
        } else {
            cout << "Invalid!" << endl;
        }
        cout << endl;
    }

    return 0;
}




Problem 38-



Problem Statement

Solve-

Here is the solution


#include <iostream>
#include <cmath>

using namespace std;

int main() {
    int n;
    cin >> n;
    int x[n], y[n];

    for(int i = 0; i < n; i++){
        cin >> x[i] >> y[i];
    }

    for(int i = 0; i < n; i++){
        long long int sum = 0;
        for(int j = y[i]; j >= 0; j--){
        	long long int expansion = pow(x[i], j);
        	sum += expansion;
        }
        cout << "Result = " << sum << endl;
    }
    return 0;
}




Problem 39-



Problem Statement

Solve-

Here is the solution


#include <iostream>
	    
using namespace std;

int main(){
	int n;
	cin >> n;
	int x[n], y[n], z[n];
	for(int i = 0; i < n; i++){
		cin >> x[i] >> y[i] >> z[i];
	}
	for(int i = 0; i < n; i++){
		for(int j = 1; j <= z[i]; j++){
			if(j % x[i] == 0 && j % y[i] == 0){
				cout << j << endl;
			}
		}
		cout << endl;
	}
	return 0;
}




Problem 40-



Problem Statement

Solve-

Here is the solution


#include <iostream>

using namespace std;

int main(void){
    int n;
    cin >> n;
    int x[n];
    for(int i = 0; i < n; i++){
        cin >> x[i];
    }
    for(int i = 0; i < n; i++){
        for(int j = 1; j <= x[i]; j++){
            for(int k = 1; k <= x[i]; k++){
                cout << "*";
            }
            cout << endl;
        }
        cout << endl;
    }
    return 0;
}




Problem 41-



Problem Statement

Solve-

Here is the solution


#include <iostream>
using namespace std;

int main() {
    int n, temp;
    cin >> n;

    int x[n];
    for (int i = 0; i < n; i++) {
        cin >> x[i];
    }
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - 1; j++) {
            if (x[j] > x[j + 1]) {
                temp = x[j];
                x[j] = x[j + 1];
                x[j + 1] = temp;
            }
        }
    }
    cout << x[1] - x[0];
    return 0;
}





Problem 41-



Problem Statement

Solve-

Here is the solution


#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int x[n], y[n], z[n];
    for(int i = 0; i < n; i++){
        cin >> x[i] >> y[i] >> z[i];
    }
    for(int i = 0; i < n; i++){
        if((x[i] < y[i] && y[i] < z[i] && x[i] < z[i]) || (x[i] > y[i] && y[i] > z[i] && x[i] > z[i])){
            cout << y[i] << endl;
        }
        else if((x[i] > y[i] && z[i] > y[i] && z[i] > x[i]) || (x[i] < y[i] && y[i] > z[i] && x[i] > z[i])){
            cout << x[i] << endl;
        }
        else{
            cout << z[i] << endl;
        }
    }
    return 0;
}




Problem 42-



Problem Statement

Solve-

Here is the solution


#include <iostream>
using namespace std;

int function(int n){
        if(n % 2 == 0 && n % 7 == 0){
	    cout << "Alice" << endl;
	}
        else if(n % 2 == 1 && n % 9 == 0){                         
		cout << "Bob" << endl;
        }
        else{
                cout << "Charlie" << endl;
        }
        return n;
}
int main(){
        int m; cin >> m;
        int array[m];
        for(int i = 0; i < m; i++){
                cin >> array[i];
        }
        for(int i = 0; i < m; i++){
                function(array[i]);
        }
        return 0;
}
	    




Problem 43-



Problem Statement

Solve-

Here is the solution


#include <iostream>
using namespace std;
void madeMoney(int x, int y){
    if(x >= y){
        int money = y;
        cout << money << endl;
    }
    else{
        int extra = y - x;
        int extraMoney = extra * 2 + x;
        cout << extraMoney << endl;
    }
}
int main(){
    int n; cin >> n;
    while(n--){
        int a, b; cin >> a >> b;
        madeMoney(a, b);
    }
    return 0;
}
	    




Problem 44-



Problem Statement

Solve-

Here is the solution


#include <iostream>
#include 
using namespace std;
void scalTri(int x, int y, int z){
    if(x == y || y == x || y == z || z == y || x == z || z == x || x == y == z){
        cout << "NO" << endl;
    }
    else{
        cout << "YES" << endl;
    }
}
int main() {
    int t;
    cin>>t;
    while(t--){
        int a,b,c;
        cin>>a>>b>>c;
        scalTri(a, b, c);
        
    }
	return 0;

}