C practice course will push you to solve problems. Here I will give you the idea about some most relevant problem solving.

I will help you to solve some problems which gives you the encourage to solve problems at any problem solving platforms.

The most common problem among programmers who want to become a competitive programmer is that how to solve real life problems.

Here, I will discuss about some problems and gives you the source code by which you can understand the basics of real life problem solving.

Competitive programming is not a static field. Everyday programmers have to face new problem here.

I will just give you some idea about how to solve problems in best way.

Then you have to solve many problems as much as you can in any problem solving platforms.

Table of Contents

## Finding square roots problem and solution in C

Read the description of finding square roots problem

### Problem description

This finding square roots problem is taken from CodeChef for practice and learning purpose only.

Now, we will discuss how to solve this problem using C language. Obviously you can use other programming language but the logic can be same.

You can also implement your own logic. Let’s see the solution of this problem bellow.

### Solution of finding square roots problem

Here at the solution code we have taken an integer variable T for storing the number of test case from user.

Then each line will take another integer as input and determine its square root by **sqrt()** function which is defined under math.h header file.

Then the **floor()** function which is also defined inside **math.h** header file will make it the largest integer which is less then or equal to then number inside it.

Let’s consider,

**floor(sqrt(2))**;

=Â floor(1.41421356237);

=Â 1

```
// solution of finding square roots problem
#include <stdio.h>
#include <math.h>
int main(){
int T, c;
scanf("%d", &T);
for(c = 1; c <= T; c++){
double num, sqr;
scanf("%lf", &num);
sqr = floor(sqrt(num));
printf("%.lf\n", sqr);
}
return 0;
}
```

Now, compile and run this code then check the output for given input set. You will see the output like this.

### Output of finding square roots problem

```
3
10
3
5
2
10000
100
```

Here, first line contains 3 which is the total test case. Then we want to determine the square root of 10 which is in the second line.

After pressing enter we will get the output 3 which is in the fourth line. Then another two input and their output is shown above.

This is not the only solution to this problem. You can use any other loop although we have used for loop here.

So, try this using other loop which will increase your problem solving power.

## Sum or difference problem and solution using C

Read the description of sum or difference program

### Problem description

Here in this sum or difference program we have to take two integers as input from user.

The we have to check if the first number is greater than we have to print their difference. Otherwise we have to print their sum.

One thing should take into account that if the two number is equal then it automatically print the sum as the first number is not greater than the second one.

We have taken this problem from CodeChef for practice and learning purpose only of them who want to become a problem solver in future.

Here we will discuss the solution this problem using C language. You can use other language too to write the solution to this problem.

You should try to invent other ways to make the solution of this problem. So, let’s see the solution first.

### Solution of sum or difference problem

At first we will take two integer from user and store them in two variable a and b.

Then we will use if else condition to check if the first number is greater than second number or not.

If the first number is greater than second, then we will subtract second number from first number and print the result.

Otherwise we will print the sum of the two numbers.

**If a user give two number**,

aÂ =Â 10;

bÂ =Â 8;

**result**Â =Â aÂ –Â bÂ =Â (10Â –Â 8)Â =Â 2;

Because **a > b** here.

**Again if he gives two number**,

aÂ =Â 20;

bÂ =Â 25;

**then result**Â =Â aÂ +Â bÂ =Â (20Â +Â 25)Â =Â 45;

Because **a <= b** here.

Now, let’s go to the code bellow to implement this situation.

```
// solution of sum or difference problem using C
#include <stdio.h>
int main(){
int a, b;
scanf("%d", &a);
scanf("%d", &b);
if(a > b){ // check if a > b
printf("%d\n", a - b);
}else{
printf("%d\n", a + b);
}
return 0;
}
```

### Output of sum or difference program

You will see the following output when you compile and run the above program.

```
82
28
54
```

Here, the user gives 82 as the first number and 28 as the second number. As **82 > 28**, so the program will print their difference which is in third line 54.

But run this program and give input 28 as first number and 82 as second number. The output will be as like as bellow.

```
28
82
110
```

However if you give two same number as input, then the program also add them and print the sum of them.

```
70
70
140
```

## Chef and operators problem solving in C

Read the problem description of chef and operators problem here;

### Problem description

This chef and operators program will take an integer which will be the testcase and then we have to give two integer for each test case.

This program will check the given two integer that which is the biggest among them. Then we will print the output according to that.

We have taken it from CodeChef for practice and learning purpose only.

We will discuss the problem and see a solution of this problem which will help the beginner programmer to go ahead in their journey.

Don’t just depend upon our solution, rather you should try to solve it in your own. So, try this.

Don’t worry, if you are confusing about the problem then we have given a simple solution to chef and operator problem which is given bellow.

### Solution of chef and operators problem

At first we have to take the testcase from the user. The we will use a for loop to take the input for each test case. You can use any loop to do this.

For each test case we will take two integers from user and check the condition using if else statement.

If first number is greater then second, we will print ‘**>**‘ sign. If the second number is greater than first number, we will print ‘**<**‘ sign. Otherwise we will print ‘**=**‘ sign.

