The Programming Project: Program
Showing posts with label Program. Show all posts
Showing posts with label Program. Show all posts

Tuesday, September 21, 2021

ISC COMPUTER SCIENCE PRACTICAL SOLVED 2021 QUESTION 1: FASCINATING NUMBERS

 QUESTION 1:

A Fascinating number is one which when multiplied by 2 and 3 and then, after the results are concatenated with the original number, the new number contains all the digits from 1 to 9 exactly once. There can be any number of zeros and are to be ignored.

Example: 273

                273 x 1 = 273

                273 x 2 = 546

                273 x 3 = 819

Concatenating the results we get, 273546819 which contains all digits from 1 to 9 exactly once.

Thus, 273 is a Fascinating number.

Accept two positive integers m and n, where m must be less than n and the values of both ‘m’ and ‘n’ must be greater than 99 and less than 10000 as user input. Display all Fascinating numbers that are in the range between m and n (both inclusive) and output them along with the frequency , in the format given below:

 

Test your program with the following data and some random data:

Example 1

INPUT                   m = 100

                                 n = 500

OUTPUT: THE FASCINATING NUMBERS ARE:

                192  219  273  327

                FREQUECNY OF FASCINATING NUMBERES IS: 4

Example 2

INPUT                   m = 900

                                 n = 5000

OUTPUT: THE FASCINATING NUMBERS ARE:

                1902  1920  2019  2190  2703  2730  3027  3270

                FREQUECNY OF FASCINATING NUMBERES IS: 8

Example 3

INPUT                   m = 400

                                 n = 900

OUTPUT: THE FASCINATING NUMBERS ARE:

                NIL

                FREQUECNY OF FASCINATING NUMBERES IS: 0

 

 import java.util.*;

