Wednesday 5 December 2018

                                               SWITCHING 100 BULBS

There are 100 light bulbs lined up in a row in a long room. Each bulb has its own switch and is currently switched off. The room has an entry door and an exit door. There are 100 people lined up outside the entry door. Each bulb is numbered consecutively from 1 to 100. So is each person.

Person No. 1 enters the room, switches on every bulb, and exits. Person No. 2 enters and flips the switch on every second bulb (turning off bulbs 2, 4, 6...). Person No. 3 enters and flips the switch on every third bulb (changing the state on bulbs 3, 6, 9...). This continues until all 100 people have passed through the room.

Find the state of nth switch(i.e.ON/OFF)?
Input:
Nth integer to find state
Output :
State of the Nth switch.
Assume 0 as OFF and 1 as ON.

Sample:
Input:
64
Output:
1

Program:
#include<stdio.h>
int a[150]={0};
int n=100;
int main()
{
    int i,j,k,swit;
    scanf("%d",&swit);
    for(i=1;i<=n;i++)
    {
        for(k=1;k<=n;k++)
        {
            if(k%i==0)
            {
                if(a[k]==0)
                    a[k]=1;
                else if(a[k]==1)
                    a[k]=0;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        if(i==swit)
        {
            printf("%d",a[i]);
        }
    }
    return 0;
}



Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...