#include <stdio.h>
#include <stdlib.h>

void main()
{
    int n,i,p;
    printf("enter the no: ");
    scanf("%d",n);
    for(i=0;i<n;i++)
    {

 p=i*i

 if(p==n)
 {
     scanf("the square root of %d is %d",n,i);
 }
    }
}

its not working error at if(p==n)

scanf("%d",n);

Apparently you still don't understand how scanf() works. Allow me to elaborate a bit. Every argument in C is passed by value, which means that a copy of the value of an argument is made and assigned to a temporary variable represented by the corresponding function parameter. So this:

void foo(int arg)
{
    arg += 10;
}

int main(void)
{
    int x = 0;

    foo(arg);
    printf("%d\n", x);

    return 0;
}

Will print 0 because only the copy of x is incremented. You can think of it something like this functionally equivalent code, where the problem is far more obvious:

int main(void)
{
    int x = 0;

    {
        int arg = x;

        arg += 10;
    }

    printf("%d\n", x);

    return 0;
}

This totally goes against what scanf() is designed to do: read values from input and assign them to variables. So how does scanf() work? Well, you can simulate pass by reference by passing the address of a variable. This creates a pointer to the object which can then be dereferenced, and the original object can be modified:

void foo(int *arg)
{
    *arg += 10;
}

int main(void)
{
    int x = 0;

    arg(&x);
    printf("%d\n", x);

    return 0;
}

Now the output will be 10 because the original object is being incremended instead of a copy. The address is still passed by value, but the object stored at that address is what you really want.

This is how scanf() is able to assign values to variables, but only if you pass the expected pointer. So if the argument isn't already a pointer, you must use the address-of operator or bad things will happen:

scanf("%d", &n);

write
scanf("%d",n); like scanf("%d",&n);
& insert a semicolon after stmnt:p=i*i;

Member Avatar for I_m_rude

the link is simply damn awesome

Read the wikipedia article shown above. For simple programming effort, use the high/low method. First, you have to decide how many decimals of precision you need, and whether or not it preferable for the final result to be low or high. Using a binary search method, you can find the square root, or close approximation, of most any number in a pretty short time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.