Freelancer

Perfect square or not

Perfect square or not - C Programming Example

This is an example of C program in which the desired number will be given as input. Output will show whether it is a perfect square or not.

We will use two different techniques for this code. The first method is using For loop statement and the second method is using SQRT (Square root) function.

C Programming Code : 1

#include<stdio.h>#include<conio.h>main(){ int n,a; printf("Enter the number that you want to know whether it is a perfect square or not \n"); scanf("%d",&n); for(a=0;a<=n;a++) { if(n==a*a) { printf("Yes indeed it is a perfect square"); return 0; } } printf("No its not"); getch();}

Output of the code

Checking a number if it is perfect square or not

Checking a number if it is perfect square or not

Explanation of the code

·#include<stdio.h>

#include<stdio.h>

ØThis are called header file.A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source filesThe C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header<stdio.h>.In the other hand<conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C.This header declares several useful library functions for performing "console input and output" from a program.

·main()

ØIn C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.

·int a,n;

ØAn integer is awhole number (not a fraction) that can be positive, negative or zero.Therefore, the numbers 10,0, 25 and 5.148 are all intergers.Unlike floating point numbers,integers cannot have decimal place.Here we declare “a”and “n” variable ininteger type. So this two variable cannottake values containing decimal number.

·printf() function

ØC uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h. Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen.

·scanf() function

ØThis function is used to get input from the user of the program.scanf(“%d”,&n”)is used here. So, this program will read in a integer value for “n” variable that the user enters on the keyboard(%d is for int values.

printf(Yes indeed it is a perfect square):It is used to print the main result.It shows that the desired number is a perfect square.

printf(No its not):It is used to print the main result.It shows that the desired number is not a perfect square.

getch() function

Øgetch() is used to hold the console(output) window on the screen after the whole program run is completed till the user enters a key from keyboard. This function is present in the header file called conio.h.

C Programming Code : 2

#include<stdio.h>#include<conio.h>#include<math.h>main(){ int a,n; printf("Enter the number that you want to whether it is a perfect square or not \n"); scanf("%d",&n); a=sqrt(n); if(a*a == n) printf("Yes,it is a perfect square."); else printf("No,it is not a perfect square."); getch();}

Explanation of the code

·#include<stdio.h>

#include<stdio.h>

ØThis are called header file.A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source filesThe C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header<stdio.h>.In the other hand<conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C.This header declares several useful library functions for performing "console input and output" from a program.

·main()

ØIn C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.

·int a,n;

ØAn integer is awhole number (not a fraction) that can be positive, negative or zero.Therefore, the numbers 10,0, 25 and 5.148 are all intergers.Unlike floating point numbers,integers cannot have decimal place.Here we declare “a”and “i” variable ininteger type. So this two variable cannottake values containing decimal number.

·printf() function

ØC uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h. Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen.

·scanf() function

ØThis function is used to get input from the user of the program.scanf(“%d”,&n”)is used here. So, this program will read in a integer value for “n” variable that the user enters on the keyboard(%d is for int values.

Øprintf(Yes indeed it is a perfect square):It is used to print the main result.It shows that the desired number is a perfect square.

Øprintf(No its not):It is used to print the main result.It shows that the desired number is not a perfect square.

·getch() function

Øgetch() is used to hold the console(output) window on the screen after the whole program run is completed till the user enters a key from keyboard. This function is present in the header file called conio.h.

Final Result

This program will show you whether thr desired number is a perfect square or not.What you need to do is, just input the number.