Sunday, 18 December 2016

Mind Reading (Magic Trick)

Mind Reading (Magic Trick)

Soucre Code:

#include<iostream>
#include<stdlib.h>
using namespace std;
int a[7]={1,2,3,4,5,6,7};
int b[7]={8,9,10,11,12,13,14};
int c[7]={15,16,17,18,19,20,21};
int array[21];
int p=0,q=3;
int operation();
int storage1();
int storage2();
int storage3();
int display();
int menu();
int reinsertion();
char ab,temp;
int main()
{
menu();
for(int i=0;i<3;i++)
{
display();
operation();
cout <<"\033[2J \033[0;0f again enter your choice\n";//clear screen code and positions of cursor
}
cout << "\033[2J  \033[1;1f";
cout<<"your number is="<<array[10];
}
int menu()
{
cout<<"think any number between 1 to 21 in your mind and press enter";
//getch();
getchar();
cout<<"Now\n";
}
int display()
{
cout<<"Row 1 ->";
for(int i=0;i<7;i++)
cout<<a[i]<<",";
cout<<endl;
cout<<"Row 2 ->";
for(int i=0;i<7;i++)
cout<<b[i]<<",";
cout<<endl;
cout<<"Row 3 ->";
for(int i=0;i<7;i++)
cout<<c[i]<<",";
cout<<endl;
}
int operation()
{
cout<<"\n->enter Row number in which your number is present=";
cin>>ab;
switch(ab)
{
case'1':
p=2;q=3;
storage1();
break;
case'2':
p=2;q=3;
storage2();
break;
case'3':
p=2;q=3;
storage3();
break;
default:
if(p<2)
{
cout<<"plz enter valid choice and try again\n";
p++;
operation();
}
else
if(p<4)
{
cout<<"remaing try ="<<q-1;
cout<<"\nplz enter valid choice and try again\n";
p++;
q--;
operation();
}
else
if(q==1)
{
cout<<"final try";
cout<<"\nplz enter valid choice and try again\n";
q--;
operation();
}
else
if(q==0)
{
cout<<"you do so many wrong try \n program is \nstop...";
exit(0);
}
}
}
int storage1()
{
int m=0,n=0;
for(int i=0;i<7;i++)
{
array[i]=b[i];
}
for(int i=7;i<14;i++)
{
array[i]=a[m];
m++;
}
for(int i=14;i<21;i++)
{
array[i]=c[n];
n++;
}
reinsertion();
}
int storage2()
{
int m=0,n=0;
for(int i=0;i<7;i++)
{
array[i]=a[i];
}
for(int i=7;i<14;i++)
{
array[i]=b[m];
m++;
}
for(int i=14;i<21;i++)
{
array[i]=c[n];
n++;
}
reinsertion();
}
int storage3()
{
int m=0,n=0;
for(int i=0;i<7;i++)
{
array[i]=a[i];
}
for(int i=7;i<14;i++)
{
array[i]=c[m];
m++;
}
for(int i=14;i<21;i++)
{
array[i]=b[n];
n++;
}
reinsertion();
}
int reinsertion()
{
int m=0;
for(int i=0;i<21;i++)
{
a[m]=array[i];
i++;
b[m]=array[i];
i++;
c[m]=array[i];
m++;
}

}

Monday, 14 November 2016

Clockwise block breaker

Problem : Clockwise Bricks Breaker

Clockwise Bricks Breaker is a game where there is a ball which is used to break the block of bricks. Brick's Blocks are always in a form of a square matrix( e.g 2X2, 4X4). The ball has some characteristics like for breaking each brick from the block. It consumes 1 unit of energy and the whole setup is in a manner that the ball will break bricks always - in clock wise direction, in a ring fashion and start from top-left. Take an example of 4X4 brick block. 
 
        Figure 1. 


 
        Figure 2. 


 
        Figure 3. 
The numbers on the bricks are nothing but points gained by breaking that brick which is assigned row wise starting from 1 to NxN. The game gets over when energy of ball exhausts and once ball initiated with some energy it will only stop when all its energy exhaust. 

Your Task is to calculate total points (points of a block should be the positional value of that block as mentioned in the picture: You have to add points of all the broken bricks) gained by a player, where the energy of ball and size of block will be given. 


Input Format: 

First line of input should be the number of test cases T

Next T lines contain two variables N and E delimited by a whitespace where, 
  • N =size of square block (i.e. block will be of NXN bricks)
  • E =Energy of ball



Output Format: 

Output will be T lines containing the integer which is total number points gained by that energy. 
OR 

Print "Invalid Input" if constraint fails 
Constraints:
  • 0< T<=10
  • 0< N <=200
  • 0<= E <=N*N


Examp
EXAMPLE NUMBERSAMPLE INPUTSAMPLE OUTPUTEXPLAINATION
12
4 5
1 50

