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.

Prime Numbers Between 1 to 100 using c++

This program will print all the prime numbers between 1 to 100.
all the programs are double checked in this site and they are working in visual studio...

#include<iostream>
using namespace std;
int  main()
{
 int i,j,f;
 cout<<"Prime numbers between 1 to 100 are:\n\n";
 for(i=2;i<=100;i++)
 {
  f=0;
  for(j=2;j<i;j++)
  {
   if(i%j==0)
   {
    f=1;
    break;
   }
  }
  if(f==0)
   cout<<i<<" ";
  }
 system("pause");
 return 0;
}

Find GCD and LCM of two Given Numbers in c++

This program will give you the gcd and lcm of two given numbers using C++ code. The logic used for this particular program is very simple and easy to understand. You need to take 5 integers values and GCD and LCM along with it. 
#include<iostream>
using namespace std;
int  main()
{
 int x,y,a,b,t,gcd,lcm;

 cout<<"Enter value of a and b:\n";
 cin>>a>>b;
 x=a;
 y=b;
 while(b!=0)
 {
  t=b;
  b=a%b;
  a=t;
  gcd=a;
  lcm=(x*y)/gcd;
 }
cout<<"\ngcd is:"<<gcd<<"\nlcm is:"<<lcm;
system("pause");
return 0;
}

Find Leap Year in c++

This program will tell you whether a given year is a leap year or not.

#include<iostream>
using namespace std;

 int  main()
 {
  int n;
  cout<<"Enter a year:";
  cin>>n;
  if(n%400==0)
 cout<<"\nNot a leap year.";
  else if(n%100==0)
   cout<<"\nNot a leap year.";
  else if(n%4==0)
   cout<<"\nIt's a leap year.";
  else
   cout<<"It's not a leap year.";
  system("pause");
  return 0;
 }
Output: Enter a year:2012 It's a leap year.

Printing Patterns Using Asterisks in c++

It is a very common question for both C and C++ programmers that printing a diamond shape using asterisks (*) and this question is going to be ask in almost every freaking semester ;) .There are a lot of different ways to do that. But among them I found this one easier. Those who likes to use C code for printing diamond just change the header #include<iostream> to #include<stdio.h>, cout to printf and cin to scanf.Lets start the fun :)

#include<iostream>

#include<cstdlib>

using namespace std;

int main()

{

int i=0, j=0, NUM=3;

for(i=-NUM; i<=NUM; i++)

{

for(j=-NUM; j<=NUM; j++)

{

if( abs(i)+abs(j)<=NUM) // Change this condition

 { cout<<"*"; }

else { cout<<" ";}

}

cout<<endl;

}
return 0;
}

The above code will prints-
 *
***
*****
*******
*****
***
*
 Wait I haven’t done yet. You can have fun with it. Just change the ‘ if ‘ condition. You will get new design :) . Wanna see???. Lets take a look-



if( abs(i)*abs(j)<= NUM)
prints-
***
***
*******
*******
*******
***
***


if( abs(i)*abs(j)<= NUM)
prints-
*
*  *
*      *
*          *
*      *
*  *
*


if(abs(i)==abs(j)
prints-
*          *
*      *
*  *
*
*  *
*      *
*          *


if( abs(i)==0||abs(j)==0)
prints-
*
*
*
*******
*
*
*


if(abs(i)>=abs(j))
prints-
*******
*****
***
*
***
*****
*******


if(abs(i)<=abs(j))
prints-
*          *
**      **
***  ***
*******
***  ***
**      **
*          *

That’s it. Just change the condition and have fun with it. ENJOY !


Saturday, December 1, 2012

Simple Horizontal Navigation Bar Using CSS


Simple Navigation Bar






today’s tutorial is all about going back to basics. This is what you need to know to build a simple navigation bar like the one pictured above .

The List

As with most modern navigation bars, ours will be based on the unordered list (<ul>) tag. This makes semantic sense, a navigation bar is really nothing but a list of links leading into your site. The traditional horizontal orientation is simply a convenient means to get all of our most important list items “above the fold,” so the user can see them without having to scroll down the page.
So here is our sample HTML:

<ul id="nav">
 <li><a href="#">About Us</a></li>
 <li><a href="#">Our Products</a></li>
 <li><a href="#">FAQs</a></li>
 <li><a href="#">Contact</a></li>
 <li><a href="#">Login</a></li>
</ul>

That’s really all it takes! You’ll notice I did use one ID, so we could tell our navigation bar apart from all the other unordered lists on the page. But if this were tucked into a div with its own ID (like a “banner” or “header” div), the ID probably wouldn’t be necessary. And yes, I could add even more IDs and classes if I wanted to make this fancier, but we’re all about simplicity today.

Making It Horizontal

By default, our list is vertical. So let’s make it horizontal:

#nav {
 width: 100%;
 float: left;
 margin: 0 0 3em 0;
 padding: 0;
 list-style: none; }
#nav li {
 float: left; }
 
Here we’re floating both our list and our list items left. Floating the list items left is what pulls them into a nice horizontal row for us, stacking the items from left to right. However, because of how floats behave, our containing list will collapse to a height of zero unless it is also floated left.
And that wouldn’t be a major problem, except I’m planning to give my list a background color later that I want to show up behind my list items, and if my list collapses, that won’t happen. That’s also why I’m giving my list a width of 100%: That way, it’ll fill up the entire width of the page (or of its container, if it’s in a container with a width set).
I’m also removing most of the margins and padding to make the list behave itself (I’m leaving some margin on the bottom, simply for aesthetic purposes), and setting the list-style to “none,” which removes the bullets from my list.
At this point, our navigation bar looks something like this:



Certainly nothing stylish (and probably difficult to use, to boot), but believe it or not, most of our heavy lifting is now done! From this basic framework, you could construct any number of unique navigation bars. But let’s style ours a bit.
First, we’ll give our navigation bar a background and some borders by updating our #nav CSS to this:

#nav {
 width: 100%;
 float: left;
 margin: 0 0 3em 0;
 padding: 0;
 list-style: none;
 background-color: #f2f2f2;
 border-bottom: 1px solid #ccc; 
 border-top: 1px solid #ccc; } 
 
 And let’s give our anchor tags a bit of breathing room and style, too:
 
#nav li a {
  display: block;
  padding: 8px 15px;
  text-decoration: none;
  font-weight: bold;
  color: #069;
  border-right: 1px solid #ccc; }  


Here, I’m giving the anchors a display of “block” to make sure they fill up the entire list item and make the whole area clickable. Then I’m adding some padding to space them out a bit. I’m also removing the underline, making the font bold, setting our color to a nice blue, and adding a border to the right-hand side of the item, which matches the border we added to the top and bottom of our unordered list.
And finally, let’s give the navigation items a different color when our users mouse over:

#nav li a:hover {
  color: #c00;
  background-color: #fff; }

 And just like that, we have a perfectly functional, useable, and useful navigation bar.

 And here’s all the CSS in one location:

#nav {
 width: 100%;
 float: left;
 margin: 0 0 3em 0;
 padding: 0;
 list-style: none;
 background-color: #f2f2f2;
 border-bottom: 1px solid #ccc; 
 border-top: 1px solid #ccc; }
#nav li {
 float: left; }
#nav li a {
 display: block;
 padding: 8px 15px;
 text-decoration: none;
 font-weight: bold;
 color: #069;
 border-right: 1px solid #ccc; }
#nav li a:hover {
 color: #c00;
 background-color: #fff; }