Friday, 21 November 2014

Break up the program that you wrote to solve above problem into two separate source files. The main function should be in one file & the calculation function must be in another file. And modify the program so that the interest rate is a symbolic constant and is no longer input from the keyboard. And put all the C preprocessor directives into a separate header file that is included in the two program source files.

#include "header.h"
main()
{
            float amt,interest;
            int year;

            float comp_int_calc(float,float,int);

            clrscr();

            printf("Enter the initial amount: ");
            scanf("%f",&amt);

            printf("Enter the Number of years: ");
            scanf("%f",&year);

            interest=comp_int_calc(amt,roi,year);
            printf("\nThe int is %.2f",interest);
            getch();
}


file-2.c

#include "header.h"
float comp_int_calc(float x,float y,int z)
{
            float i;
            i=x*pow((1+y/100),2);
            return(i-x);
}

header.h

#include<stdio.h>
#include<math.h>
#define roi 10

Then press Alt+P in Turbo C and enter a project file name, e.g. Q11.prj. Create a new project file of the same name e.g. Q11.prj and enter the following in it-

file-1.c
file-2.c
header.h

Now compile the project file and the desired output will be obtained.

Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted. You can assume that x,p & n are integer variables and that the function will return an integer.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
            int intUserInput, intUserInput1, intUserInput2;
            int intCompResult;
            int invert(int, int, int);
            clrscr();
            printf("\n\n\t Please insert integer to invert: ");
            scanf("%d", &intUserInput);

            printf("\n\n\t Please insert starting point to invert: ");
            scanf("%d", &intUserInput);

            printf("\n\n\t Please insert Length to invert: ");
            scanf("%d", &intUserInput2);

            intCompResult=invert(intUserInput, intUserInput1, intUserInput2);
            printf("\n\n\t Invert no. is : %d", intCompResult);
            getch();
}
int invert(int x, int p, int n)
{
            int intbinary[8];
            int i;
            int y;
            int r=0;
            for(i=0;i<8;i++)
            {
                        intbinary[i]=0;
            }
            i=0;
            y=0;
            while(x>0)
            {
                        intbinary[i]=x%2;
                        x=x/2;
                        i++;
            }
            for(i=0;i<8;i++)
            {
                        if(i==p)
                        {
                                    for(i=p;i>p-n;i--)
                                    {
                                                if(intbinary[i]==0)
                                                {
                                                            intbinary[i]=1;
                                                }
                                                else
                                                {
                                                intbinary[i]=0;
                                                }
                                    }
                                    i=i+n;
                        }
            }
            for(i=0;i<8;i++)
            {
                        r=r+(intbinary[i]*pow(2,i));
            }
            return r;

}

Write an interactive program to calculate simple Interest and Compound Interest.

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

void main()
{
            float pri,amt,rate,si,ci,time,interest,i,j=1;
            clrscr();
            printf("\n\n\t\tEnter the principle -> ");
            scanf("%f",&pri);
            printf("\n\n\t\tEnter the rate -> ");
            scanf("%f",&rate);
            printf("\n\n\t\tEnter the time In Year -> ");
            scanf("%f",&time);
            // -------Programm For Simple Interest---------

            interest=(pri*rate*time)/100;
            si=pri+interest;
            printf("\n\n\t\tYour Interest is %f",interest);
            printf("\n\n\t\tYour Simple Interest is %f",si);

            // ------Programm For Compound Interest--------

            for(i=1; i<=time; i++)
            {
                        j=(rate+100)/100*j;
            }
            ci=pri*j;
            interest=ci-pri;
            printf("\n\n\t\tYour Interest in Compound is %f",interest);
            printf("\n\n\t\tYour Compound Interest is %f",ci);
            getch();
}