Showing posts with label Mathematical Programs. Show all posts
Showing posts with label Mathematical Programs. Show all posts

Thursday, 9 January 2014

C Program to print table of n and square of n using pow()

Print Table of n and Square of n

#include<stdio.h>
#include<conio.h>
void main()
{
      int n;
      printf("Not Squaren");
      printf("-----------------n");
      for(n=1;n <=10;n++)
          printf("%dt%dn",n,n*n);
      getch();
}
Output :
 No Square
 -------------
  1 1
  2 4
  3 9
  4 16
  5 25
  6 36
  7 49
  8 64
  9 81
 10 100

Explanation of Program :

We can alternately write above program like this -
printf("%dt%dn",n,pow(n,2));
In order to use above line in the code we need to include math.h header file inside C program.
#include<math.h>

Alternate Way Using Pow :

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

void main()
{
      int n;
      printf("Not Squaren");
      printf("-----------------n");
      for(n=1;n <=10;n++)
          printf("%dt%dn",n,pow(n,2));
      getch();
}

C Program to find Factorial of Number without using function

Find Factorial of Number without using function

#include<stdio.h>
#include<conio.h>
void main()
{
int i,number,factorial;
printf("nEnter the number : ");
scanf("%d",&n);

factorial = 1;
for(i=1;i<=n;i++)
      factorial = factorial * i;

printf("nFactorial of %d is %d",n,factorial );
getch();
}
Output :
Enter the number : 5
Factorial of 5 is 120

Explanation of Program :

Before Explaining the program let us see how factorial is calculated -
Factorial of 5 = 5!
               = 5 * 4!
               = 5 * 4  * 3!
               = 5 * 4  * 3 * 2!
               = 5 * 4  * 3 * 2 * 1!
               = 5 * 4  * 3 * 2 * 1 * 0!
               = 5 * 4  * 3 * 2 * 1 * 1
               = 120
Firstly accept the number whose factorial is to be found.
printf("nEnter the number : ");
scanf("%d",&n);
We have to iterate from 1 to (n-1) using for/while/do-while loop. In each iteration we are going to multiply the current iteration number and result.
for(i=1;i<=n;i++)
      factorial = factorial * i;

Some Precautions to be taken :

Precaution 1 : Initialize ‘factorial’ variable

factorial = 1;
before going inside loop we must initialize factorial variable to 1 since by default each c variable have garbage value. If we forgot to initialize variable then garbage value will be multiplied and you will get garbage value as output.

Precaution 2 : For loop must start with 1

Suppose by mistake we write following statement -
for(i=0;i<=n;i++)
      factorial = factorial * i;
then in the very first iteration we will get factorial = 0 and in all successive iteration we will get result as 0 since anything multiplied by Zero is Zero

Precaution 3 : Try to accept lower value to calculate factorial

We have 2 bytes to store the integer in Borland C++ compiler, so we can have maximum limit upto certain thousand. Whenever we try to accept value greater than 20 we will get factorial overflow.

Find Factorial of Number Using Recursion

#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
 int x,n;
 printf("nEnter the value of n :");
 scanf("%d",&n);
 x=fact(n);
 printf("n%d",x);
 getch();
}
int fact(int n)
{
 if(n==0)
  return(1);
 return(n*fact(n-1));
}

C Program to Solve Second Order Quadratic Equation

Program : To obtain solution of second order quadratic equation
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c;
float desc,root1,root2;
clrscr();
printf("nEnter the Values of a : ");
scanf("%f",&a);
printf("nEnter the Values of b : ");
scanf("%f",&b);
printf("nEnter the Values of c : ");
scanf("%f",&c);

desc = sqrt(b*b-4*a*c);

root1 = (-b + desc)/(2.0*a);
root2 = (-b - desc)/(2.0*a);

printf("nFirst Root : %f",root1);
printf("nSecond Root : %f",root2);

getch();
}
Output :
Enter the Values of a : 1
Enter the Values of a : -5

Enter the Values of a : 6

First Root : 3.000000
Second Root : 2.000000

C Program to find sum of two numbers

Program : C Program to find sum of two numbers

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b,sum;
clrscr();

printf("Enter two no: ");
scanf("%d%d",&a,&b);

sum = a+b;

printf("Sum : %d",sum);

getch();
}
Output :
Enter two no: 5 6
Sum : 11

C Program to find the simple interest

C Program to find the simple interest
#include<stdio.h>
#include<conio.h>