36
Invalid Input
For first test case as with Energy 5, the ball will break 5 bricks having points 1,4,16,13,2

So 1+4+16+13+2=36.
Second test case is Invalid Input because constraint E<=N*N not satisfied.
22
5 0
9 -3

0
Invalid Input
For first test case it is 0 as energy is zero.

For second test case it is Invalid Input because constraint E >= 0 not satisfied.

SOLUTION :

#include<iostream>
using namespace std;
int main()
{
int N,P;
cout<<"Enter N & Power :";
cin>>N>>P;
if(P>N*N)
P=N*N;
int wall[N][N],temp=0;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
temp++,wall[i][j]=temp;
int sum=0,
    i[4]={0,0,N-1,N-1},
    j[4]={0,N-1,N-1,0};
    temp=0;
while(P>0)
{
if(temp%4==0)
{
if(wall[i[0]][j[0]]==0)
{
i[0]+=1;
j[0]=i[0];
sum+=wall[i[0]][j[0]];
wall[i[0]][j[0]]=0;
j[0]+=1;
}
else
{
sum+=wall[i[0]][j[0]];
wall[i[0]][j[0]]=0;
j[0]+=1;
}
}
if(temp%4==1)
{
if(wall[i[1]][j[1]]==0)
{
j[1]-=1;
i[1]=i[0];
sum+=wall[i[1]][j[1]];
wall[i[1]][j[1]]=0;
i[1]+=1;
}
else
{
sum+=wall[i[1]][j[1]];
wall[i[1]][j[1]]=0;
i[1]+=1;
}
}
if(temp%4==2)
{
if(wall[i[2]][j[2]]==0)
{
i[2]-=1;
j[2]=i[2];
sum+=wall[i[2]][j[2]];
wall[i[2]][j[2]]=0;
j[2]-=1;
}
else
{
sum+=wall[i[2]][j[2]];
wall[i[2]][j[2]]=0;
j[2]-=1;
}
}
if(temp%4==3)
{
if(wall[i[3]][j[3]]==0)
{
j[3]+=1;
i[3]=i[2];
sum+=wall[i[3]][j[3]];
wall[i[3]][j[3]]=0;
i[3]-=1;
}
else
{
sum+=wall[i[3]][j[3]];
wall[i[3]][j[3]]=0;
i[3]-=1;
}
}
temp+=1;
P--;
}
cout<<"sum="<<sum;
}

Sunday, 23 October 2016

Datatype in C


Data type use to Store different type of data like any text data 
so in C use datatype to store data
but every datatype have a special type of storage 


first, see the type then we get more about datatype:-

1.character
2.Radix Point / Floating type
3 Integer non Radix point

TypeStorage sizeValue range
char1 byte-128 to 127 or 0 to 255
unsigned char1 byte0 to 255
signed char1 byte-128 to 127
int2 or 4 bytes-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295
short2 bytes-32,768 to 32,767
unsigned short2 bytes0 to 65,535
long4 bytes-2,147,483,648 to 2,147,483,647
unsigned long4 bytes0 to 4,294,967,295

Floating type:-

TypeStorage sizeValue rangePrecision
float4 byte1.2E-38 to 3.4E+386 decimal places
double8 byte2.3E-308 to 1.7E+30815 decimal places
long double10 byte3.4E-4932 to 1.1E+493219 decimal places