Consider a user give two integer **15 and 20**.

Then we check that **15Â <Â 20** and print ‘**<**‘.

Again if he give two number **25 and 15**.

Then we know that **25Â >Â 15** and print ‘**>**‘.

If the given two number is same, then we will print ‘**=**‘.

Now, let’s go to the C solution of chef and operators problem.

```
// solution of chef and operators problem
#include <stdio.h>
int main(){
int T, c;
scanf("%d", &T);
for(c = 1; c <= T; c++){
int x, y;
scanf("%d %d", &x, &y);
if(x > y){ // check if x > y
printf(">\n");
}else if(x < y){ // check if x < y
printf("<\n");
}else{ // default which is x = y
printf("=\n");
}
}
return 0;
}
```

### Output of chef and operator problem

After compiling and running the above program you will see the following output.

```
3
10 20
<
20 10
>
10 10
=
```

First line contains 3 which it the number of test case for this problem. Then the user gives two integer 10 and 20 where **10 < 20** and for this we have printed ‘**<**‘ here.

Second input is 20 and 10 where the program have printed ‘**>**‘ sign on the screen. And it has printed ‘**=**‘ sign for the third test case.

Now, you should try this problem using different loop like while loop and do while loop. If you use other programming language then the logic will be same.

## Solving reverse the number problem in C

Read the problem description of reverse the number problem here;

### Problem description

The main program is to reverse the number. If a user gives the input 12345 then we will make it reverse like 54321 and print it.

We have to avoid zero at the beginning of our output.

We have to take the testcase as input from user and for each test case the program will take an input integer and print the reverse of that number.

This problem is taken from CodeChef for practice and learning purpose only.

We will try to describe the solution and the cause behind it when we solve this problem in this guide.

You can solve this problem in many way using different logic. You should think about your logic.

We will give a simple solution to this problem bellow. Let’s try to solve reverse the number problem by C.

### Solution of reverse the number problem

We have taken an integer **Test** for storing the given number of test case for this reverse the number program.

For each test case we will take the number which we have to reverse. The we will use a loop until the number is equal to zero.

We have used while loop here. Inside the while loop we will determine reminder by dividing the given number by 10.

This reminder will be the last digit of the number as like as bellow.

When we divide **25 by 10**, the reminder will be **5** which is the last digit of this number 25. That means we have got the first digit of our output.

As we have take the variable **revNum** = 0.

So, (revNum * 10) + rem = 5

The we will update the number by divide it with 10 which will always give an integer.

After completing the function of while loop we will get the reverse number. Then we will print that number. Now, let’s see the code.

```
// solution of reverse the number problem by C
#include <stdio.h>
int main(){
int Test, i;
scanf("%d", &Test);
for(i = 1; i <= Test; i++){
int n, rem, revNum = 0;
scanf("%d", &n);
while(n != 0){
rem = n % 10;
revNum = (revNum * 10) + rem;
n = n / 10;
}
printf("%d\n", revNum);
}
return 0;
}
```

### Output of reverse the number problem

After compiling and running this reverse the number program you will see the output like this.

```
4
12345
54321
31203
30213
2123
3212
2300
32
```

Here, the first line is the test case given by user. Then the second line is the first input which we have to reverse using out program.

The third line is the output for the first input.

Then the fifth line is the output for the input of fourth line and so on.

You many have noticed that for the last test case we have printed 32 instead of 0032. You will get error if this solution prints 0032.

## Second largest number problem and solution by C

Read the description of second largest number problem carefully.

### Problem description

In this above problem we have to determine the second largest number inside the given three numbers of user.

We will take three integers as input and check by our program that which number is the second largest among them. Then we will print that number.

We have taken this problem from CodeChef for practice and learning purpose only.

You should bear in mind that the solution we have given here is not the only solution to this problem. Rather it will be better if you find the solution by your own.

We have to compare each number with rest two and then we will take the decision that which number is the second largest. Let’s see a solution of this problem.

### Solution of second largest number problem

Here in the given solution we have taken a variable T to store the test case and then we have used a for loop for rotating the program T times.

For each test case we will take three number and then compare them.

If the first number is greater than second and less than third or greater than third and less than first number, then we will print that the first number is the second largest.

We will check this for second number also. If first and second number is not the second largest then the third number will automatically be the second largest number.

Assume a user gives three number 5, 10 and 15. Now our program will check the second number first.

Is second number is less than first number and greater than third number?

The answer is ‘Yes’. And then the program will print the second number as the second largest number.

```
// solution of second largest number in C
#include <stdio.h>
int main(){
int T, i;
scanf("%d", &T);
for(i = 1; i <= T; i++){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if((a >= b && b >= c) || (c >= b && b >= a)){
printf("%d\n", b); // b is the second largest
}else if((b >= a && a >= c) || (c >= a && a >= b)){
printf("%d\n", a); // a is the second largest
}else{
printf("%d\n", c); // c is the second largest
}
}
return 0;
}
```

### Output of second largest number problem

When you compile and run the above program you will see the following output. You can also check the program for different input.

