diff --git a/Question 2/Answer/README.md b/Question 2/Answer/README.md new file mode 100644 index 0000000..87abb04 --- /dev/null +++ b/Question 2/Answer/README.md @@ -0,0 +1,18 @@ +**Question 2 - Solution** +**Execution Instructions** + + Compiler Details : gcc + Type down the command in the linux terminal answer_2.c -o ob + The above mentioned command will generate an object file which can be run directly on the terminal using ./ob + +**Input & Output** + + Input + A file named "1Kdigits.txt" containing the digits + Output + The highest product is 23514624000.000000 + +**Author** + + Diksha Choudhary + Email: diksha2.choudhary83@gmail.com diff --git a/Question 2/Answer/answer_2.c b/Question 2/Answer/answer_2.c new file mode 100644 index 0000000..aa41066 --- /dev/null +++ b/Question 2/Answer/answer_2.c @@ -0,0 +1,79 @@ +#include +#include +#include +int main() +{ + char ch[1000]; + FILE *fptr; //pointer to the file + int i,j,n,temp,pos; + float prod=1,max=1; + fptr=fopen("1Kdigits.txt","r"); //stored the digits in a file + if(fptr==NULL) //checking if the file has openend successfully or not + printf("Unable to open file"); + else + { + fgets(ch,1000,fptr); + j=0; + i=0; + while(i<13) + { + n=ch[j]-'0'; + if(n==0) + { + prod=1; + i=0; + } + else + { + prod=prod*n; + i=i+1; + } +j=j+1; + } + pos=j-1; + i=13; + max=prod; + while(j<1000) + { + n=ch[j]-'0'; + if(n==0) + { + if (i>=13&&prod>max) + { + max=prod; + pos=j; + } + prod=1; + i=0; + } + else + { + if(i<13) + { + prod=prod*n; + i=i+1; + } + else + { + if(prod>max) + { + max=prod; + pos=j; + } + temp=ch[j-13]-'0'; + prod=prod/temp; + prod=prod*n; + } + + } + j=j+1; + } + if(prod>max) + { + max=prod; + pos=j; + } +} + printf("The highest product is %f",max); + return 0; +}