void main()
{
int amount,rate,time,si;
clrscr();

printf("nEnter Principal Amount : ");
scanf("%d",&amount);

printf("nEnter Rate of interest : ");
scanf("%d",&rate);

printf("nEnter Period of Time   : ");
scanf("%d",&time);

si = (amount * rate * time)/100;

printf("Simple Intrest : %d",si);

getch();
}
Output :
Enter Principal Amount : 500
Enter Rate of interest : 5
Enter Period of Time   : 2
Simple Interest : 50

C Program to Convert temperature from degree centigrade to Fahrenheit

Program to convert temperature from degree centigrade to Fahrenheit

#include<stdio.h>
#include<conio.h>

void main()
{
float celsius,fahrenheit;
clrscr();

printf("nEnter temp in Celsius : ");
scanf("%f",&celsius);

fahrenheit = (1.8 * celsius) + 32;
printf("nTemperature in Fahrenheit : %f ",fahrenheit);

getch();
}
Output :
Enter temp in Celsius : 32
Temperature in Fahrenheit : 89.59998

C Program to calculate sum of 5 subjects and find percentage

C Program to calculate sum of 5 subjects and find percentage

#include<stdio.h>
#include<conio.h>

void main()
{
int s1,s2,s3,s4,s5,sum,total=500;
float per;

clrscr();

printf("nEnter marks of 5 subjects : ");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);

sum = s1 + s2 + s3 + s4 + s5;

printf("nSum : %d",sum);

per = (sum * 100) / total;

printf("nPercentage : %f",per);
getch();
}
Output :
Enter marks of 5 subjects : 80 70 90 80 80
Sum : 400
Percentage : 80.00

C Program to reverse a given number !

#include<stdio.h>
#include<conio.h>

void main()
{
int num,rem,rev=0;
clrscr();

printf("nEnter any no to be reversed : ");
scanf("%d",&num);

 while(num>=1)
    {
    rem = num % 10;
    rev = rev * 10 + rem;
    num = num / 10;
    }

printf("nReversed Number : %d",rev);
getch();
}
Output :
Enter any no to be reversed : 123
Reversed Number : 321

C Program to calculate gross salary of a person.

C Program to calculate gross salary of a person.

#include<stdio.h>
#include<conio.h>

void main()
{
int gross_salary,basic,da,ta;
clrscr();

printf("Enter basic salary : ");
scanf("%d",&basic);

da = ( 10 * basic ) / 100;
ta = ( 12 * basic ) / 100;

gross_salary = basic + da + ta;

printf("Gross salary : %d",gross_salary);
getch();
}
Output :
Enter basic Salary : 1000
Gross Salart : 1220

C Program to find greatest in 3 numbers

C Program to find greatest in 3 numbers

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b,c;
clrscr();

printf("nEnter value of a, b & c: ");
scanf("%d %d %d",&a,&b,&c);

if((a>b)&&(a>c))
    printf("na is greatest");

if((b>c)&&(b>a))
    printf("nb is greatest");

if((c>a)&&(c>b))
    printf("nc is greatest");

getch();
}
Output :
Enter value for a,b & c : 15 17 21
c is greatest

C program to reads customer number and power consumed and prints amount to be paid

An electric power distribution company charges its domestic consumers as follows

  Consumption   Rate of
  Units  Charge
  ------------------------------------------------------
  0-200       Rs.0.50 per unit
  201-400     Rs.100 plus Rs.0.65 per unit excess 200
  401-600     Rs.230 plus Rs.0.80 per unit excess of 400.
  -------------------------------------------------------

Write a C program that reads the customer number and power consumed and prints the amount to be paid by the customer.

#include<stdio.h>
#include<conio.h>
void main()
{
int n, p;
float amount;
clrscr();
printf("Enter the customer number: ");
scanf("%d",&n);
printf("Enter the power consumed: ");
scanf("%d",&p);

 if(p>=0 && p<=200)
    amount=p*0.50;
 else if(p>200 && p<400)
    amount = 100+((p-200) * 0.65);
 else if(p>400 && p<=600)
    amount = 230 + ((p-400) * 0.80);
printf("Amount to be paid by customer no. %d is Rs.:%5.2f.",n,amount);

getch();
}
Output :
Enter the customer number: 1
Enter the power consumed: 100
Amount to be paid by customer no. 1 is Rs.:50.00.

C program to read the values of x, y and z and print the results expressions in one line.

Problem Statement : Write a program to read the values of x, y and z and print the results of the following expressions in one line.
  1. (x+y+z) / (x-y-z)
  2. (x+y+z) / 3
  3. (x+y) * (x-y) * (y-z)
