Monday 29 August 2016

Program for Decimal to Binary Conversion

www.programmingcalculator.com
https://nikhilnihal.github.io/programmingcalculator/index.html

C++ Program for Decimal to Binary Conversion

#include<bits/stdc++.h>

using namespace std;

int main()
{
    long dec,rem,i=1,sum=0,binary=0;
    cout<<"Enter the decimal to be converted:";
    cin>>dec;

    while(dec!=0)
    {
      rem=dec%2;
      dec/=2;
      binary+=rem*i;
      i*=10;
    }

    cout<<"The binary of the given number is:"<<binary<<endl;

    return 0;
}

Program for Binary to Decimal Conversion

www.programmingcalculator.com
https://nikhilnihal.github.io/programmingcalculator/index.html

C++ Program to Convert Binary To Decimal


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long bin, dec = 0, rem, num, base = 1;

    cout << "Enter the binary number";
    cin >> num;
    bin = num;
    while (num > 0)
    {
        rem = num % 10;
        dec = dec + rem * base;
        base = base * 2;
        num = num / 10;
    }
    cout << "The decimal equivalent of " << bin << " : " << dec << endl;
    return 0;

}

Saturday 27 August 2016

Ratio of Two Numbers

www.programmingcalculator.com
https://nikhilnihal.github.io/programmingcalculator/index.html

C++ PROGRAM TO FIND RATIO OF TWO NUMBERS

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int first_number, second_number,ans,t,gcd;

    cin>>first_number>>second_number;

    int a=first_number;

    int b=second_number;

    while (b != 0) {
     t = b;
     b = a % b;
     a = t;
    }

    gcd = a;

    printf("%d/%d",first_number/gcd,second_number/gcd);

    return 0;
}

Power Finding Program

www.programmingcalculator.com
https://nikhilnihal.github.io/programmingcalculator/index.html

C++ PROGRAM FOR FINDING POWER

#include <bits/stdc++.h>
using namespace std;

int main() {
    int exp;
    float base, power = 1;

    cin >> base >> exp;

    while (exp != 0) {
        power *= base;
        --exp;
    }

    cout << "Result = " << power;
    
    return 0;
}

L.C.M and H.C.F Program

www.programmingcalculator.com
https://nikhilnihal.github.io/programmingcalculator/index.html

C++  PROGRAM  FOR  FINDING  H.C.F.  &  L.C.M.



#include<bits/stdc++.h>
 
int main() 
{
  int a, b, x, y, t, gcd, lcm;
 
  cin>>a>>b;
 
  a = x;
  b = y;
 
  while (b != 0) {
    t = b;
    b = a % b;
    a = t;
  }
 
  gcd = a;
  lcm = (x*y)/gcd;
 
  cout<<gcd<<endl;
  cout<<hcf<<endl;
 
  return 0;
}

Friday 26 August 2016

Logical XOR Program

www.programmingcalculator.com
https://nikhilnihal.github.io/programmingcalculator/index.html

C++ Program for Logical XOR of two Decimal Numbers


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int first_number, second_number,ans;

    cin>>first_number>>second_number;

    ans=first_number^second_number;

    cout<<ans<<endl;

    return 0;
}

Logical OR Program

www.programmingcalculator.com
https://nikhilnihal.github.io/programmingcalculator/index.html

C++ Program for Logical OR of two Decimal Numbers


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int first_number, second_number,ans;

    cin>>first_number>>second_number;

    ans=first_number|second_number;

    cout<<ans<<endl;

    return 0;
}

Logical AND Program

www.programmingcalculator.com

C++ Program for Logical  AND of Two Decimal Numbers


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int first_number, second_number,ans;

    cin>>first_number>>second_number;

    ans=first_number&second_number;

    cout<<ans<<endl;

    return 0;
}

Monday 1 August 2016

Barracuda networks (A Bangalore's Startup) written Round Programming Questions


1) C program to copy one file to another.


#include<bits/stdc++.h>

using namespace std;

int main()
{
    char ch,src[20],tgt[20];
    FILE *s,*t;

    gets(src);
    s=fopen(src,"r");
    if(s==NULL)
    {
        printf("press any key to exit\n");
        exit(EXIT_FAILURE);
    }


    gets(tgt);
    t=fopen(tgt,"w");
    if(t==NULL)
    {
        printf("press any key to exit\n");
        exit(EXIT_FAILURE);
    }


    while(( ch=fgetc(s))!=EOF)
        fputc(ch,t);

    return 0;

}


-----------------------------------------------------------------------------------------------------------------------

2 ) use function pointer to add , sub and multiply

#include<bits/stdc++.h>

using namespace std;

int add(int a,int b)
{
return a+b;
}


int sub(int a,int b)
{
return a-b;
}


int mul(int a,int b)
{
return a*b;
}