public class ISC2021QUESTION1 {
       public static void main(String[] args) {
       int m,n;
       Scanner in = new Scanner(System.in);
       do {
        System.out.println("Enter the lower bound(m):");
        m = in.nextInt();
        System.out.println("Enter the upper bound(n):");
        n = in.nextInt();
       }while (m < 99 || n > 10000 || m < n); // conditions check
       Fascinating obj = new Fascinating(m,n);
       obj.generateFascinatingNumbers();
       in.close();
       }
}
class Fascinating {
    public void generateFascinatingNumbers() {
        System.out.println("THE FSCINATING NUMBERS ARE:");
        for (int i=m;i<=n;i++){
            if(isFascinating(i)==true) {
                System.out.println(" "+i+" ");
                frequency++;
            }
        }
        if(frequency==0) {
            System.out.println("NIL:");
            System.out.println("FREQUNCY OF THE FASCINATING NUMBERS IS:"+frequency);
        }
        else
            System.out.println("FREQUNCY OF THE FASCINATING NUMBERS IS:"+frequency);
    }
    private boolean isFascinating(int numb){
        int digitFrequency=0// local variable
        String temp = "";
        // concatenating the numbers as stated
        temp = Integer.toString(numb)+Integer.toString(numb*2)+Integer.toString(numb*3);
        // checking that the frequecny of each digit is 1 or not
        for(int j=0j < 9j++){ // running through digit 1 to 9
            digitFrequency = 0;
            flag = true;
            for(int i=0;i<temp.length();i++){
                if(check.charAt(j)==temp.charAt(i)){
                    digitFrequency++;
                }
            }
            if(digitFrequency != 1
                flag = false;
            if(flag==false
                break;
        }
        return (flag == false? false : true);
    }
    Fascinating(int mint n)    {
        this.n = n;
        this.m = m;
        frequency = 0;
    }
    private static String check = "123456789";
    private int n;
    private int m;
    private int frequency;
    private boolean flag;
}

 

Sunday, December 6, 2020

PYTHON PROGRAM FOR BEGINNERS : SCHOOL LEVEL

 Calculating How Old Your Dog is in Human Years

This assignment is designed to give you practice writing code and applying lessons and topics for the current module.


This homework deals with the following topics:


Getting user input

Error checking

Variables & data types

Conditionals

The Assignment


In this assignment, you will write a program in Python that calculates a dog’s age in human years.


The program will prompt the user for an age in dog years and calculate that age in human years.  Allow for int or float values, but check the user’s input to make sure it's valid -- it should be numeric and positive.  Otherwise, let the user know their input is not valid.


You can use the following rules to approximately convert a medium-sized dog’s age to human years:


For the first year, one dog year is equal to 15 human years

For the first 2 years, each dog year is equal to 12 human years

For the first 3 years, each dog year is equal to 9.3 human years

For the first 4 years, each dog year is equal to 8 human years

For the first 5 years, each dog year is equal to 7.2 human years

After that, each dog year is equal to 7 human years.  (Note: This means the first 5 dog years are equal to 36 human years (5 * 7.2) and the remaining dog years are equal to 7 human years each.)

Print the result in the following format, substituting for <dog_age> and <human_age>: "The given dog age <dog_age> is <human_age> in human years."  Round the result to 2 decimal places.  


For example:


If the user enters 2, the program will print: “The given dog age 2 is 24.0 in human years.”

If the user enters 3, the program will print: “The given dog age 3 is 27.9 in human years.”

If the user enters 4.5, the program will print: “The given dog age 4.5 is 32.4 in human years.”

If the user enters 12.1, the program will print: “The given dog age 12.1 is 85.7 in human years.”

Considering invalid inputs:


Your program must ask the user for an age in dog years - hint: use the input() function

We are going to test invalid inputs - make sure that your code can handle negative value inputs and non-numerical inputs!

For invalid inputs, make sure that your printed response adheres to the following:

     - If a text-based input is provided, make sure your response contains the word 'invalid'.  For example, if the user doesn’tinput a number, print “<age> is invalid.”, substituting for <age>.


     - If a negative input is provided, make sure your response contains the word 'negative'.  For example, if the user inputs a negative number, print “Age cannot be a negative number.”

  

  Python CODE:

 dog_age=(input("Enter the dog's age:"))

flag=False
try:
    dog_age = float(dog_age)
    dog_age_in_Human_years=0
    if dog_age <0:
        flag = True
    elif 0 < dog_age <=1:
        dog_age_in_Human_years += dog_age*15.0
    elif 1dog_age <=2:
        dog_age_in_Human_years += dog_age*12.0
    elif 2dog_age <=3:
        dog_age_in_Human_years += dog_age*9.3
    elif 3dog_age <=4:
        dog_age_in_Human_years += dog_age*8.0
    elif 4dog_age <=5:
        dog_age_in_Human_years += dog_age*7.2
    else:
        dog_age_in_Human_years += 5*7.2+(dog_age-5)*7.0 
except ValueError as e:
    print("Your input is invalid")
if flag == False:
    print("The given dog age"dog_age"is"round(dog_age_in_Human_years,2), "in human years.")
else:
    print("Age cannot be a negative number")
    

Thursday, April 17, 2014

Binomial Coefficient using recursion in Python and C language





C Code
#include<stdio.h>
long int Binomial(long int, long int);
int main()
    {
    long int n,k;
    printf("\n Enter the value of n and k, n is greater than k:");
    scanf("%ld %ld",&n,&k);
    while( k < 0 || n < 1 || k > n) {
        printf("\n Wrong input, enter again:");
        scanf("%ld %ld",&n,&k);
        }
    printf("\n Value of %ldC%ld: %ld \n", n,k,Binomial(n,k));
    return 0;
    }
long int Binomial(long int n, long int k)
    {
    return (k <= 1 ? (k == 0 ? 1 : n) : (n*Binomial(n-1,k-1))/k);
    } 


Python Code
 
#File: binomial.py
#A simple program to calculate nCk using recursion
def main():
    print "Binomial Coefficient"
    n = input("Enter the value of n:")
    k = input("Enter the value of k:")
    x,y= n,k
    def binomial(n,k):
        if k < 2:
            if k == 1:
                return n
            else:
                return 1
        else:
            return (n*binomial(n-1,k-1))/k
   
    print "Value of nCk is",binomial(x,y)
   
main()


OUTPUT:

administrator@ubuntu:~/python$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:26)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import binomial
Binomial Coefficient
Enter the value of n:1200
Enter the value of k:600
Value of nCk is 396509646226102014036716014862605729531608195706791651137874888645453416610941265150218719101931467943643355545203450497992759241509133380338379948816055787676920090991204851973167965845932778899299658455186568000803781988756668261491937388063732393433822461878746138860879613812823280622769290795808335597761702284943981759657834318699226167559487050708295600
>>>

/* To calculate the Binomial Coeffcient using recursion */
/* We have use the formula nCk = (n/k)*{(n-1)C(k-1)} */

Monday, September 16, 2013

Intersection of Sets



To find intersection of two sets through C

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void inter_set(int *s1,int*s2,int n,int m);
int main()
{
int n,m,i;
int *s1,*s2;
printf("\n Enter the number of elements in the sets:");
scanf("%d %d",&n,&m);
s1=(int *)malloc(n*sizeof(int));
s2=(int *)malloc(m*sizeof(int));
printf("\n Enter the elements of the set 1:");
for(i=0;i<n;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s1[i]);
}
printf("\n Enter the elements of the set 2:");
for(i=0;i<m;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s2[i]);
}
inter_set(s1,s2,n,m);
return 0;
}
void inter_set(int *s1,int*s2,int n,int m)
{
int *s,i=0,j,k;
int flag=0;
s=(int *)malloc((m<n ? m: n)*sizeof(int));
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
if(s2[j]==s1[k])
{
flag=1;
break;
}
}

