Showing posts with label C Plus Plus. Show all posts
Showing posts with label C Plus Plus. Show all posts

The 3n + 1 problem

Sample Problems

Consider the following algorithm to generate a sequence of numbers. Start with an integer n. If n is even, divide by 2. If n is odd, multiply by 3 and add 1. Repeat this process with the new value of n, terminating when n = 1. For example, the following sequence of numbers will be generated for n = 22:

22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured (but not yet proven) that this algorithm will terminate at n = 1 for every integer n. Still, the conjecture holds for all integers up to at least 1, 000, 000.

For an input n, the cycle-length of n is the number of numbers generated up to and including the 1. In the example above, the cycle length of 22 is 16. Given any two numbers i and j, you are to determine the maximum cycle length over all numbers between i and j, including both endpoints.

Input

The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

Output

For each pair of input integers i and j, output i, j in the same order in which they appeared in the input and then the maximum cycle length for integers between and including i and j. These three numbers should be separated by one space, with all three numbers on one line and with one line of output for each line of input.

Sample Input

1 10

100 200

201 210

900 1000

Sample Output

1 10 20

100 200 125

201 210 89

900 1000 174

Sample C++ Program

Problem: WERTYU










A common typing error is to place the hands on the keyboard one row to the right of the correct position. So "Q" is typed as "W" and "J" is typed as "K" and so on. You are to decode a message typed in this manner.

Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except Q, A, Z), or punctuation shown above [except back-quote (`)]. Keys labelled with words [Tab, BackSp, Control, etc.] are not represented in the input. You are to replace each letter or punction symbol by the one immediately to its left on the QWERTY keyboard shown above. Spaces in the input should be echoed in the output.

Sample Input


O S, GOMR YPFSU/

Output for Sample Input

I AM FINE TODAY.

want more problems.....!? click here!
click here!

Binary to decimal

Another Solution for Binary to Decimal:


SOURCE CODE:


#include stdio(.)h
#include conio(.)h

void showbits(int h)
{
if(h==1)
printf("%d",h);
else
{
showbits(h/2);
printf("%d",h%2);
}
}
main()
{
int num;
void showbits(int h);

printf("BINARY : ");
scanf("%d",&num);
printf("\nBinary of %d equivalent to decimal is ",num);
showbits(num);
getch();
}

Another C Plus Plus

Another Sample Code:


#include
#include
#include

main()
{
int i,n1,t,w,a[100],i1=0;
int n,f2;


printf("[b]PROGRAM FOR DECIMAL TO BINARY CONVERSION[/b]\n");
printf("******************************************\n");
printf("\n\nENTER A NUMBER:");
scanf("%d",&n);
w=n;
f2=w;

if(f2==n)
{
printf("");f2=0;
}
else
{
f2=n-f2;
}
n1=n;

for(i=0;n1>=1;i++)
{
t=n1%2;
n1=n1/2;
i1++;
a[i]=t;
}

printf("\n\nBINARY EQVALENT=>");

for(i=(i1-1);i>=0;i--)
{
printf("%d",a[i]);
}
printf(".");

for(i=1;i<=6;i++)
{
f2=f2*2;
if(f2>=1)
{
printf("1");
f2=f2-1;
}
else
{ printf("0");
}
}
getch();
}

Decimal to Binary

SOURCE CODE:

#include iostream
#include conio (.) h

using namespace std;


int bintodec(int decimal)
{
int total = 0;
int power = 1;

while(decimal > 0)
{
total += decimal % 10 * power;
decimal = decimal / 10;
power = power * 2;
}
return total;
}

main()
{
int num;

cout << "Enter decimal: "; cin >> num;
cout <<>

getch();
}

Binary To Decimal

Computers work on the principle of number manipulation. Inside the computer, the numbers are represented in bits and bytes. For example, the number three is represented by a byte with bits 0 & 1 set; 00000011. This is numbering system using base 2. People commonly use a decimal or Base 10 numbering system. What this means is that in Base 10, count from 0 to 9 before adding another digit. The number 22 in Base 10 means we have 2 sets of 10's and 2 sets of 1's.

Base 2 is also known as binary since there can only be two values for a specific digit; either a 0 = OFF or a 1 = ON. You cannot have a number represented as 22 in binary notation. The decimal number 22 is represented in binary as 00010110 which by following the below chart breaks down to:

Bit Position
7
6
5
4
3
2
1
0

1 1 1 1 1 1 1 1
Decimal 128 64 32 16 8 4 2 1

22 or 00010110:

All numbers representing 0 are not counted, 128, 64, 32, 8, 1 because 0 represents OFF

However, numbers representing 1 are counted, 16 + 4 + 2 = 22 because 1 represents ON

Decimal Values and Binary Equivalents chart:

DECIMAL
BINARY
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
16 10000
32 100000
64 1000000
100 1100100
256 100000000
512 1000000000
1000 1111110100
1024 10000000000



SOURCE CODE:

#include iostream
#include conio (.) h
using namespace::std;

void binary(int decimal) {
int remainder;

if(decimal <= 1) { cout << remainder =" decimal%2;">> 1);
cout <<>> num;
cout << endl << "Binary of " << num << " is ";
binary(num);

getch();
}





Example output:

Enter decimal: 39

Binary of 39 is 100111



HINT:

This just keeps right shifting ( >> ) the number by one bit and printing the binary value of the last bit which is derived using %.

Array

Source Code:

#include(<)iostream(>)
#include(<)conio(.)h(>)
#include(<)stdlib(.)h>

using namespace std;

main(){
int num[5]={0};

for(int x=0;x<5;x++){>>num[x];
}
while(1){
cout<<"\n\n1. Change Array Values"; cout<<"\n2.Exit"; int c, index, nval; cout<<"\nChoice: "; cin>>c;

if(c==1){
cout<<"Enter Index: "; cin>>index;
cout<<"Enter new Value: "; cin>>nval;
for(int x=0;x<5;x++){ index="="x){" return="" sample="">

Calculator

Sample Output:


Source code:

#include
#include
#include


void add(int x, int y, int z);
void sub(int x, int y, int z);
void pro(int x, int y, int z);
void div(float x, float y, float z);


main()
{

system ("cls");



int a, b, c;

char choice;

printf("Menu:\n");
printf("[+]Add\n");
printf("[-]Subtract\n");
printf("[*]Multiply\n");
printf("[/]Divide\n");
printf("[e]Exit\n");
printf("Choice:");
scanf("%s",&choice);


if(choice=='e')
{
return 0;

}



if(choice=='+')
{

printf("\n\nEnter Num1:");
scanf("%d",&a);
printf("Enter Num2:");
scanf("%d",&b);
printf("Enter Num3:");
scanf("%d",&c);

add(a, b, c);

char doll;
printf("\n\nReturn to Menu? Y or y for yes, N or n to exit prog
ram: ");
scanf("%s",&doll);



if((doll=='Y')||(doll=='y'))
{
return main();
}

else return 0;
}


if(choice=='-')
{

printf("\n\nEnter Num1:");
scanf("%d",&a);
printf("Enter Num2:");
scanf("%d",&b);
printf("Enter Num3:");
scanf("%d",&c);
sub(a, b, c);

char doll;
printf("\n\nReturn to Menu? Y or y for yes, N or n to exit program: ");
scanf("%s",&doll);


if((doll=='Y')||(doll=='y'))
{
return main();

}

else return 0;

}
if(choice=='*')
{
printf("\n\nEnter Num1:");
scanf("%d",&a);
printf("Enter Num2:");
scanf("%d",&b);
printf("Enter Num3:");

scanf("%d",&c);
pro(a, b, c);


char doll;
printf("\n\nReturn to Menu? Y or y for yes, N or n to exit program: ");
scanf("%s",&doll);


if((doll=='Y')||(doll=='y'))
{

return main();
}


else return 0;
}

if(choice=='/')
{
printf("\n\nEnter Num1:");

scanf("%d",&a);
printf("Enter Num2:");
scanf("%d",&b);
printf("Enter Num3:");

scanf("%d",&c);
div(a, b, c);

char doll;
printf("\n\nReturn to Menu? Y or y for yes, N or n to exit program: ");
scanf("%s",&doll);


if((doll=='Y')||(doll=='y'))
{

return main();
}

else return 0;
}

else
return 0;




getch();
}


void add(int x, int y, int z)
{
int sum=0;
sum=x+y+z;
printf("\n%d+%d+%d is equal to %d",x, y, z, sum);
}

void sub(int x, int y, int z)
{
int sub1=0;
sub1=x-y-z;
printf("\n%d-%d-%d is equal to %d",x, y, z, sub1);
}
void pro(int x, int y, int z)
{

int pro1=1;
pro1=x*y*z;
printf("\n%d*%d*%d is equal to %d",x, y, z, pro1);

}
void div(float x, float y, float z)
{
float div=1, div2=0;
div=x/y;
div2=div/z;
printf("\n%.0f/%.0f/%.0f is equal to %.2f",x, y, z, div2);

}

C Plus Plus (Area and Parimeter)

#include
#include

using namespace std;

main(){
int length, width, parimeter, area;

cout<<"Length = "; cin>>length;
cout<<"Width = "; cin>>width;

parimeter = 2 * (length-width);
area = length*width;

cout<<<"Parimeter is "<
cout<<<"Area is "<

getch();

}

C Plus Plus Pointers

#include stdio(dot)h
#include conio
(dot)h

using namespace std;

void swapInteger(int *x, int *y){
int temp=*x;
*x=*y;
*y=temp;


}

main(){
int a, b;

cout<<"x: "; cin>>a;
cout<<"y: "; cin>>b;
swapInteger(&a, &b);
cout<<"X = "<<
cout<<"Y = "<

getch();
}

Pointers

#include

using namespace std;

main(){
int a=8;
int *ptr;

a=8;
ptr=&a;

cout<<"&a : "<<&a<
cout<<"ptr : "<<<

cout<<"a : "<<&a<
cout<<"*ptr : "<<*ptr<<


cout<<"&*ptr : "<<&*ptr<
cout<<"*&ptr : "<<*&ptr<<

system("pause");
}