int main()
{
    typedef int (*a)(int,int); /// a is now declared as type

    a fp[3]={add, sub ,mul};

 int n;
 cin>>n;
 int c,b;
 cin>>c>>b;

if(n==0)
    cout<<fp[0](c,b)<<endl;
else if(n==1)
    cout<<fp[1](c,b)<<endl;
else
    cout<<fp[2](c,b)<<endl;

return 0;
}

DMX technologies (A Bangalore's Startup) written Round Programming Questions

1) Spiral Matrix Printing 
  
For example : if given n=2 , then output is   3 2 
                                                                      0  1

                       if given n=3 , then output is   4  3  2
                                                                      5  0  1
                                                                      6  7  8 


C++ Program 

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;

    int a[n+1][n+1];
    int num=n*n-1;

    for(int i=1,j=n;i<=n,j>=0;i++,j--)
    {
        for(int k=i;k<=j;k++)                              ///upper row
            a[i][k]=num--;

        for(int k=i+1;k<=j;k++)
            a[k][j]=num--;

        for(int k=j-1;k>=i;k--)
            a[j][k]=num--;

        for(int k=j-1;k>i;k--)
            a[k][i]=num--;

           // if(num<0) break;
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            cout<<a[i][j]<<"   ";
              cout<<endl;
    }

    return 0;

}

-------------------------------------------------------------------------------------------------------------------------

2) All Permutations of a given string using Recursion 

Link :- http://nikhilnihal.blogspot.in/2016/07/printing-all-permutation-of-string.html



Wednesday 20 July 2016

Printing all the permutation of the string using recursion


           dog
   dog ,  odg ,  god    (in first call 1st character is swapped with all postions (1st to last))
dgo     ogd       gdo   (in 2nd call ,2nd character is swapped with all postions(2nd to last) )




#include<bits/stdc++.h>

using namespace std;

void permu(string k,int fst,int lst)
{
    if(fst==lst)
        cout<<k<<endl;

    for(int i=fst;i<=lst;i++)
    {
        swap(k[fst],k[i]);
        permu(k,fst+1,lst);
        swap(k[fst],k[i]);
    }
}

int main()
{
    string s;
    cin>>s;
    int lst=s.size()-1;
    permu(s,0,lst);
    return 0;
}

Reverse String using Recursion C++


Here the Logic is simple to print the last character of the string for each function call and again call the reverse function again ,this time the string doesn't contain last character.


#include<bits/stdc++.h>

using namespace std;

void reve(string k)
{

  int sz=k.size();
  if(sz==1)
    cout<<k[sz-1];
  else
  {
     cout<<k[sz-1];
     string p=k.substr(0,sz-1);
     reve(p);
  }
}

int main()
{
    string s;
    cin>>s;
    reve(s);

    return 0;
}

Friday 15 July 2016

LeadSquared Interview Questions

On 12th july , I got a chance to sit for the written round of Leadsquared drive.
In 1 hour we have to do 3 java questions.

1st question->Excel sheet problem
                      ex. 1 -> A , 2->B , 26->Z ,27->AA 

 import java.util.*;

public class excel
{
public static void main(String args[])
{
  Scanner s=new Scanner(System.in);
       int r,n=s.nextInt();
  String k=" ";
       while(n>0)
  {
r=n%26;
          if(r==0)
 {
String ch="Z";
            k=k+ch;
n=n/26 -1;
 }
          else
 {
 int ch=r-1+(int)'A';
 char p=(char)ch;
 String h= Character.toString(p);
              k=k.concat(h);    
     n=n/26;
 }
  }
 
  int len=k.length();
  for(int i=len-1;i>=0;i--)
  System.out.print(k.charAt(i));
       // System.out.println(k);  
}
}



2nd Question-> find a number just bigger than the given number, with the same digits combination.
                          example  : 756 -> 765  ,  342-> 423 , 987->"Not possible"


#include<bits/stdc++.h>

using namespace std;

int main()
{
    string k;
    cin>>k;
    int l=k.length();
    int a[l];

    for(int i=0;i<l;i++)
     a[i]=k[i]-'0';

    int lst=a[l-1],fl=0,rem;

    for(int p=l-1;p>=1;p--)
    {
     for(int i=p-1;i>=0;i--)
     {
      if(a[p]>a[i])
       {
          fl=1;
          int tmp=a[i];
          a[i]=a[p];
          a[p]=tmp;
          rem=i+1;
          break;
       }
     }
     if(fl==1) break;
    }


    if(fl==0) cout<<"Not possible"<<endl;
    else
    {
         sort(a+rem,a+l);
        for(int i=0;i<l;i++) cout<<a[i];
        cout<<endl;
    }
    return 0;
}



3rd Question: Parenthesis Balance Checking Problem.
                       {([]{})}-> Balanced , {}() ->Balanced , {(})-> Not Balanced