if(flag==1)
{
s[i]=s2[j];
         i++;
flag=0;
}
 if(flag==0)
continue;
 }
 if(i==0)
printf("\n Intersection is Null:");
else
{
  printf("\n Intersection of the entered sets is:");
  printf("\n { ");
  for(j=0;j<i-1;j++)
printf(" %d,",s[j]);
  printf(" %d }\n ",s[i-1]);
}


return;
}

Tuesday, September 10, 2013

Divided Difference




#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
void differenceTable(double **A,double **L,int ROW);
double result(double **A,double **L,double x,int ROW);
double factorial(int n);
FILE *fp;
int main(int argc, char *argv[])
{
double **A,*b,*xn,*xnplus1,*p,temp,x,**L,**U,lambda,error,t1=0.0;
int ROW,i,j,k=0,t=1,m=1,bo=1;
char ch;
if(argc==1)
    {
    printf("\n Enter the number of values:");
    scanf("%d",&ROW);
    printf("\n Enter the value at which value has to be aprroximated:");
    scanf("%lf",&x);
    A=(double **)malloc((ROW+1)*sizeof(double*));
    L=(double **)malloc((ROW+1)*sizeof(double*));
    U=(double **)malloc((ROW+1)*sizeof(double*));
    for(i=0;i<=ROW;i++)
        {
        A[i]=(double *)malloc((ROW+1)*sizeof(double));
        L[i]=(double *)malloc((ROW+1)*sizeof(double));
        U[i]=(double *)malloc((ROW+1)*sizeof(double));
        }
    b=(double *)malloc((ROW+1)*sizeof(double));
    p=(double *)malloc((ROW+1)*sizeof(double));
    xn=(double *)malloc((ROW+1)*sizeof(double));
    xnplus1=(double *)malloc((ROW+1)*sizeof(double));
    for(i=1;i<=ROW;i++)
        {
        printf("\n Enter the value of X[%d]:",i);
        scanf("%lf",&A[i][1]);
        }
    for(i=1;i<=ROW;i++)
        {
        printf("\n Enter the value of Y[%d]:",i);
        scanf("%lf",&A[i][2]);
        }
       
    }
else if (argc==2)
    {
    fp=fopen(argv[1],"r");
        if(fp==NULL)
            {
            printf("\n File not found, program will terminate:");
            exit(0);
            }
    fscanf(fp,"%d",&ROW);
    fscanf(fp,"%lf",&x);
    A=(double **)malloc((ROW+1)*sizeof(double*));
    L=(double **)malloc((ROW+1)*sizeof(double*));
    U=(double **)malloc((ROW+1)*sizeof(double*));
    for(i=0;i<=ROW;i++)
        {
        A[i]=(double *)malloc((ROW+1)*sizeof(double));
        L[i]=(double *)malloc((ROW+1)*sizeof(double));
        U[i]=(double *)malloc((ROW+1)*sizeof(double));
        }
    b=(double *)malloc((ROW+1)*sizeof(double));
    p=(double *)malloc((ROW+1)*sizeof(double));
    xn=(double *)malloc((ROW+1)*sizeof(double));
    xnplus1=(double *)malloc((ROW+1)*sizeof(double));
   
    while(!feof(fp))
    {
    for(i=1;i<=ROW;i++)
    {
        for(j=1;j<=2;j++)
        {
        fscanf(fp,"%lf ",&A[i][j]);
        }
       
    }
    }
    fclose(fp);
    }
    else
    {
    printf("\n Invalid Arguments,program will terminate:\n");
    exit(0);
    }
printf("\n         X          Y");
printf("\n------------------------------------------------------------------\n");
for(i=1;i<=ROW;i++)
    {
    for(j=1;j<=2;j++)
        {
       
        U[i][j]=A[i][j];
        printf("   %+lf",U[i][j]);
        }
    printf("\n");
    }
for(i=1;i<=ROW-1;i++)
    {
    for(j=ROW-i,t=1;j>=1;j--,t++)
        {
        L[t][i]=(U[t+1][2]-U[t][2])/(A[t+1+k][1]-A[t][1]);
        //printf("\n (%d,%d)------>%lf",t-1,t+1-1+k,L[t][i]);
        }
    for(t=1;t<=ROW-i;t++)
        U[t][2]=L[t][i];
    //printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    k++;
    }   
differenceTable(A,L,ROW);
printf("\n Value at %+lf by Divied Difference Interpolation formula is %+lf\n",x,result(A,L,x,ROW));
return 0;
}
double result(double **A,double **L,double x,int ROW)
{
int i,j;
double value=0,tmp=1.0;
for(i=0;i<ROW;i++)
{
    if(i==0)
        value +=A[1][2];
    else
    {
    for(j=1;j<=i;j++)
        tmp=tmp*(x-A[j][1]);
    tmp = tmp/factorial(i);
    tmp = tmp*L[1][i];
    value +=tmp;
    }
    if(i==0)
    printf("%2.2lf ",A[1][2]);
    else
    printf("%2.2lf ",L[1][i]);
    tmp=1.0;
}
return value;
}
double factorial(int n)
{
 if(n<=1)
     return 1;
 else
     return n*factorial(n-1);
}
void differenceTable(double **A,double **L,int ROW)
{
int **position,*ap,j,m,i;
position=(int **)malloc((ROW+1)*sizeof(int*));
ap=(int *)malloc((ROW+1)*sizeof(int));
for(i=0;i<=ROW;i++)
    position[i]=(int *)malloc((ROW+1)*sizeof(int));
int an,tmp;
tmp=ROW;
for(i=1;i<=ROW;i++)
    {
    an=1+(i-1)*2;
    ap[i]=an;
    for(j=1;j<=tmp;j++)
        {
        position[i][j]=an+(j-1)*4;
        }
    tmp--;
    }
tmp=ROW;
tmp=ROW+(ROW-1)*3;
int *match,tmp1,l,*pos,flag,z,k;
match=(int *)malloc((ROW+1)*sizeof(int));
pos=(int *)malloc((ROW+1)*sizeof(int));
for(i=0;i<=ROW;i++)
    {
    match[i]=0;
    pos[i]=0;
    }

printf("\n-------------------------------------Divided Difference Table----------------------------------\n");
printf("\n X        Y");
for(i=1;i<=ROW-1;i++)
    printf("                D^%d",i);
printf("\n----------------------------------------------------------------------------------------------\n");
for(i=1;i<=tmp;i++)
{
tmp1=ROW;

    for(l=1;l<=ROW;l++)
    {
    flag=0;
    for(m=1;m<=tmp1;m++)
        {
        if(i==position[l][m])
            {
            flag=1;
            match[l]=1;
            pos[m]=position[l][m];
            break;
            }
        } //inner for
    if(flag==1)
    {
    for(k=1;k<=ROW;k++)
        {
    if(match[k]==0)
    {
        printf("");
    }
    else
        {
        if(k==1)
            //printf("(%d,%d)+(%d,%d)|%d",(position[l][m]/4)+1,k,(position[l][m]/4)+1,k+1,i);
            printf("%+2.4lf\t %+2.4lf",A[(position[l][m]/4)+1][k],A[(position[l][m]/4)+1][k+1]);
        else
            {
            z=(position[l][m]-ap[k])/4+1;
            //printf("\t\t(%d,%d)|%d",z,k-1,i);
            printf("\t\t\t%+lf",L[z][k-1]);
            }
       
        }
        } //end of k-loop
           
    } //end of flag==1
   
    else
        {printf(" ");}
    tmp1--;
    for(k=0;k<=ROW;k++)
    {
    pos[k]=0;
    match[k]=0;
    }
    }
   
printf("\n");
}
printf("\n----------------------------------------------------------------------------------------------\n");
}

