How to divide numbers in C-Language

 

How to divide numbers in C-Language

Divide of numbers in C-Language


Q:- How to Divide two constant numbers?




How to divide numbers in C-Language






Answer:

  1. #include <stdio.h>
  2. int main() {
  3.        const float h=6;
  4.        const float k=2;
  5.        float res_;
  6.        res_=h/k;
  7.        printf("Division = %.2f",res_); 
  8.        return 0;
  9. }
Code Explanation:
 We include preprocessing directory in line one .
 We use main() function in second line.
 We define two constants variable which can not be change in third and fourth line .
 We create an 'res_' variable with integer data type in fifth line .
 We use 'res_' variable to store multiplication  of 'h' from 'k'   line six.
 Print the 'res_' variable  in seventh line.
 On eight line  we end the program with the value of '0'.
 In line nine we close the curly braces which we open in second line. 
output:

How to divide numbers in C-Language







Note: If you use constants so you can not change your constant values during runtime.




Q:- How to Divide two numbers by taking input from user?



How to Divide two numbers by taking input from user?







Answer:

  1. #include <stdio.h> 
  2. int main() {
  3.        float s,t,v;
  4.        printf("Enter First Number:");
  5.        scanf("%f",&s);
  6.        printf("Enter Second Number:");
  7.        scanf("%f",&t);
  8.        if(t==0)
  9.        {
  10.         printf("Cannot divide by zero");
  11.         } 
  12.        else{
  13.        v=s/t;
  14.        printf("Your answer  is %.2f",v);
  15.        }
  16.        return 0;
  17. }
Code Explanation:
 
On line one we include preprocessing directory.
Second line we use main() function.
Third  line we define three variable in the float data type to get answer in decimal.
Fourth line we ask from user to enter first number.
Fifth line we get  1st input number from the user.
Sixth line we ask from user to enter Second number.
Seventh line we get  2nd  number as a input from the user.
Eighth line we use if statement for checking second variable 't' is must be not equal to zero.
In line nine we open curly braces for an if statement.
We display a massage to the user ""Cannot divide by zero"" in tenth line.
Eleven line we close the curly braces which we open in for an if statement.
Then in line twelve we apply an else condition that if user not insert '0' so what action perform.
In line thirteen we create a variable 'v' which store the division of two numbers.
On fourteen line we display the answer to the user.

Q:-Why we use "%.2f"?

  • "%f" is an format specifier for float data type .
  • .2 for getting only two digits after decimal point.
In line fifteen we close the curly bracket of else .
We return the value '0' in line sixteen.
For line seventeen we close the curly bracket of main() because if we open bracket so it must be closed.

Output:

How to Divide two numbers by taking input from user?









This is the code for Divide of two numbers .You can add only few lines of similar code so you can multiply more than two numbers.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.