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;
}
}
}
}

PDFRise

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;
}
}

PDFRise

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  

Wednesday, 12 October 2016

codevita box in a box solution

                              PROblem 

Idea is to take a number as input and print a pattern of boxes
If input is 2, two boxes are to be printed - one inside the other
Smallest box will be of size 3*3, the next bigger box will be 5*5, the next one will be 7*7, so on and so forth
For input 1, then draw a box of dimensions 3*3
For input 2, outer box will be 5*5, inner will be 3*3
For input 3, outer box will be 7*7, with 2 more inner boxes
So for n, outermost box will be n*2 +1 in size, with (n-1) inner boxes
All boxes will be top left aligned as shown in the figure
Input Format:

First line of input contains a number N
Output Format:

Print N nested boxes
Constraints:
  1. 0 < N < 25 

Sample Input and Output

Example NumberSample InputSample Output
1
2

2
3






 Solution 
 
 

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"enter N =  ";
cin>>n;

for(int i=1;i<=((2*n)+1);i++)
{
for(int j=1;j<=((2*n)+1);j++)
{
if(i==1||j==1||i==n*2+1||j==n*2+1)//for last box
cout<<"* ";
else
{
if(i%2==0)//for even line
{
if(j%2==0)
cout<<"  ";
else if(j<=i)
cout<<"  ";
else
cout<<"* ";
}

else if(i%2==1)// for odd line
{
if(j<=i)
cout<<"* ";
else if(j%2==0)
cout<<"  ";
else
cout<<"* ";
}
}
}
cout<<endl;
}
return 0;
}


       
PDFRise

codevita KNight move solution


Problem : Knight Moves


A Chess board position is accurately captured by Forsyth-Edwards notation and is abbreviated as FEN. A FEN "record" defines a particular game position, all in one text line and using only the ASCII character set. A FEN record contains six fields. A complete description of the FEN format to represent Chess positions can be found at here

For the purpose of this problem only consider first of the six fields of FEN. Before we describe the problem, let us look at how FEN maps to a board position. The following 5 images show board positions and its corresponding FEN representation. 

                    Figure 1.




This board position depicts initial position before any side has made a move. In FEN format this board position is represented as


rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w
















Let's say, White plays e4. Then the board position looks like shown below 


                    Figure 2.




This board position depicts the Chess board after White has played e4. In FEN format this board position is represented as


rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b
















Similarly, 3 more half-moves are depicted in following diagrams 

                          Figure 3.                                                  Figure 4.                                                     Figure 5. 


The FENs corresponding to Figure 3, 4 and 5 are represented as 

           3. rnbqkbnr/pppp1ppp/8/4P3/4P3/8/PPPP1PPP/RNBQKBNR w
           4. rnbqkbnr/pppp1ppp/8/4p3/4PP2/8/PPPP2PP/RNBQKBNR b
           5. rnbqkbnr/pppp1ppp/8/8/4Pp2/8/PPPP2PP/RNBQKBNR w 

Wikipedia describes first field of FEN format as follows 

Each rank is described, starting with rank 8 and ending with rank 1; within each rank, the contents of each square are described from file "a" through file "h". Following the Standard Algebraic Notation(SAN), each piece is identified by a single letter taken from the standard English names (pawn = "P", knight = "N", bishop = "B", rook = "R", queen = "Q" and king = "K").[1] White pieces are designated using upper-case letters ("PNBRQK") while black pieces use lowercase ("pnbrqk"). Empty squares are noted using digits 1 through 8 (the number of empty squares), and "/" separates ranks. 

The second field denotes whose move it is now. "w" depicts that it is White's turn to play and "b" indicates that it is Black's turn to play 

CodeVita Problem Statement 

Given a board position in FEN format, your task is to find out all the move(s) that Knight(s) of the playing side can make. 


Input Format: 
  1. First line contains single FEN record, which corresponds to a particular board position and also indicates whose turn it is.


Output Format: 
  1. The output must be printed as follows
    1. All legal moves that Knight can make must be in the format "[<Move Format>]"
    2. Where <Move Format> is move represented in format "[fromSquare][toSquare]"
  2. See Example section for better understanding of output format
  3. Follow Output printing specification to print the output in required format


Sample Input and Output

SNO.INPUTBOARD DEPICTIONOUTPUT
1
3k4/8/8/8/8/1K4R1/5R2/7N w


[]
2
3k4/8/8/8/8/6b1/5r2/K6N w


[h1g3, h1f2]
3
2k1b3/6n1/n7/5R2/8/8/8/4K3 b


[g7e6, g7f5, g7h5, a6b8, a6c7, a6c5, a6b4]


  1. Should start with "[" and end with "]"
  2. If more than one move is possible, moves should be separated by a comma followed by whitespace
  3. Moves of a single bishop should be printed in Move Format. Scan the board from 8th rank to 1strank from a-file to h-file. Whichever square gets hit first, that move should be printed first.
  4. If more than one bishop exists for side to move, then start scanning for bishop from 8th rank to 1st rank, left to right i.e. from a-file to h-file. Whichever bishop appears first, print all moves for that bishop first.
  5. Verify your understanding of how printing should happen against examples shown above 

 SOLUTION
Please read notes
Thanks for visting.I'll soon update explition video
You can get board to string at -here don't forget to add w/b for turn