import java.util.*;

public class balanced_parenthesis
{
public static void main(String arg[])
    {
 Scanner s=new Scanner(System.in);
      String k=s.nextLine();
      int l=k.length(),fl=0;
     
 Stack st = new Stack();
     
      for(int i=0;i<l;i++)
      {
        if(k.charAt(i)=='(' || k.charAt(i)=='{' || k.charAt(i)=='[')
         {
st.push(new Character(k.charAt(i)));
         }
        else if(k.charAt(i)==')' || k.charAt(i)=='}' || k.charAt(i)==']')
        {
          if(st.empty())
          {
              fl=1; break;
          }
 char a = (Character) st.pop();
          if((k.charAt(i)=='(' && a!=')') || (k.charAt(i)=='[' && a!=']') || (k.charAt(i)=='{' && a!='}'))
            {
                fl=1; break;
            }
       }
     }  

if(!st.empty() || fl==1)
System.out.println("Not balanced");
    else
        System.out.println(" balanced ");

}
}


Wednesday 6 July 2016

c++ Program to reverse the sentence

C++ Program to reverse the sentence

Input- "My name is Nikhil"
Output "Nikhil is name My"


Here the idea to reverse each word in 1st phase and in 2nd phase reverse the whole sentence.

#include<bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    getline(cin,s);

    cout<<s<<endl;


    int l=s.size();

    int pre=0;
    for(int i=0;i<l;i++)
    {
      if(s[i]==' ')
      {
        int k=i-1;
        while(pre<k){char c=s[pre]; s[pre]=s[k]; s[k]=c; pre++; k--; }

        pre=i+1;
      }
      else if(i==l-1)
      {
        int k=i;
        while(pre<k){char c=s[pre]; s[pre]=s[k]; s[k]=c; pre++; k--; }
      }
    }

    int bck=0;
    int nxt=l-1;

    while(bck<nxt)
    {
     char c=s[bck];
     s[bck]=s[nxt];
     s[nxt]=c;

     nxt--; bck++;
    }

    cout<<s<<endl;

    return 0;
}

Tuesday 17 May 2016

Directi Online Interview Question :- Two non-overLapping subarrays of size k which gives maximum sum


Directi Online Interview Question.


Two non-overLapping subarrays of size k which gives maximum sum.
This is a 1-D Dp approach ,in O(n) time-complexity.


#include<bits/stdc++.h>

using namespace std;

int pre[1000],a[10000],dp[1000];

int main()
{
    int n,k;
    cin>>n>>k;

    for(int i=1;i<=n;i++) cin>>a[i];

        pre[0]=0;
        pre[1]=a[1];

        dp[k]=pre[k];    ///upto length k
    for(int i=2;i<=n;i++) pre[i]=pre[i-1]+a[i];

    for(int i=k+1;i<=n;i++) dp[i]=max(dp[i-1],pre[i]-pre[i-k]);

    int ans=0;

    for (int i=k;i<=n;i++){
    ans = max(ans,dp[i-k]+pre[i]-pre[i-k]);}

    cout<<ans<<endl;


    return 0;
}

Thursday 28 April 2016

Women safety and security android app

                                               


Download Link:

https://play.google.com/store/apps/details?id=comm.Kishlay.screamDetector&hl=en












"Chilla" is a personal safety & security app that can be triggered by just a shrill scream . It totally removes the hassles of unlocking the phone or opening the app. It has been found that in cases where someone follows a girl / women or eve-teases her , she generally doesn't calls out to parents or police and if they attack her , the app is of no use to her then. In that situation scream is a natural reaction and tapping that is the best possible way to bring her help.
The apps can be triggered by:
1. Scream
2.Pressing power Button 5 times
After Trigger, it does the following
1.Sends SMS with location
2.Sends Email with Audio Recording
3.Automatically places a call
If a scream is detected the app automatically unlocks the phone and places a call to the guardian.
The power button feature works even if the app is not on.
It virtually acts as your personal guardian angel when you are in distress and goes a long way in ensuring your personal safety so that With an elegant yet simple interface it is extremely simple to use.
Not just the safety of women, the safety mode can be additionally customized to cater to the safety
of men too. Besides, the app can be used in case of emergencies too, for example during a heart
attack, the victim’s location and recording can be immediately sent without unlocking the phone.
The best part is, Scream Alert is absolutely free, and it is made for the sole motive of helping women
lead freer and safer lives. A revolution in field of personal security, Scream Alert is your ultimate
safety app, install it today and put your safety in your own hands. Don’t just take our word for it, try
it.
 This app has been acknowledged by Indian Government and they have put it on their website:

Uploading and Running Lambda function in AWS

Main.go package main import ( "fmt" "encoding/json" "log" "github.com/aws/aws-lambda-g...