# Some Random Example Using C Program

In this C programming tutorial for beginners tutorial, we will learn some basic things and will make some easy programs for normal calculations. If you haven’t read my previous articles or don’t have any idea about c programming just read them. After this tutorial, you will learn the following terms.

• What Is The Difference Between Integer And Float Data?
• How To Find The Square Root Of Any Number In C.
• How To Use Different Arithmetic Operators In C.

Look at the 1st program for today:

#include <stdio.h>
int main ()
{
int a,b,div;
float d;
scanf(“%d %d”,&a,&b);
div=a/b;
d=a/b;
printf(“Division using integer data type div=%dn”,div);
printf(“Division using floating data type d=%fn”,d);

return 0;
}

In this program, we are taking two integer types of data in a and b as input. And shows two outputs. One is integer div and the other is floating d. Suppose, we are taking inputs 3 and 2 then the output will be like this:

Division using integer data type div=1
Division using floating data type d=1.000000

Both div and d prints 1. But we know the actual value is 1.5. That is because 1st of all integer types of data can’t contain a fractional part. So, it will evaluate only the integer type and reject the fractional part. That’s why div=1. In the case of d. d is a floating type of data so it can contain fractional numbers. But the problem is both a and b were integers so their results will be an integer. That so why d stores only the integer part as it is the resultant of a/b. It was automatically added 6 digits of 0 after the integer to convert it integer to float type of data.

Now, look at this code:

#include <stdio.h>
int main ()
{
float a,b,d;
int div;
scanf(“%f %f”,&a,&b);
div=a/b;
d=a/b;
printf(“Division using integer data type div=%dn”,div);
printf(“Division using floating data type d=%fn”,d);

return 0;
}

The same input-output of this code will be:

Division using integer data type div=1
Division using floating data type d=1.500000

As a,b, and div all are float type variables so, d gives the accurate value. But div gives 1 as it is an integer type of variable. In the operation, a/b gives 1.5 but an integer type of variable can’t store a fractional part. That so why it removes the fractional part from the resultant and stores only the integer part that is 1 in div. I think these 2 example gives you a clear idea about the difference between float and int type of data.

Now, suppose you need to use some mathematical operation for example we have to write a program that will take an integer as input and gives the output as the square root of that input. For this, we have to use a function called sqrt() to find the square root of any number. To use this and also some other functions for the mathematical operation we must declare the math header file before starting our program.

Look at this code:

#include <stdio.h>
#include<math.h>
int main ()
{
float a,b;
scanf(“%f”,&a);
b=sqrt(a);
printf(“%fn”,b);

return 0;
}

Here we have included a header file named math.h to use the sqrt() function. As square root of a data can be a fraction that so why we used float types of variables for operation. sqrt() use to calculate the square root of any given number. To evaluate the power of any given number you can use the pow() function.

Syntax: pow(a,b);

It will evaluate the value of a^b.

Now, suppose you have to find the area or length, or width of a triangle. Or, the radius of a circle. For this kind of problem just use the general law for them to find out the required value. For example: suppose you are given the height and width of a triangle. You have to find the area of this triangle. You can do this simply by the following program:

#include <stdio.h>
int main ()
{
int height=10, width=5, area;
area = (height*width)/2 ;
printf(“Area is %d unit.n”,area);

return 0;
}
Output: Area is 25 unit.

In this program, we used integer type of data only. But you can also use float type of data as the area might contain fractions. For this program, if height is 5 and width is 5 then the area will print as 12. But the actual area is 12.5. If we use float instead of int then there will be no problem and it will give an accurate area for all input.

So, try to practice all other these kinds of problems as much as you can. Now I am finishing this tutorial by giving you two tasks.

1) You are given the area of a circle is 100 units square. Write a program that will evaluate the radius of this circle.

2) The temperature for today is 24 degrees Celsius. Your task is to calculate the temperature in Fahrenheit and in kelvin scale using c program.

Hint: Relation between C, F and K is

C/5 =(F-32)/9 =(k-273)/5 .

I would like to remind you that usage of the #define function will reduce your code length and also save you much time while coding. So, don’t forget to check this post to learn how to use the #define function in C programming properly.

Just copy your code from your codeblocks text editor and paste it into the comment box. Don’t try to see other codes try fully by yourself. Keep practicing. Happy coding.