for download of source code mega link

       

            
/******************************************************KINGHT MOVE SOLUTION************************************************/
/*
Notes:-
1. it check KINGHT MOVE only
2. it not check correctness of fen inputr it assume that input string is correct (worng string also give worng ouput)
3. it not care about kings in board it also work if one knight pressent in board which is worng according to chess rules 
*/
#include<iostream>
using namespace std;//you can't use namespace in codevita so you can replace cout with std::cout as similler to cin


bool move_index_checker(int,int);
bool move_checker(int,int);
void moves(int,int);


string ip;//for fen input
char Board[9][9]=    // for storing chess board in matrix it. it store it in 1 to 8 index o index is empty
{{' ',' ',' ',' ',' ',' ',' ',' ',' '},
 {' ',' ',' ',' ',' ',' ',' ',' ',' '},
 {' ',' ',' ',' ',' ',' ',' ',' ',' '},
 {' ',' ',' ',' ',' ',' ',' ',' ',' '},
 {' ',' ',' ',' ',' ',' ',' ',' ',' '},
 {' ',' ',' ',' ',' ',' ',' ',' ',' '},
 {' ',' ',' ',' ',' ',' ',' ',' ',' '},
 {' ',' ',' ',' ',' ',' ',' ',' ',' '},
 {' ',' ',' ',' ',' ',' ',' ',' ',' '}},ch;//ch for storing turn of player
int count=1,i=1,j=1;
unsigned int a=0;


int main()
{
cout<<"Enter string=\n";
getline(cin,ip);//ip store the fen string
cout<<ip;
while(count<=64)// 
{
//cout<<"ip["<<a<<"]="<<ip[a];
if(ip[a]=='r'||ip[a]=='n'||ip[a]=='b'||ip[a]=='q'||ip[a]=='k'||ip[a]=='p'||ip[a]=='R'||ip[a]=='N'||ip[a]=='B'||ip[a]=='Q'||ip[a]=='K'||ip[a]=='P')
{
Board[i][j]=ip[a];
j++; count++;
}
else if(ip[a]=='/')
{
i++; j=1;
}
else
{
j+=(int)ip[a]-48;
//cout<<(int)ip[a]-48<<endl;
count+=(int)ip[a]-48;
}
a++;
}
(ip[a+1]=='w')? ch='N':ch='n';
cout<<ch<<endl<<endl;

int x[3]={1,0,0},y[3]={0,0,0};
for(i=1;i<9;i++)
{
for(j=1;j<9;j++)
{
if(Board[i][j]==ch)
{
x[x[0]]=i;y[x[0]]=j;
x[0]+=1;
}
}
}
cout<<'[';
if(x[1]!=0&&y[1]!=0)
{
moves(x[1],y[1]);
}
if(x[2]!=0&&y[2]!=0)
{
moves(x[2],y[2]);
}
cout <<']';
cout<<endl<<endl;
}


bool move_index_checker(int v,int h)
{
if((v>0&&v<9)&&(h>0&&h<9))
return true;
return false;
}

bool move_checker(int v,int h)
{
if(ch=='N')
{
if(Board[v][h]=='R'||Board[v][h]=='N'||Board[v][h]=='B'||Board[v][h]=='Q'||Board[v][h]=='K'||Board[v][h]=='P')
return false;
}
else
{
if(Board[v][h]=='r'||Board[v][h]=='n'||Board[v][h]=='b'||Board[v][h]=='q'||Board[v][h]=='k'||Board[v][h]=='p')
return false;
}
return true;
}

void moves(int v,int h)
{
if(move_index_checker(v-2,h+1)&&move_checker(v-2,h+1))
{
cout<<static_cast<char>(h+96)<<9-v<<static_cast<char>(h+96+1)<<9-(v-2)<<',';
}
if(move_index_checker(v-2,h-1)&&move_checker(v-2,h-1))
{
cout<<static_cast<char>(h+96)<<9-v<<static_cast<char>(h+96-1)<<9-(v-2)<<',';
}
if(move_index_checker(v+2,h+1)&&move_checker(v+2,h+1))
{
cout<<static_cast<char>(h+96)<<9-v<<static_cast<char>(h+96+1)<<9-(v+2)<<',';
}
if(move_index_checker(v+2,h-1)&&move_checker(v+2,h-1))
{
cout<<static_cast<char>(h+96)<<9-v<<static_cast<char>(h+96-1)<<9-(v+2)<<',';
}
if(move_index_checker(v-1,h+2)&&move_checker(v-1,h+2))
{
cout<<static_cast<char>(h+96)<<9-v<<static_cast<char>(h+96+2)<<9-(v-1)<<',';
}
if(move_index_checker(v-1,h-2)&&move_checker(v-1,h-2))
{
cout<<static_cast<char>(h+96)<<9-v<<static_cast<char>(h+96-2)<<9-(v-1)<<',';
}
if(move_index_checker(v+1,h+2)&&move_checker(v+1,h+2))
{
cout<<static_cast<char>(h+96)<<9-v<<static_cast<char>(h+96+2)<<9-(v+1)<<',';
}
if(move_index_checker(v+1,h-2)&&move_checker(v+1,h-2))
{
cout<<static_cast<char>(h+96)<<9-v<<static_cast<char>(h+96-2)<<9-(v+1)<<',';
}
}

       
PDFRise