#include<stdio.h>
#include<conio.h>

void main()
{
int x,y,z;
float a,b,c;
clrscr();

printf("nEnter the values of x,y and z : ");
scanf("%d %d %d",&x,&y,&z);
a = (x+y+z) / (x-y-z);
b = (x+y+z) / 3;
c = (x+y) * (x-y) * (y-z);

printf("a = %fnb = %fnc = %f",a,b,c);
getch();
}
Output:
Enter the values of x,y and z : 1.1 2.5 5.5
a = -1.000000
b = 2010.000000
c = 27939.000000

C Program to find exponent Power Series !!

Program :
A program to evaluate the power series
           x2      x3            xn
ex  =  1 + x + ---  +  --- + ..... + ---- , 0 < x < 1
                2!      3!            n!
It uses if……else to test the accuracy.
The power series contains the recurrence relationship of the type
        Tn   =  Tn-1  (---)   for n > 1

        T1   =  x             for n = 1

        T0   =  1
If Tn-1 (usually known as previous term) is known, then Tn (known as present term) can be easily found by
multiplying the previous term by x/n. Then
  ex   =  T0 +  T1  +  T2 + ...... +  Tn  =  sum

C Program for Exponent Series :

#include<stdio.h>
#define ACCURACY 0.0001                                     

main()
{
 int n, count;
 float x, term, sum;

 printf("Enter value of x:");
 scanf("%f", &x);

 n = term = sum = count = 1;

 while (n <= 100)
     {
     term = term * x/n;
     sum = sum + term;
     count = count + 1;
       if (term < ACCURACY)
           n = 999;
       else
           n = n + 1;
    }

 printf("Terms = %d Sum = %fn", count, sum);
 }
Output :
Enter value of x:0
   Terms = 2 Sum = 1.000000

   Enter value of x:0.1
   Terms = 5 Sum = 1.105171

   Enter value of x:0.5
   Terms = 7 Sum = 1.648720

   Enter value of x:0.75
   Terms = 8 Sum = 2.116997

   Enter value of x:0.99
   Terms = 9 Sum = 2.691232

   Enter value of x:1
   Terms = 9 Sum = 2.718279

C Program to Compute sum of the array elements using pointers !

Write a ‘C’ Program to compute the sum of all elements stored in an array using pointers

C Program to compute sum of the array elements using pointers

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10];
 int i,sum=0;
 int *ptr;

 printf("Enter 10 elements:n");

 for(i=0;i<10;i++)
    scanf("%d",&a[i]);

 ptr = a;           /* a=&a[0] */

 for(i=0;i<10;i++)
    {
    sum = sum + *ptr;    //*p=content pointed by 'ptr'
    ptr++;
    }

 printf("The sum of array elements is %d",sum);
}

Output :

Enter 10 elements : 11 12 13 14 15 16 17 18 19 20

The sum of array elements is 155

Explanation of Program :

Accept the 10 elements from the user in the array.
for(i=0;i<10;i++)
      scanf("%d",&a[i]);
We are storing the address of the array into the pointer.
ptr = a;
Now in the for loop we are fetching the value from the location pointer by pointer variable. Using De-referencing pointer we are able to get the value at address.
for(i=0;i<10;i++)
    {
    sum = sum + *ptr;
    ptr++;
    }
Suppose we have 2000 as starting address of the array. Then in the first loop we are fetching the value at 2000. i.e
sum = sum + (value at 2000)
    = 0   + 11
    = 11
In the Second iteration we will have following calculation -
sum = sum + (value at 2002)
    = 11  + 12
    = 23

C Program to Calculate Area and Circumference of circle

C Program to find area and circumference of circle

#include<stdio.h>

int main()
{
int rad;
float PI=3.14,area,ci;

printf("nEnter radius of circle: ");
scanf("%d",&rad);

area = PI * rad * rad;
printf("nArea of circle : %f ",area);

ci = 2 * PI * rad;
printf("nCircumference : %f ",ci);

return(0);
}

Output :

Enter radius of a circle : 1
Area of circle : 3.14
Circumference  : 6.28

Explanation of Program :

In this program we have to calculate the area and circumference of the circle. We have following 2 formulas for finding circumference and area of circle.
Area of Circle = PI * R * R
and
Circumference of Circle = 2 * PI * R
In the above program we have declared the floating point variable PI whose value is defaulted to 3.14.We are accepting the radius from user.
printf("nEnter radius of circle: ");
scanf("%d",&rad);