Wednesday, December 5, 2012

Monday, December 3, 2012

Find the factorial of given number

The trick here is to realize that the limiting case is the number of times five will be a factor of X!, as 5*2 = 10, and any time the number is multiplied by 10, there will be an additional trailing zero. Due to the abundance of even numbers, there are plenty of spare twos that are factors of any number. The trick is that the number of trailing zeros in X! is the number of times five divides into the number, plus the number of times 25 divides into the number, plus the number of times 125 divides into the number, and so forth. (25 is 5*5, so when five is divided into the number, that still leaves a single 5 remaining as a factor.) 

#include <iostream>
using namespace std;

int main()
{
 int factorialnumber = 0;
 cout<<"Please enter a number: ";
 cin>>factorialnumber;
 int zero_count = 0;
 for(int five_factor=5; 
     five_factor<=factorialnumber; 
     five_factor*=5)
 {
  zero_count += factorialnumber/five_factor;
 }
 cout<<"Trailing zeros of "<<factorialnumber;
 cout<<"! is "<<zero_count<<endl;
}
Many other solutions exist! If yours doesn't match the Learn4Develop key, but it still works, let us know and we'll upload it as an alternative answer.

String Permutation programm using c++

First of all i like to say this is a little bit advanced programme for beginners who can't understand the code looking at that.. You try to understand the code than definitely you will write code without plagiarism. :) 

The trick to string permutations is to find all permutations that begin with one letter, then all permutations that begin with a second letter, and so forth. This might suggest a recursive solution: in order to find all permutations that begin with a given letter, call permute on the remainder of the string, thereby finding all permutations of the substring using the same algorithm.

permute string
and output should be a word per line.
Here is a sample for the input cat:
cat
cta
act
atc
tac
tca

#include <iostream>
#include <string>
using namespace std;

string swtch(string topermute, int x, int y)
{
 string newstring = topermute;
 newstring[x] = newstring[y];
 newstring[y] = topermute[x]; //avoids temp variable
 return newstring;
}
void permute(string topermute, int place)
{
 if(place == topermute.length() - 1)
 {
  cout<<topermute<<endl;
 }
 for(int nextchar = place; nextchar < 
     topermute.length(); nextchar++)
 {
  permute(swtch(topermute, place, nextchar),
          place+1);
 }
}

int main(int argc, char* argv[])
{
 if(argc!=2)
 {
  cout<<"Proper input is 'permute string'";
                return 1;
 }
        permute(argv[1], 0);
}
Many other solutions exist! If yours doesn't match the Learn4Develop key, but it still works, let us know and we'll upload it as an alternative answer. For instance, if you have a solution that doesn't use the string library, or if you use only C++ i/o, let us know so that we can upload it as an example for those who haven't learned about certain topics.

Print out the size of the file In c++

In this Programm, given the name of a file, print out the size of the file, in bytes. If no file is given, provide a help string to the user that indicates how to use the program. You might need help with taking parameters via the command line or file I/O in C++

This solution uses some sophisticated features of ifstream, but it can be done by simple counting bytes as well.



/*
 * This program was created by Denis Meyer 
 * Gugzi said,,No Plagiarism :P 
 */
#include <iostream>
#include <fstream>
using namespace std;

int main (int argc, char* const argv[]) {
        if ( argc < 1 )
        {
                cout << endl << "Usage programname filename" << endl << endl;
                return 1;
        }
        else if ( argc != 2 )
        {
                cout << endl << "Usage: " << argv[0] << " filename" << endl << endl;
                return 1;
        }
    
    ifstream file(argv[1]);
    if(!file.is_open()) {
        cout << endl << "Couldn't open File " << argv[1] << endl << endl;
        return 1;
    }
    
    long begin, end;
    
    begin = file.tellg();
    file.seekg (0, ios::end);
    end = file.tellg();
    
    file.close();
    
    cout << endl << "The Size of the File '" << argv[1] << "' is: " << (end-begin) << " Byte." << endl << endl;
    
    return 0;
}

Pascal's Triangle programm using c++

The way to compute any given position's value is to add up the numbers to the position's right and left in the preceding row. For instance, to compute the middle number in the third row, you add 1 and 1; the sides of the triangle are always 1 because you only add the number to the upper left or the upper right (there being no second number on the other side).
The program should prompt the user to input a row and a position in the row. The program should ensure that the input is valid before computing a value for the position.

#include <iostream>
using namespace std;

int compute_pascal(int row, int position)
{
 if(position == 1)
 {
  return 1;
 }
 else if(position == row)
 {
  return 1;
 }
 else
 {
  return compute_pascal(row-1, position) + compute_pascal(row-1, position-1);
 }
}
int main()
{
        int row, position;
        cout<<"Please input a row and a position along the row: ";
        cin>>row>>position;
        if(row<position)
        {
                cout<<"Invalid entry.  Position must be less than or equal to row.";
                return 0;
        }
        cout<<"Value at row "<<row<<" and position " <<position<<" is "<<compute_pascal(row, position);
}

Counts the total number of lines in a file using strcut c++

Here's a simple help  to get you started with struct: write a program that takes a file as an argument and counts the total number of lines. Lines are defined as ending with a newline character. Program usage should be
count filename.txt
and the output should be the line count.
#include <fstream>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
 if(argc!=2)
 {
  cout<<"Input should be of the form 'count filename.txt'";
  return 1;
 }
 else
 {
  ifstream input_file(argv[1]);
  if(!input_file)
  {
   cout<<"File "<<argv[1]<<" does not exist";
   return 0;
  }
  char c;
  int count = 0;
  while(input_file.get(c))
  {
   if(c == '\n')
   {
    count++;
   }
  }
  cout<<"Total lines in file: "<<count;
 }
 return 0;
}
Many other solutions exist! If yours doesn't match the Learn4Develop key, but it still works, let me know and I'll upload it as an alternative answer.

Find whether a given number is perfect or not using c++

This program will find whether a given number is perfect or not. A perfect number is that in which we get the same result if we multiply the digits of the number or we add the digits of the number. For example: 321.
Here 3+2+1=3*2*1=6. It means 321 is a perfect number.

#include<iostream>
using namespace std;
int  main()
{
 int n,r,m=1,s=0;
 cout<<"Enter value of n:";
 cin>>n;
 while(n>0)
 {
  r=n%10;
  n=n/10;
  m=m*r;
  s=s+r;
 }
 if(s==m)
  cout<<"\nNumber is a perfect number.";
 else
  cout<<"\nNumber is not a perfect number.";
 system("pause");
 return 0;
}
Output:
Enter value of n:123 Number is a perfect number.