so 1st datatype is 
char it store only one character in it 
eg.,
#include<stdio.h>
int main()
{
//here char define type but it variable is  var

char var;

//this is a comment it doesn't affect the code. single-line comment is given by '//' and multi-line bye '/*' "*/" 

// single line comment 

/* multi line 
comment*/




var='A';
//now var have value of 'A'

printf("value of var=%c",var);

//show output A

 similar int store number  without a decimal point 
there are so many types like short, long, unsigned
 we talk about it in the next post

Thursday, 20 October 2016

Problem : Cats And Dogs


                                   Problem : Cats And Dogs



Cats & Dogs have become friends now. They are going to a picnic and they decided to sit together in groups to have their lunch.

There are N Cats & M Dogs. They can form any number of groups to sit together, subject to rules below. 

Rules:
  1. They will sit in a straight line
  2. A Cat cannot sit beside another Cat or alone
  3. A Dog cannot sit beside another Dog or alone


You need to find minimum number of groups they need to form. If no group can be formed then print -1.

Input Format:

First line of input will be a number of Test Cases T

Next T lines of input will contains two numbers N and M, delimited by space
Output Format:

Print minimum number of groups to be formed for each Test Case in new line
OR

Print -1 if no group can be formed
Constraints:
  1. 0<T<=10^6
  2. 0<N,M<=10^18

Example:

EXAMPLE NUMBERSAMPLE INPUTSAMPLE OUTPUT
1
2
5 5
1 3

1
-1
2
3
7 8
3 5
6 7

1
2
1

Solution:-
#include<iostream>
using namespace std; 
int main()
{
int T,N,M;
cout<<"Enter No of Test case: ";
cin>>T;
for(int i=0;i<T;i++)
{
cout<<"\nEnter cat and dog :";
cin>>N>>M;
if(M/2>N||N/2>M||M<1||N<1)
cout<<"Answer : -1";
else if(M==N)
cout<<"Answer : 1";
else
{
int temp;
if(M>N)
{
temp=M-N-1;
cout<<"Answer : "<<temp+1;
}
else
{
temp=N-M-1;
cout<<"Answer : "<<temp+1;
}
}
}
}

Wednesday, 19 October 2016

Double And Add One



Problem : Double And Add One
If you like numbers, you may have been fascinated by prime numbers.
Here's a problem related to prime numbers: Accept input numbers N and i. Identify all prime numbers P up to N with the following property:

P1=2*P+1 is also prime
P2=2*P1+1 is also prime
...
Pi=2*P(i-1)+1 is also prime

Example: Inputs N=100, i=3

Let's start with p=2(it is also prime ). Since i=3 we need 3 consecutive prime numbers that satisfy the Double and Add 1 property explained below:
p1=2*p+1 translates to p1=2*2+1=5, which is prime
p2=2*p1+1 translates to p2=2*5+1=11, which is prime
p3=2*p2+1 translates to p3=2*11+1=23, which is prime

Hence p=2 is to be included in the output.

Next, if p=3, the derived numbers are 7, 15, 31 of which 15 is not prime. Hence p=3 is not a solution

Exploring other primes up to 100 in this fashion, we identify the following additional numbers to be included in the solution for i=3:

5 (since the derived numbers 11, 23, 47 are all prime)
89 (since the derived numbers 179, 359, 719 are all prime)

Hence the output would be: 2, 5, 89

Input format for the example: 100 3
Output format for the example: 2 5 89
(Numbers separated by single space)
Input Format:

First line contains an integer N
Second line contains integer i
Output Format:

Space delimited prime numbers satisfying Double and Add 1 property in the given range N

Constraints:
  1. 2< N <= 100000 
  2. 1< i <= 10 

Sample Input and Output


EXAMPLE NUMBERSAMPLE INPUTSAMPLE OUTPUT
1
100
3
2 5 89
2
20
2

2 5 11

Solution Source code :-

#include<iostream> 
using namespace std; 
bool primcheck ( int a)
{
int temp=0; 
for(int i=2;i<a;i++)
if(a%i==0)
temp++; 
}
if(temp==0)
return true; 
else 
return false; 
}
int main()
{
int N,in,temp=0,a;
cout<<"Enter N and i =";
cin>>N>>in;
for(int i=2;i<N;i++)
{
if(primcheck(i))
{
a=i;
for(int j=0;j<in;j++)
{
a=a*2+1;
if(!(primcheck(a)))
temp++;
}
if(temp==0)
cout<<i<<endl;
}
temp=0;
}
}

Tuesday, 18 October 2016

Start with programming in C

Start with C language


So, if you don’t have any idea about C or programming and why it is use, did is useful etc. question in your mind 


Then single answer is Yes, it is very important if you  want  to develop any computer Application like games, software even in hacking.


then first you need


   è Dev C++   (for windows)                 link
   è Cppdroid  (for Android)                   link
   è Gcc compiler according to your Operating System( OS )


History:- It is very boring part for skip it but if you want then go to wiki


Advantages:-
    
     1.       Portable language means your created  program can share with others
     2.       Close to machine so it give you to direct hardware access
     3.       Variety of data type with powerful operators
Disadvantages:-
     
     1.        Not have OOP’s concepts (that why c ++ developed)
     2.       C doesn’t have namespace concepts
     3.       It Application is only portable on same type OS ( NOTE:- Application not programs  )

 it is only some point there are many more


NOTE very important :- if not do any programming then forget all things and set your mind that “ learning of programming important if you want to do some things in computer and you must read this post completely    ”

My First Program :-
Open dev c++ and press Ctl+N

        
Past this code in window

#include<stdio.h>
int main()
{
printf("My First program by TrueProgrammerS");
return 0;

}

And  save it as c source file


Now click F9 if all things are okey then you get no error like this report window if any error are their check the code and try again


After done click F10 for run your program then you get output window in black color it know as console window and its is console application now



Like this
So how it works


So 1st line
#include<stdio.h>  :- this line add stdio.h file in your program from C library for printf



int mian() :- it is a position where your program start running and this { } show it’s body


printf(): it use to display any thing on that console window and what ever you write in this “ 


Any thing“ it show on window


return 0 :- it show that your main is end


last one ; :- this   ‘;’ this use to show end of line it means your line is end

Note= in C small letter and capital letter are different ( that why C is case sensitive language)


so try and comment if you get any problem 


thanks for reading
next post I will show to take input and do some math’s with C.


 GOOD NIGHT