BLOGGER TEMPLATES AND TWITTER BACKGROUNDS �

25 January, 2011

Java: Reverse a number

This program will reverse a long and store it in another.

class NumRev
{
public static void main(String ar[])
{
long num = 1357912; /* Try with diff values */
long rev = 0;
int i = (((Long)num).toString()).length(); /* No of digits */


System.out.println("Original Number: " + num);
for(i-=1; num!=0; i--)
{
long rem = num % 10;
rev += (rem * Math.pow(10, i));
num /= 10;
}


System.out.println("Reversed Number: " + rev);
}
}


Difficulty Rating: 2.5/10
For creative minds: This program gets the number of digits in a number by converting it to a string. Can you do it without the string portion? Is it possible to reverse without using the number of digits at all?

04 August, 2009

Accept a Password string, displaying it as *

This program will accept a string from the user as a password, displaying *'s instead of the data entered by the user.
Lang: C

#include(stdio.h)
#include(conio.h)
#include(string.h)
void main() /* Or int main() with return 0 */
{
char pass[20];
int i;

printf("Enter password: ");
for(i=0; i<20; i+=1)
{
pass[i] = getch();
if(pass[i] == '\r') /* The carriage return escape sequence */
break;
printf("*");
}

pass[i] = '\0';
printf("\n");
if(strcmp(pass, "123")) /* Assume "123" is the correct password */
printf("Correct Password");
else
printf("Wrong Password");

getch();
}

Difficulty Rating: 3/10
For Creative Minds: Try using this with getche() instead of getch(). What changes will you have to make?

19 May, 2009

Introduction to the site

This blog is meant to be a guide for computer programs in various languages. It is not, I repeat, NOT meant to be a definitive database or a place from where you could copy school assignments. Currently Program Matrix has only C, but hopefully C# is soon on its way.

The programs here are meant to be for learners. I will, therefore, provide comparatively easier programs, leaving anything 'high-funda' (as some call it) out. You are still expected to have some basic knowledge of the programming language in question! (And, I'm afraid, a bit advanced too...)

If you are specifically looking for a program, use the search feature at the top. If you still don't find anything, you can always request. I'll try my best to fulfill the same.

As you might already be knowing, there are several methods to write a program to achieve a single objective, and thus, your methods might differ from mine.

Note: Replace the () brackets in the #include directives and any other places as required by <> brackets.