Sunday, 26 October 2014

find area of circle


Program to find area of circle

Let’s see how to write this C program

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float area,radius;
const float pi=22/7;
printf("enter the radius of circle ");
scanf("%f",&radius);

area=2*pi*pow(radius,2) ;
//area=2*pi*radius*radius;

printf("area of the circle is = %f",area);
getch();
return 0;
}


Explanation of the program


float area,radius;
Float is a kind of data type which is of size ‘4bytes’.
it is use store the decimal values or we can say real number.
we are declaring area and radius as the variable of float datatype.

const float pi=3.1428;
Here we are declaring a variable named as pi with the constant value of 3.1428.
Const is keyword which specify that variable pi is a constant so its value can’t be changed in run time of the program.

scanf("%f",&radius);
It is use to receive the the value for variable radius.
As we know that the radius is of datatype float so we need to use “%f” to store that float value.

area=2*pi*pow(radius,2);
This is a mathematical expression equivalent to ‘area=2*π*radius2 ‘.
We can also write it as     area=2*pi*radius*radius;

pow(radius,2)
pow() is a power function in the header file of math.h.
which accepts two parameters one is the number and another is the power of that number.
Eg:- if we want to write
A2  then we write it as pow(A,2);
A4  then we write it as pow(A,4);
2n  then we write it as pow(2,n);

printf("area of the circle is = %f",area);
This statement will display the “area of the circle is = “ followed by the value stored in the variable area.

%f
This tells the compiler that we are going to display or read the real number.

Getch(); and return 0;
We already discussed this click here to move back.


Pictures of program:

Step 1:  Create the program:-





Step 2: output 




Note:  if any of the programmer is facing the problem of repapering of the previous screen again and again

He can use the function clrscr();
It present in the conio.h header file
After declaring the variables

Sunday, 19 October 2014

print addition of 2 user numbers


Program to print addition of 2 user numbers


Let’s see how to write this C program

#include<conio.h>
#include<stdio.h>
int main()
{
int fnum,snum,result;
printf("enter first number ");
scanf("%d",&fnum);
printf("enter second number ");
scanf("%d",&snum);

result=fnum+snum;

printf("result =%d",result);
getch();
return 0;
}


Explanation of the program


int fnum,snum,result;
Here we initialize our 3 variables named as  fnum,snum and result. These variable currently holds no value.

printf("enter first number ");
use to print as it is on the screen “enter first number“.

scanf("%d",&fnum);
allow user to enter any desired value in fnum variable.

printf("enter second number ");
use to print as it is on the screen “enter second number“.

scanf("%d",&snum);
allow user to enter any desired value in fnum variable.

result=fnum+snum;;
in this statement we add up the values of fnum and snum and then we pass that value to the variable result. Now result is holding up the addition of fnum and snum variable.

printf("result is = %d",result);
this will going to display the value stored in the variable result.
In this we combine it with the string as firstly it will display the 
“result is = “
Then the value of result is being displayed.

%d
This tells the compiler that we are going to display or read the value of integer type.

Getch(); and return 0;
We already discussed this click here to move back.


Pictures of program:

Step 1:  Create the program:-






Step 2: output 



Note:  if any of the programmer is facing the problem of reappearing of the previous screen again and again

He can use the function clrscr();
It present in the conio.h header file
After declaring the variables

Sunday, 12 October 2014

print addition of 2 default numbers


Program to print addition of 2 default numbers


Let’s see how to write this C program

#include<conio.h>                   
#include<stdio.h>
int main()                                   
{
int first= 10,second=12;       
int result;   
              
result = first+second;

printf("result is = %d",result);
getch();
return 0;
}

Explanation of the program

Int first=10,second=12;
This statement we going to initialize the two variables in the memory named as first and second which holds the default values of 10 and 12 respectively.

int result;
Here we initialize our 3rd variable named as result. This currently holds no value

result=first+second;
in this statement we add up the values of first and second and then we pass that value to the variable result. Now result is holding up the addition of first and second variable.

printf("result is = %d",result);
this will going to display the value stored in the variable result.
In this we combine it with the string as firstly it will display the 
“result is = “
Then the value of result is being displayed.

%d
This tells the compiler that we are going to display the value of integer variable

Getch()and return 0
We already discussed this click here to move back.

Pictures of program:

Step 1:  Create the program:-




Step 2: output 



Note:  if any of the programmer is facing the problem of reappearing of the previous screen again and again

He can use the function clrscr();
It present in the conio.h header file
After declaring the variables

Sunday, 5 October 2014

Printing your name on the screen using gets() in C language

Printing your name on the screen using gets() in C language



Let’s see how to write this C program

#include<conio.h>
#include<stdio.h>
int main()
{
char myname[20];
printf(“enter your name : “);
gets(myname);
printf(“my name is %s“,myname);
getch();
return 0;
}


Details about scanf() and gets()

As you can see this program is almost similar to the previous program we have done.

The difference is that in this program we have used the gets() instead of scan() function to get input from the user.

Gets() is the function under the stdio.h header file.

Gets() can also be capable of receiving blank space inputs as scanf() is not

It is specially made for character type inputs on other hand scanf() is better for receiving the numeric inputs .
                            


Explanation of the program


As it is similar to the previous program so. I start explaining it from little ahead

Whatever user is going to type it will be store in the variable myname as a string or character.

And then we going to display it on the screen similarly as we have done it into our previous program using printf() function.


Pictures of the program


Step1: write a program




Step2: compile and run
Now I am not going to show how to run the program I have tell all that in my previous program or you can move to my first program for that.




Step3: output




Now here you can enter any of the string may be with or without blank space

With the help of gets() function we can also receive the character inputs like blank space.

You may increase the size of myname variable to hold the string of larger size

Example:
char myname[I];
//here I = any positive integer        

Note:  if any of the programmer is facing the problem of reappearing of the                      previous screen again and again
           He can use the function clrscr();
           It present in the conio.h header file
           After declaring the variables


Example in this program

……
………..
char myname[20];
clrscr();
printf(“enter your name : “);
…………
……………….