I'm not getting about what is the error in the following program..please answer me.

0 votes
asked Jun 16, 2018 by Stuti Vasudev
#include<stdio.h>
#include<stdlib.h>

int SquareArea(int k)
{
    int i,A;
    for (i=5;i<k;i+=5)
    {
        printf("The side of square   : %d ");
        scanf("%d",&k);
        A=i*i;
    }

    return A;
}

int main()
{
    int Area,k;
    Area=(SquareArea(k));
    printf("\nArea of square is  %d",Area);

printf("\n\n\n\n\n\n\n\n\n");
return 0;
}

2 Answers

+2 votes
answered Jun 16, 2018 by vikram shah

firstly your  logic to compute Area is wrong.

secondly   printf("The side of square   : %d "); requires two parameter , like   printf("The side of square   : %d ",k);

third error is Area=(SquareArea(k));  you are passing garbage value.

fourth :

for (i=5;i<k;i+=5)
    {
        printf("The side of square   : %d ");
        scanf("%d",&k);
        A=i*i;
    }

is wrong , SquareArea funtion can simply return k*k;

fifth this line   scanf("%d",&k); should be in main() before call to SquareArea()

0 votes
answered Jan 25, 2019 by Jyothi_Rk
edited Jan 28, 2019
Hi,

I have modified your code with the addition of comments stating why was the particular line modified.Do have a look.

#include<stdio.h>        
//stdlib.h is not needed

int SquareArea(int k)
{
    int A;
    A=k*k;
    return A;  
    // Instead  of these 3 lines,we can just write  -> return(k*k);
}

int main()
{
    int Area,k;
    printf("The side of square\t");      
    // '%d' is not needed because you are not printing value
   
    scanf("%d",&k);   
    // "k" value should be read before sending it,else garbage value will be sent
    
    Area=SquareArea(k);               
    //common braces were not needed.So I removed them
    
    printf("\nArea of square is  %d\n",Area);
    //unnecessary printf with multiple \n is removed
return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...