Its basically gives output such as below based on square of number:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,c,d[25],e,f[25],g;
int i,j,k,l,m,n,o,r;
float b;
clrscr();
Ankit:
printf("Enter any no from:-4,9,16,25,36,......\n");
scanf("%d",&a);
printf("\n");
b=sqrt(a);
c=sqrt(a);
if(b*b!=a || a==1)
{
printf("Number entered is not valid as its square-root is not integer %f\n",b);
goto Ankit;
}
else
{
//First Row//
for(i=1;i<=c;i++)
{
printf("%d ",i);
}
//Second group of rows//
printf("\n\n");
k=0;
f[k]=b;
m=2;
for(i=0;i<((c/2)-1);i++)
{
l=i+1;
d[k]=b+((c+2*(c-2))-6*i);
k=0;
for(j=0;j<i;j++)
{
printf("%d ",d[k]-(l-1));
k=k+1;
l=l-1;
}
printf("%d ",d[k]);
e=d[k];
for(j=0;j<c-m;j++)
{
e=e+1;
printf("%d ",e);
}
for(j=i;j>=0;j--)
{
g=f[j];
g=g+l;
printf("%d ",g);
l=l+1;
}
b=e;
k=k+1;
f[k]=e;
printf("\n\n");
m=m+2;
}
//Third row//
if(c!=2)
{
if(c!=2 && c!=3)
{
l=(c/2)-1;
for(i=0;i<((c/2)-1);i++)
{
printf("%d ",d[i]-l);
l=l-1;
}
if(c%2==0)
{
d[k]=f[k]+2;
}
else
{
d[k]=f[k]+5;
}
printf("%d ",d[k]);
}
else
{
k=0;
d[k]=8;
printf("%d ",d[k]);
}
if(c%2==0)
{
l=2;
printf("%d ",d[k]-1);
for(i=((c/2)-1);i>0;i--)
{
printf("%d ",f[i-1]+l);
l=l+1;
}
}
else
{
l=1;
printf("%d ",d[k]+1);
for(i=(c/2);i>0;i--)
{
printf("%d ",f[i-1]+l);
l=l+1;
}
}
printf("\n\n");
}
// Fourth groups of rows//
if(c%2==0)
{
m=0;
n=2;
r=0;
for(i=((c/2)-2);i>0;i--)
{
o=3+r;
l=(c/2)+m;
k=0;
for(j=0;j<i+1;j++)
{
e=d[k]-l;
printf("%d ",e);
k=k+1;
l=l-1;
}
for(j=0;j<n;j++)
{
e=e-1;
printf("%d ",e);
}
for(j=i+1;j>0;j--)
{
e=f[j-1]+o;
printf("%d ",e);
o=o+1;
}
m=m+1;
n=n+2;
r=r+2;
printf("\n\n");
}
}
else
{
m=0;
n=1;
r=0;
for(i=0;i<((c/2)-1);i++)
{
o=2+r;
l=(c/2)+m;
k=0;
for(j=0;j<c/2-i;j++)
{
e=d[k]-l;
printf("%d ",e);
k=k+1;
l=l-1;
}
for(j=0;j<n;j++)
{
e=e-1;
printf("%d ",e);
}
for(j=c/2-i;j>0;j--)
{
e=f[j-1]+o;
printf("%d ",e);
o=o+1;
}
m=m+1;
n=n+2;
r=r+2;
printf("\n\n");
}
}
}
//Fifth row//
if(c%2==0)
{
m=c+c+(2*((c/2)-1));
}
else
{
m=c+c+(c-2);
}
for(j=0;j<c;j++)
{
printf("%d ",m);
m=m-1;
}
getch();
}
No comments:
Post a Comment