Showing posts with label if statement. Show all posts
Showing posts with label if statement. Show all posts

Friday, July 24, 2015

C++ Program to Solve Tower-of-Hanoi Problem using Recursion

Leave a Comment
This C++ Program uses recursive function & solves the tower of hanoi. The tower of hanoi is a mathematical puzzle. It consists of threerods, and a number of disks of different sizes which can slideonto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top. We have to obtain the same stack on the third rod.

Here is the source code of the C program for solving towers of hanoi.

Code:

#include<iostream>
using namespace std;

void towers(int, char, char, char);

int main()
{
    int num;

    cout<<"Enter the number of disks : ";
    cin>>num;
    cout<<"The sequence of moves involved in the Tower of Hanoi are :\n";
    towers(num, 'A', 'C', 'B');
    return 0;
}

void towers(int num, char frompeg, char topeg, char auxpeg)
{
    if (num == 1)
    {
        cout<<"\n Move disk 1 from peg"<<frompeg<<"to peg "<< topeg;
        return;
    }
    towers(num - 1, frompeg, auxpeg, topeg);
    cout<<"\n Move disk "<<num<<"from peg"<<frompeg<<" to peg "<<topeg;
    towers(num - 1, auxpeg, topeg, frompeg);

}

Output:


Read More

Thursday, February 12, 2015

Life, the Universe, and Everything

Leave a Comment
Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

Example

Input:
1
2
88
42
99

Output:
1
2
88 
 

Solution

#include<iostream>
using namespace std;
typedef struct list{
    int number;
    list *next;
}list;
//prototypes
list* insert(int,list*);
void print(list*);

int main()
{
    list *num=NULL;
    int number;
    while(1)
    {
        cin>>number;
        if(number==42)
            break;
        else
        num=insert(number,num);
    }
    print(num);
return 0;
}

//Definations
list* insert(int numb,list *num)
{
    list *ptr;
    ptr=new list;
    ptr->number=numb;
    ptr->next=NULL;

    if(num==NULL)
    {
        num=ptr;
    }
    else
    {
        list *temp;
        temp=num;
        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        temp->next=ptr;
    }
    return num;
}
void print(list *num)
{
    list *temp;
    temp=num;
    while(temp!=NULL)
    {
        cout<<temp->number<<endl;
        temp=temp->next;
    }
}




Read More

Thursday, February 5, 2015

Commission Program using nested if

Leave a Comment
/*
Using the following chart, write an if/else if statement that assigns
.10, .15, or .20 to commission, depending on the value in sales.
Sales Commission Rate
Up to $10,000 10%
$10,000 to $15,000 15%
Over $15,000 20%
*/
#include<iostream>
using namespace std;
void main()
{
int sales =0;
cout<<"Enter Sales = ";
cin>>sales;
if(sales<10000)
{
cout<<"Commission Rate is == 10%"<<endl;
}
else if(sales>=10000||sales<15000)
{
cout<<"Commission Rate is == 15%"<<endl;
}
else
{
cout<<"Commission Rate is == 20"<<endl;
}
}
Read More

write an if/else statement that assigns 0 to x when y is equal to 10. Otherwise it should assign 1 to x.

Leave a Comment
/*
write an if/else statement that assigns 0 to x when y is equal to 10. Otherwise it should assign 1 to x.
*/
#include<iostream>
using namespace std;
void main()
{
    int x=0,y=0;
    cout<<"Enter the value of x = ";
    cin>>x;
    cout<<"Enter the value of y = ";
    cin>>y;
    if(y==10)
    {
        x=0;
    }
    else{
    x=1;
    }
    cout<<"The value of x = "<<x<<endl;
    cout<<"The value of y = "<<y<<endl;
}
Read More

Write an if statement that assigns 100 to x when y is equal to 0.

Leave a Comment
/*
Write an if statement that assigns 100 to x when y is equal to 0.
*/


Code :

#include<iostream>
using namespace std;
void main()
{
    int x=0,y=0;
    cout<<"Enter the value of x = ";
    cin>>x;
    cout<<"Enter the value of y = ";
    cin>>y;
    if(y==0)
    {
        x=100;
    }
    cout<<"The value of x = "<<x<<endl;
    cout<<"The value of y = "<<y<<endl;
}
Read More