Hacking Equipments | C/C++ Coding | Updates:: Did you tried Our Online ? AdobePhotoshop |

Goto Top

[ Problem ] Calculating Digital Root Input from Binary File

Problem :


Given a well-formed (non-empty, fully valid) string of digits, let the integer N be the sum of digits. Then, given this integer N, turn it into a string of digits. Repeat this process until you only have one digit left. Simple, clean, and easy: focus on writing this as cleanly as possible in your preferred programming language.

------------------------------------------------------------------

Input Description

------------------------------------------------------------------

As a crude form of hashing function, Lars wants to sum the digits of a number. Then he wants to sum the digits of the result, and repeat until he have only one digit left. He learnt that this is called the digital root of a number, but the Wikipedia article is just confusing him.

Can you help him implement this problem in your favourite programming language?

It is possible to treat the number as a string and work with each character at a time. This is pretty slow on big numbers, though, so Lars wants you to at least try solving it with only integer calculations (the modulo operator may prove to be useful!).

------------------------------------------------------------------

Formal Inputs & Outputs
Input Description

A positive integer, possibly 0.
Output Description

An integer between 0 and 9, the digital root of the input number.

Sample Inputs & Outputs



Sample Input

31337

Sample Output

8, because 3+1+3+3+7=17 and 1+7=8
Challenge Input

1073741824

Challenge Input Solution

?
Note :
GIve the user option to either input the number as well or read input from a binary file in binary mode.


Solution :


Code :

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

using namespace std;

int main(void){  
	long int n,m;
	int c=0,a=0;
	printf("Take Input from binary File (y/n) :=:");
	char ch=getch();
	while(1){
		if(ch=='Y'||ch=='y'){
			char r[25];
			cout<<"\n\n\nEnter The Name of the File :=:"; cin>>r;
			printf("\n\n Write New Value or Read Previous ? (W/R) :=:"); 
			char chi=getch();
			if(chi=='W'||chi=='w'){
				int num;
				cout<<"\n\nEnter The Number to store :=: "; cin>>num;
				ofstream of(r,ios::binary|ios::out);
				of.write((char*)&num,sizeof(&num)); // writing
				of.close();
			}
			ifstream in(r,ios::binary|ios::in);
			if(!in){
				cout<<"\n\n\t\t::Wrong Input of file::\n";
			}
			else{
				in.read((char*)&n,sizeof(&n)); // then reading what is written :p
			}
			in.close();
			break;
		}
		else if(ch=='N'||ch=='n'){

			cout<<"\n\n\nEnter Your Number :=:"; cin>>n; cout<<"\n\n";
			break;
		}
		else{
			continue;
		}
	}
	if(n<=9){
		cout<<"Invalid Entry Kindly Number Should be greater than 9\n\n";
		exit(1);
	}
	m = n;
	while(m){
		c+=(m%10);
		m/=10;
	}
	m = c;
	while(m){
		a+=(m%10);
		m/=10;
	}
	cout<<"\n\n\nHere is the Digital Root of Num :"<<n<<": :=:"<<a<<"\n\n";
	return 0;
}


Logic & Implementation :


  • Used Concept of Binary File I/O , in.read((char *)& (Here is Your Variable/identifier) ,  sizeof(& (Here is Your Variable/identifier) )) . same with in.write , it will convert your each bit( character  ) in binary while storing .
  • Digital root is already explained.
  • Use printf() when you are using getch() or getche() , printf() is in stdio.h and getch() and getche() is in conio.h 
  • When Stream directories Like fstream or iostream will be used Do not use as .h at end in c++ Use using namespace std; as i've used .
  • Keep In Mind { Everything inside this will be destroyed after it complete execution called Scope } .





0 Comments :

Post a Comment

Having Confusion ,oH Dear ask me in comments!!

Related Posts Plugin for WordPress, Blogger...
 

About Admin

I am a pro-programmer of C++ ,php i can crack some softwares and am a web desighner .I AM also on .


| Solve Byte © 2011 - 2016. All Rights Reserved | Back To Top |