Wednesday, September 4, 2013

Newton's Backward Interpolation

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
void differenceTable(double **A,double **L,int ROW);
double result(double **A,double **L,double x,int ROW);
double factorial(int n);
FILE *fp;
int main(int argc, char *argv[])
{
double **A,*b,*xn,*xnplus1,*p,temp,x,**L,**U,lambda,error,t1=0.0;
int ROW,i,j,k,t=1,m=1,bo=1;
char ch;
if(argc==1)
{
printf("\n Enter the number of values:");
scanf("%d",&ROW);
printf("\n Enter the value at which value has to be aprroximated:");
scanf("%lf",&x);
A=(double **)malloc((ROW+1)*sizeof(double*));
L=(double **)malloc((ROW+1)*sizeof(double*));
U=(double **)malloc((ROW+1)*sizeof(double*));
for(i=0;i<=ROW;i++)
{
A[i]=(double *)malloc((ROW+1)*sizeof(double));
L[i]=(double *)malloc((ROW+1)*sizeof(double));
U[i]=(double *)malloc((ROW+1)*sizeof(double));
}
b=(double *)malloc((ROW+1)*sizeof(double));
p=(double *)malloc((ROW+1)*sizeof(double));
xn=(double *)malloc((ROW+1)*sizeof(double));
xnplus1=(double *)malloc((ROW+1)*sizeof(double));
for(i=1;i<=ROW;i++)
{
printf("\n Enter the value of X[%d]:",i);
scanf("%lf",&A[i][1]);
}
for(i=1;i<=ROW;i++)
{
printf("\n Enter the value of Y[%d]:",i);
scanf("%lf",&A[i][2]);
}

}
else if (argc==2)
{
fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("\n File not found, program will terminate:");
exit(0);
}
fscanf(fp,"%d",&ROW);
fscanf(fp,"%lf",&x);
A=(double **)malloc((ROW+1)*sizeof(double*));
L=(double **)malloc((ROW+1)*sizeof(double*));
U=(double **)malloc((ROW+1)*sizeof(double*));
for(i=0;i<=ROW;i++)
{
A[i]=(double *)malloc((ROW+1)*sizeof(double));
L[i]=(double *)malloc((ROW+1)*sizeof(double));
U[i]=(double *)malloc((ROW+1)*sizeof(double));
}
b=(double *)malloc((ROW+1)*sizeof(double));
p=(double *)malloc((ROW+1)*sizeof(double));
xn=(double *)malloc((ROW+1)*sizeof(double));
xnplus1=(double *)malloc((ROW+1)*sizeof(double));

while(!feof(fp))
{
for(i=1;i<=ROW;i++)
{
for(j=1;j<=2;j++)
{
fscanf(fp,"%lf ",&A[i][j]);
}

}
}
fclose(fp);
}
else
{
printf("\n Invalid Arguments,program will terminate:\n");
exit(0);
}
printf("\n         X          Y");
printf("\n------------------------------------------------------------------\n");
for(i=1;i<=ROW;i++)
{
for(j=1;j<=2;j++)
{

U[i][j]=A[i][j];
printf("   %+lf",U[i][j]);
}
printf("\n");
}
for(i=1;i<=ROW-1;i++)
{
for(j=ROW-i,t=1;j>=1;j--,t++)
{
L[t][i]=U[t+1][2]-U[t][2];
}
for(t=1;t<=ROW-i;t++)
U[t][2]=L[t][i];
}
differenceTable(A,L,ROW);
printf("\n Value at %+lf by Newton's Backward Interpolation formula is %+lf\n",x,result(A,L,x,ROW));
return 0;
}
double result(double **A,double **L,double x,int ROW)
{
int i,j,flag=1,t;

double h,value=0,p,tmp=1.0;
h=A[2][1]-A[1][1];
p=(A[ROW][1]-x)/h;
for(i=0,t=ROW+1;i<=ROW;i++,t--)
{
if(i==0)
value +=A[ROW][2];
else
{
for(j=1;j<=i;j++)
tmp=tmp*(p-j+1)*(-1);
tmp = tmp/factorial(i);
tmp = tmp*L[t-1][i];
value +=tmp;
}
tmp=1.0;
}
return value;
}
double factorial(int n)
{
 if(n<=1)
  return 1;
 else
  return n*factorial(n-1);
}
void differenceTable(double **A,double **L,int ROW)
{
int **position,*ap,j,m,i;
position=(int **)malloc((ROW+1)*sizeof(int*));
ap=(int *)malloc((ROW+1)*sizeof(int));
for(i=0;i<=ROW;i++)
position[i]=(int *)malloc((ROW+1)*sizeof(int));
int an,tmp;
tmp=ROW;
for(i=1;i<=ROW;i++)
{
an=1+(i-1)*2;
ap[i]=an;
for(j=1;j<=tmp;j++)
{
position[i][j]=an+(j-1)*4;
}
tmp--;
}
tmp=ROW;
tmp=ROW+(ROW-1)*3;
int *match,tmp1,l,*pos,flag,z,k;
match=(int *)malloc((ROW+1)*sizeof(int));
pos=(int *)malloc((ROW+1)*sizeof(int));
for(i=0;i<=ROW;i++)
{
match[i]=0;
pos[i]=0;
}

printf("\n-----------------------------Difference Table-----------------------------\n");
printf("\n X       Y");
for(i=1;i<=ROW-1;i++)
printf("      D_%d",i);
printf("\n--------------------------------------------------------------------------\n");
for(i=1;i<=tmp;i++)
{
tmp1=ROW;

for(l=1;l<=ROW;l++)
{
flag=0;
for(m=1;m<=tmp1;m++)
{
if(i==position[l][m])
{
flag=1;
match[l]=1;
pos[m]=position[l][m];
break;
}
} //inner for
if(flag==1)
{
for(k=1;k<=ROW;k++)
{
if(match[k]==0)
{
printf("");
}
else
{
if(k==1)
//printf("(%d,%d)+(%d,%d)|%d",(position[l][m]/4)+1,k,(position[l][m]/4)+1,k+1,i);
printf("%+2.2lf\t%+2.2lf",A[(position[l][m]/4)+1][k],A[(position[l][m]/4)+1][k+1]);
else
{
z=(position[l][m]-ap[k])/4+1;
//printf("\t\t(%d,%d)|%d",z,k-1,i);
printf("\t\t%+2.2lf",L[z][k-1]);
}

}
} //end of k-loop

} //end of flag==1

else
{printf(" ");}
tmp1--;
for(k=0;k<=ROW;k++)
{
pos[k]=0;
match[k]=0;
}
}

printf("\n");
}
printf("\n--------------------------------------------------------------------------\n");
}

