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



1 comment:

  1. Hello everyone, I saw comments from people who already got their loan from Jackson Walton Loan Company, honestly i thought it was a scam , and then I decided to apply under their recommendations and just few days ago I confirmed in my own personal bank account a total amount of $29,000 which I requested for business. This is really a great news and i am so happy, I am advising everyone who needs real loan and sure to pay back to apply through their email (Text or Call ) +1-205-5882-592

    They are capable of given you your loan thanks.

    Contact Mr Jackson.

    E-mail: jacksonwaltonloancompany@gmail.com

    Fax: +1-205-5882-592

    Website: jacksonwaltonloancompany.blogspot.com

    Address is 68 Fremont Ave Penrose CO, 81240.

    ReplyDelete

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...