Skip to content

Instantly share code, notes, and snippets.

@Zulqurnain
Created September 18, 2013 15:19
Show Gist options
  • Save Zulqurnain/6610725 to your computer and use it in GitHub Desktop.
Save Zulqurnain/6610725 to your computer and use it in GitHub Desktop.
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.
/*Note : Program can take input from binary File*/
#include <iostream>
#include <fstream>
#include <conio.h>
#include <stdio.h>
using namespace std;
int main(){ // jUST fOR fUN Zulqurnain jutt Here
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment