Monday, October 27, 2014

ADVANCE C PROGRAMMES

Calculate the Area of a Triangle
#include
#include
#include

void main()
{
    int s, a, b, c, area;
    clrscr();
    printf("Enter the values of a, b and c \n");
    scanf("%d %d %d", &a, &b, &c);
    s = (a + b + c) / 2;
    area = sqrt(s * (s - a) * (s - b) * (s - c));
    printf("Area of a triangle = %d \n", area);
    getch();
}

Output

Enter the values of a, b and c
12 10 8
Area of a triangle = 39

Calculate the Area of a Circle

#include 
#include 
#include 
#define PI 3.142
 
void main()
{
    float radius, area;
 
    clrscr();
    printf("Enter the radius of a circle \n");
    scanf("%f", &radius);
    area = PI * pow(radius, 2);
    printf("Area of a circle = %5.2f\n", area);
    getch();
}

Output
 
Enter the radius of a circle
30
Area of a circle = 2827.80

Calculate the Simple Interest

#include 
#include 
void main()
{
    float principal_amt, rate, simple_interest;
    int time;
    clrscr();
    printf("Enter the values of principal_amt, rate and time \n");
    scanf("%f %f %d", &principal_amt, &rate, &time);
    simple_interest = (principal_amt * rate * time) / 100.0;
    printf("Amount = Rs. %5.2f\n", principal_amt);
    printf("Rate = Rs. %5.2f%\n", rate);
    printf("Time = %d years\n", time);
    printf("Simple interest = %5.2f\n", simple_interest);
    getch();
}

Output
 
Enter the values of principal_amt, rate and time
12
10
5
Amount = Rs. 12.00
Rate = Rs. 10.00%
Time = 5 years
Simple interest =  6.00

Find out the Roots of a Quadratic Equation

#include 
#include 
#include 
#include 
 
void main()
{
    float a, b, c, root1, root2;
    float realp, imagp, disc;
    clrscr();
    printf("Enter the values of a, b and c \n");
    scanf("%f %f %f", &a, &b, &c);
    if (a == 0 || b == 0 || c == 0)
    {
        printf("Error: Roots cannot be determined \n");
        exit(1);
    }
    else
    {
        disc = b * b - 4.0 * a * c;
        if (disc < 0)
        {
            printf("Imaginary Roots\n");
            realp = -b / (2.0 * a) ;
            imagp = sqrt(abs(disc)) / (2.0 * a);
            printf("Root1 = %f  +i %f\n", realp, imagp);
            printf("Root2 = %f  -i %f\n", realp, imagp);
        }
        else if (disc == 0)
        {
            printf("Roots are real and equal\n");
            root1 = -b / (2.0 * a);
            root2 = root1;
            printf("Root1 = %f\n", root1);
            printf("Root2 = %f\n", root2);
        }
        else if (disc > 0 )
        {
            printf("Roots are real and distinct \n");
            root1 =(-b + sqrt(disc)) / (2.0 * a);
            root2 =(-b - sqrt(disc)) / (2.0 * a);
            printf("Root1 = %f  \n", root1);
            printf("Root2 = %f  \n", root2);
        }
    }
    getch();
}
 
Output
 
Enter the values of a, b and c
45 50 65
Imaginary Roots
Root1 = -0.555556  +i 1.065740
Root2 = -0.555556  -i 1.065740

Simulate a Simple Calculator

#include 
#include  
void main()
{
    char operator;
    float num1, num2, result;
    clrscr();
    printf("Simulation of a Simple Calculator\n");
    printf("*********************************\n");
    printf("Enter two numbers \n");
    scanf("%f %f", &num1, &num2);
    fflush(stdin);
    printf("Enter the operator [+,-,*,/] \n");
    scanf("%s", &operator);
    switch(operator)
    {
    case '+': result = num1 + num2;
        break;
    case '-': result = num1 - num2;
        break;
    case '*': result = num1 * num2;
        break;
    case '/': result = num1 / num2;
        break;
    default : printf("Error in operationn");
        break;
    }
    printf("\n %5.2f %c %5.2f = %5.2f\n", num1, operator, num2, result); 
getch();
}

Output
 
Simulation of a Simple Calculator
*********************************
Enter two numbers
2 3
Enter the operator [+,-,*,/]
+
 
2.003.005.00
 
Simulation of a Simple Calculator
*********************************
Enter two numbers
50 40
Enter the operator [+,-,*,/]
*
 
50.00 * 40.00 = 2000.00
Simulation of a Simple Calculator
*********************************
Enter two numbers
500 17
Enter the operator [+,-,*,/]
/
 
500.00 / 17.00 = 29.41
Simulation of a Simple Calculator
*********************************
Enter two numbers
65000 4700
Enter the operator [+,-,*,/]
-
 
65000.00 - 4700.00 = 60300.00

Find the Sum of First N Natural Numbers

#include 
#include 
void main()
{
    int i, num, sum = 0;
    clrscr();
    printf("Enter an integer number \n");
    scanf ("%d", &num);
    for (i = 1; i <= num; i++)
    {
        sum = sum + i;
    }
    printf ("Sum of first %d natural numbers = %d\n", num, sum);
    getch();
}

Output
 
Enter an integer number
1300
Sum of first 1300 natural numbers = 845650
 
Enter an integer number
15
Sum of first 15 natural numbers = 120



Find First N Fibonacci Numbers

#include 
#include  
void main()
{
    int fib1 = 0, fib2 = 1, fib3, num, count = 0;
    clrscr();
    printf("Enter the value of num \n");
    scanf("%d", &num);
    printf("First %d FIBONACCI numbers are ...\n", num);
    printf("%d\n", fib1);
    printf("%d\n", fib2);
    count = 2; /* fib1 and fib2 are already used */
    while (count < num)
    {
        fib3 = fib1 + fib2;
        count++;
        printf("%d\n", fib3);
        fib1 = fib2;
        fib2 = fib3;
   }
   getch();
}

Output
 
Enter the value of num
15
First 15 FIBONACCI numbers are ...
0
1
1

2

No comments:

Post a Comment