Generating digit numbers of equally total digits : For « Language « C++






Generating digit numbers of equally total digits

       
Code submitted by Kean Peng Tan (kptan1980 at gmail.com)

       
//generate number version I
//this program is generating  digit number
//numbers of equally total digits
//which means digit1+digit2+digit3+digit4
//is equally same shall be generated

//read and write a text file
#include <iostream>
#include <fstream>

#include <iostream>

using namespace std;
main()
{
  //reading a file
    ofstream out("c:\\generate.txt"); // output, normal file
   
  if(!out) {
    cout << "Cannot open test file.\n";
    return 1;
  }
  
 //variables
 int input=25; //modified here
 int temp[3];
 temp[0]=0;
 temp[1]=0;
 temp[2]=0;
 temp[3]=0;

 //repetitive loop
 int total=0;
 int sum=0; // initial sum  
 int row=0; // initial row 

 int p1,p2,p3,p4;
 p1=temp[0]; //MSB
 p2=temp[1]; 
 p3=temp[2];
 p4=temp[3];
 
for(p1=0;p1<10;p1++){
    if(p2==10){
      p2=0;}
 for(p2=0;p2<10;p2++){
    if(p3==10){
      p3=0;}
  for(p3=0;p3<10;p3++){
    if(p4==10){
      p4=0;}
   for(p4=0;p4<10;p4++)
   {
   //4th digit  
  //determine loop
  sum=p1+p2+p3+p4;
             if(sum==input)
             {
                 out << p1 << p2 << p3 << p4 << " "; 
                 total++;
                 row=total;
                 if(row%10==0)
                 {
                   out << endl;
                              }
             }   //end sum if
                 
      } //end inner loop p4
    }  //end inner loop p3 
   }  //end inner loop p2
  }  //end inner loop p1 
  
  out << "The sum of outputs is equal to " << input << endl;
  out << "Total: " << total << endl; // display in file
  out.close(); 
  cout << "Total: " << total << endl; 
  cout << "success!" << endl;
        
 system("pause");
 return 0;
}// end main

           
       








Related examples in the same category

1.For loop with int value type as the controlFor loop with int value type as the control
2.For statementFor statement
3.for loop: A Multiplication Tablefor loop: A Multiplication Table