Introduction To Programming

Why programs ? Programming is nothing but making the computers think the same way a human thinks. Programs are made for obtaining a specific solution which can even be solved by humans which when solved by us may consume time..

Thursday, October 29, 2009

Square root without Math.sqrt:

Here is the java program to find sqrt of a number without the Math.sqrt function.
You can also convert into c program in simple way.

class square
{
public static void main(String args[])
{
double x=5;
double y;
double j,i;
for(i=1.000000;(i*i)<5;i=i+0.000001)
{
}
j=i;
i=i-0.000001;
y=(j+i)/2;
System.out.println("y="+y);
}
}

Output:
y=2.2360674999507304

The square root of the number.

In this program I initialized that x=5.
The for loop continue until square of the i is smaller than 5.
When it’s larger it’ll come out of the loop.
When i=2.2360679999507305 it’ll come out of the loop
Then j=i now j=2.2360679999507305
i=i-0.000001
now i will become i=2.2360669999507303
y=(j+i)/2 y value is average of j,i(because the square root’ll between the smaller and larger value)
so now y will becomesquare root of the number.

Using this method try to write c program yourself (it’s simple because I give algorithm).

Logical program 1:

#include < stdio.h >
int fun(char *str)
{
char *ptr=str;
while(*ptr++);
return ptr-str-1;
}
void main()
{
printf("%d",fun("selva"));
}

Output:

5

How ?

Pointer:
It stores another variable address. We can access that variable indirectly using pointer.
Eg:
int i;
int *j; // pointer variable declaration
j=&i; now address of i is stored in j ( & is"address of" operator)
consider
so j =4001.

In program selva string will stored in *str pointer. Consider str address 1000
The string characters will store like this,

 1000 1001 1002 1003 1004 1005 s e l v a \0

char *ptr=str;
now ptr will store the str address i.e.,
ptr=1000
in while loop I used increment operation. It’ll first increment *ptr address and check the condition whether it’s null or not.
This loop will continue untill *ptr is null.

at first ptr=1000
after *ptr++ it will be ptr=1001 and *ptr=e
again ptr will increased ptr=1002 and *ptr=l
like that
ptr=1003 *ptr=v
ptr=1004 *ptr=a
ptr=1005 *ptr=\0
ptr=1006
now *ptr is null loop is come to end

ptr=1006 and str=1000

ptr-str-1
will produce 5.

Loops end with semicolon:

#include < stdio.h >
void main()
{
int i=1;
while(i =0);
printf(“%d”,i);
}

Is’t makes compilation error?
No, It won’t make error. It’ll show only for assigning value in condition as “possibly incorrect assignment”.

Output :
0

While(i=0);
is equivalent to
while(i=0)
{
}

so there's no errors in this coding

Assigning value in if instead of condition

We know that if is used to check condition the general syntax for if conditional statement is

if(condition)
{
Statement1;
}
else
{
Statement2;
}

But we can assign the value for the variable in if and also it work as condition checking.
Here’s the sample program to implement that;

#include < stdio.h >
void main()
{
int i=0,j=0;
if(i=1)
{
j++;
}
printf(“%d %d “, i , j);
}

In this program initially i=0 and j=0.
In if i was assigned i=1 instead of condition checking.
Now i=1 and now the 1 will be act as condition in if. (1 is true, 0 is false).
So condition is true so j will be increased now j=1.
Output:
1 1

If you give i=0 in if i.e.,
if(i=0)
0(false) so j won’t be increased
Output:
0 0

Note: it’ll show only warning as “possibly incorrect assignment” s . Don’t care about that . you can do this in loops