Newton's Forward Interpolation : Numerical Analysis C Program

Newton's Forward Interpolation : Numerical Analysis C Program

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
void differenceTable(double **A,double **L,int ROW);
double result(double **A,double **L,double x,int ROW);
double factorial(int n);
FILE *fp;
int main(int argcchar *argv[]) {
double **A,*b,*xn,*xnplus1,*p,temp,x,**L,**U,lambda,error,t1=0.0;
int ROW,i,j,k,t=1,m=1,bo=1;
char ch;
if(argc==1) { 
    printf("\n Enter the number of values:");
    scanf("%d",&ROW);
    printf("\n Enter the value at which value has to be aprroximated:");
    scanf("%lf",&x);
    A=(double **)malloc((ROW+1)*sizeof(double*));
    L=(double **)malloc((ROW+1)*sizeof(double*));
    U=(double **)malloc((ROW+1)*sizeof(double*));
    for(i=0;i<=ROW;i++) {
        A[i]=(double *)malloc((ROW+1)*sizeof(double));
        L[i]=(double *)malloc((ROW+1)*sizeof(double));
        U[i]=(double *)malloc((ROW+1)*sizeof(double));
    }
 b=(double *)malloc((ROW+1)*sizeof(double));
 p=(double *)malloc((ROW+1)*sizeof(double));
 xn=(double *)malloc((ROW+1)*sizeof(double));
 xnplus1=(double *)malloc((ROW+1)*sizeof(double));
 for(i=1;i<=ROW;i++)  {
    printf("\n Enter the value of X[%d]:",i);
    scanf("%lf",&A[i][1]);
 }
 for(i=1;i<=ROW;i++) {
    printf("\n Enter the value of Y[%d]:",i);
    scanf("%lf",&A[i][2]);
 }
  
}
else if (argc==2) {
    fp=fopen(argv[1],"r");
    if(fp==NULL) {
        printf("\n File not found, program will terminate:");
        exit(0);
    }
    fscanf(fp,"%d",&ROW);
    fscanf(fp,"%lf",&x);
    A=(double **)malloc((ROW+1)*sizeof(double*));
    L=(double **)malloc((ROW+1)*sizeof(double*));
    U=(double **)malloc((ROW+1)*sizeof(double*));
    for(i=0;i<=ROW;i++) {
        A[i]=(double *)malloc((ROW+1)*sizeof(double));
        L[i]=(double *)malloc((ROW+1)*sizeof(double));
        U[i]=(double *)malloc((ROW+1)*sizeof(double));
    }
    b=(double *)malloc((ROW+1)*sizeof(double));
    p=(double *)malloc((ROW+1)*sizeof(double));
    xn=(double *)malloc((ROW+1)*sizeof(double));
    xnplus1=(double *)malloc((ROW+1)*sizeof(double));
    while(!feof(fp)) {
    for(i=1;i<=ROW;i++) {
        for(j=1;j<=2;j++) {
            fscanf(fp,"%lf ",&A[i][j]);
            }
        }
    }  
 fclose(fp);
 }
 else {
    printf("\n Invalid Arguments,program will terminate:\n");
    exit(0);
}
printf("\n         X          Y");
printf("\n------------------------------------------------------------------\n");
for(i=1;i<=ROW;i++) {
    for(j=1;j<=2;j++) {
        U[i][j]=A[i][j];
        printf("   %+lf",U[i][j]);
        }
    printf("\n");
}
for(i=1;i<=ROW-1;i++) {
    for(j=ROW-i,t=1;j>=1;j--,t++)  {
        L[t][i]=U[t+1][2]-U[t][2];
        }
 for(t=1;t<=ROW-i;t++)
    U[t][2]=L[t][i];
differenceTable(A,L,ROW);
printf("\n Value at %+lf by Newton's Forward Interpolation formula is %+lf\n",x,result(A,L,x,ROW));
return 0;
}
double result(double **A,double **L,double x,int ROW) {
int i,j;
double h,value=0,p,tmp=1.0;
h=A[2][1]-A[1][1];
p=(x-A[1][1])/h;
for(i=0;i<ROW;i++){
    if(i==0)
    value +=A[1][2];
    else {
        for(j=1;j<=i;j++)
        tmp=tmp*(p-j+1);
        tmp = tmp/factorial(i);
        tmp = tmp*L[1][i];
        value +=tmp;
    }
    if(i==0)
        printf("%2.2lf ",A[1][2]);
    else
        printf("%2.2lf ",L[1][i]);
    tmp=1.0;
    }
    return value;
}
double factorial(int n) {
    if(n<=1)
        return 1;
    else
        return n*factorial(n-1);
}
void differenceTable(double **A,double **L,int ROW) {
    int **position,*ap,j,m,i;
    position=(int **)malloc((ROW+1)*sizeof(int*));
    ap=(int *)malloc((ROW+1)*sizeof(int));
    for(i=0;i<=ROW;i++)
        position[i]=(int *)malloc((ROW+1)*sizeof(int));
    int an,tmp; 
    tmp=ROW;
    for(i=1;i<=ROW;i++) {
        an=1+(i-1)*2;
        ap[i]=an;
        for(j=1;j<=tmp;j++)  {
            position[i][j]=an+(j-1)*4;
            }
        tmp--;
    }
    tmp=ROW;
    tmp=ROW+(ROW-1)*3;
    int *match,tmp1,l,*pos,flag,z,k;
    match=(int *)malloc((ROW+1)*sizeof(int));
    pos=(int *)malloc((ROW+1)*sizeof(int));
    for(i=0;i<=ROW;i++) {
        match[i]=0;
        pos[i]=0;
        }
printf("\n-----------------------------Difference Table-----------------------------\n");
printf("\n X       Y");
for(i=1;i<=ROW-1;i++)
    printf("      D^%d",i);
printf("\n--------------------------------------------------------------------------\n");
for(i=1;i<=tmp;i++) {
    tmp1=ROW;
     for(l=1;l<=ROW;l++) {
        flag=0;
        for(m=1;m<=tmp1;m++)  {
            if(i==position[l][m])   {
                flag=1;
                match[l]=1;
                pos[m]=position[l][m];
                break;
                }
            } //inner for
        if(flag==1)        {
            for(k=1;k<=ROW;k++)  {
                if(match[k]==0) {
                    printf("");
                }
        else  {
            if(k==1)
            //printf("(%d,%d)+(%d,%d)|%d",(position[l][m]/4)+1,k,(position[l][m]/4)+1,k+1,i);
                printf("%+2.2lf\t%+2.2lf",A[(position[l][m]/4)+1][k],A[(position[l][m]/4)+1][k+1]);
            else   {
                z=(position[l][m]-ap[k])/4+1;
                //printf("\t\t(%d,%d)|%d",z,k-1,i);
                printf("\t\t%+2.2lf",L[z][k-1]);
                }
            }
        } //end of k-loop     
    } //end of flag==1
    else
        {printf(" ");}
    tmp1--;
   for(k=0;k<=ROW;k++) {
        pos[k]=0;
        match[k]=0;
        }
    }
    printf("\n");
    }
printf("\n--------------------------------------------------------------------------\n");
}