Thursday, 17 July 2014

Three times the sum of digits of the number equals number itself.

// num = 27 = 3*(2+7)
// AIM: To find all such numbers

#include<stdio.h>
void main()
{
    long num = 1, i;
    int temp = 0;
    long  test = 0;
    long count = 0;
    while(count < 100)
    {
        i = num;
        test = 0;
        while(i != 0)
        {
            temp = i % 10;
            test = test + temp;
            i = i / 10;
        }
        if( (test*3) == num)
        {
            printf("found : %d", num);
            //only such number is 27, u can check upto infinite but no use, I already tried. :P
        }
        //printf("num = %d and test*3 = %d\n",num, test*3);
        count++;
        num++;
    }
}

Saturday, 11 January 2014

Automatically Refresh/Reload a Web Page at fixed Interval of Time (Using VB Script)

Copy the code below to notepad and save it as refresh.vbs
Double click on the file to run the script.

On Error Resume Next

Set objExplorer = CreateObject("InternetExplorer.Application")

objExplorer.Navigate "http://www.youtube.com/watch?v=81MV1agQMG4"
objExplorer.Visible = 1

Wscript.Sleep 5000

Set objDoc = objExplorer.Document

Do While True
    Wscript.Sleep 5000
    objDoc.Location.Reload(True)
    If Err <> 0 Then
        Wscript.Quit
    End If
Loop

TEXT TO SPEECH (Using VB Script)

Copy the code below to notepad and save it as filename.vbs
Double click on the file to run the script.

dim m, s
   m=inputbox("Enter text","Text2Speech")
   set s=createobject("sapi.spvoice")
   s.Speak m

Monday, 23 December 2013

Program to invert 'n' alternate bits of a number (Asked in APPLIED MATERIALS coding round)

// initial 30 = 11110
// final   11 = 01011

#include<stdio.h>
int invert_alternate_bit(int,int);
int main()
{
printf("NEW num : %d\n",invert_alternate_bit(30,3));
return 0;
}
int invert_alternate_bit(int num,int n)
{
int c = 0;
printf("OLD num : %d\n",num);
for(int i=1; i<= n ; i++)
{
c = c << 2;
c = c ^ 1;
}
num = num ^ c;
return num;
}

Sunday, 22 December 2013

Program to initialize the array as a pyramid of alphabets (Asked in APPLIED MATERIALS coding round)

#include<stdio.h>
void print_in(int,int,char);
void show(int,int);
void initialize(int,int);
char a[4][9];
int main()
{
char ch='A';
initialize(5,9);
print_in(0,4,ch);
show(5,9);
}
void print_in(int row,int col,char ch)
{
int count=1;
while(count<=25) // till Y
{
if(count==1)
{
a[row][col] = ch;
ch++;
}
if(count>1 && count<=4)
{
a[row][col] = ch;
col++;
ch++;
}
if(count>4 && count<=9)
{
a[row][col] = ch;
col++;
ch++;
}
if(count>9 && count<=16)
{
a[row][col] = ch;
col++;
ch++;
}
if(count>16 && count<=25)
{
a[row][col] = ch;
col++;
ch++;
}
if(count==1)
{
row++; // 1
col--;  // 3
}
if(count==4)
{
row++; // 2
col = col-4;// 2
}
if(count==9)
{
row++; // 3
col = col-6;// 1
}
if(count==16)
{
row++; // 4
col = col-8;// 0
}

count++;
}
}
void show(int row,int col)
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
printf("%c ",a[i][j]);
}
printf("\n");
}
}
void initialize(int row,int col)
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
a[i][j] = ' ';
}
}
}


Sample Output:
A
BCD
EFGHI
JKLMNOP
QRSTUVWXY