Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diksha choudhary 2(#021) #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Question 2/Answer/README.md
Original file line number Diff line number Diff line change
@@ -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: [email protected]
79 changes: 79 additions & 0 deletions Question 2/Answer/answer_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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;
}