```
3
120 11 400
120
10213 312 10
312
10 3 450
10
```

First line contains 3 which is the number of test case. Then the given three number is 120, 11 and 400.

As 120 is the second largest among them, the program prints 120. Similarly, the program have given the other output also.

However, if you have any other logic to solve this problem then you should try to implement that. This is not the only solution of this second largest number program.

## Valid triangles program with solution in C

Read the description of valid triangle problem before solving it.

### Problem description

We know that the sum of three angle of any triangle is equal to 180. Here, we have to implement this logic to write the solution of valid triangles program.

The program will take three angles from the user and check if the sum of all angles is 180 or not. If it is 180 then we have to print ‘YES’ otherwise ‘NO’.

This valid triangle program is not a hard problem at all. This problem is taken from CodeChef for practice and learning purpose only.

Here in this C practice guide we will discuss the solution of this problem as well as the cause behind it.

Now, let’s go to the solution of this valid triangle program. We have used C language here although you can use any other language too.

The logic may be same or different for you.

### Solution of valid triangle problem

For solving this problem, at first we have to take an input from user which will be the test case for this problem. The each test case will take three integers.

After taking these input we will add the three angles and check if the sum is equal to 180 or not. When the sum is 180 then the triangle is possible otherwise not.

Suppose, a user gives three integers which is **60, 80 and 40**.

Then the sum of these angle will be (60 + 80 + 40) = 180

So, this triangle is possible and it is a **valid triangle**.

But when a user gives three angles like **50, 30 and 80**

Then the sum is = (50 + 30 + 80) = 160

As 160 is not equal to 180 then this triangle is not possible and is **not a valid triangle**.

```
// solution of valid triangle using C
#include <stdio.h>
int main(){
int T, i;
scanf("%d", &T);
for(i = 1; i <= T; i++){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int sum = a + b + c;
if(sum == 180){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}
```

### Output of valid triangle problem

You will see the output like following when you compile and runÂ the above program.

```
3
40 40 100
YES
45 45 90
YES
180 1 1
NO
```

In the above output, first line which is 3, is the number of test case for this program.

Then second line is the first input set for the program. Here the sum is equal to 180 and for this the program have printed ‘YES’.

The other test case is also have worked like this.

However, you can implement other logic to solve this valid triangle program if you have any idea about solving this problem.

The solution we have given here is not the only solution of this problem. Every problem have several solution and you have to discover your own.

## Lucky four problem solving with C

Read the description of lucky four problem before solving it.

### Problem description

This lucky four problem is all about finding the digit 4 in any integers. We have to print the occurrence of the digit four in any integer.

We will take the integer from the user and find the number of digit 4 inside that integer.

After that we will print the number of 4 inside that integers. We also have to print zero if the digit 4 is not present in that integer.

We have taken this problem from CodeChef to discuss about the solution of this problem for learning purpose only.

Obviously you can solve this lucky four problem using different method as well as different programming languages.

We will use C programming language to solve this problem. Now, let’s see the solution to this lucky four problem.

### Solution of lucky four problem

We will take an integer at the beginning of this problem which will be the number of test case.

The the user will give an integer for each test case. What we have to do is, find the number of 4 inside that integers.

Here, in this bellow program we have stored the given integer in the variable a. After that we have used a while loop till a > 0.

Inside the while loop we have divided the integer with 10 which will give the reminder of its last digit.

Then check that digit that is it 4 or not. We will update the value of number by dividing it with 10.

Consider, a user have given **244** to check the occurrence of digits lucky four. Then this program will divide it with 10 and store the reminder in rem variable.

**rem = 244 % 10**

**= 4**

As the reminder is 4 then the value of **count** variable will be 1.

The the new value of integer will be **244 / 10**

= 24 (Integer part only).

Then the new integer will also be divided by 10 again which will give the reminder 4 again.

The value of **count** will be 2 and value of new integer will be **24 / 10**

= 2

Then again 2 will be divided by 10 which will give the reminder 2.

As 2 is not lucky four, so the value of counter will not be increased here.

Then after checking all the digits of the integer we will print the value of count which will be 2 here.

```
// solution of lucky four problem by C
#include <stdio.h>
int main(){
int Test, i;
scanf("%d", &Test);
for(i = 1; i <= Test; i++){
int a, rem, count = 0;
scanf("%d", &a);
while(a > 0){
rem = a % 10;
if(rem == 4){
count++;
}
a = a / 10;
}
printf("%d\n", count);
count = 0;
}
return 0;
}
```

### Output of lucky four problem

Now, compile and run the above **lucky four** program to get the output like we have given bellow.

```
5
447474
4
228
0
6664
1
40
1
81
0
```

In this above output of lucky four program, the first line contains integer 5 which is the testcase for this program.

Then the second line is the first integer which we have to check for lucky four. As here the digit 4 remain 4 times then the program have printed 4.

Similarly the program have checked all the other input and printed the occurrence of 4 in the output.

In this above program we have used for loop for taking the value of all the integers.

We have used while loop also to check the digits and update the number.

However you can use any other loop for writing this program. You should also try using different logic to solve this problem.