write a c program to find the square of a 5 digit number
and print the result.
Answer Posted / sarthak
I was asked to calculate the square without using long int or double. This is how i did it. I was eventually rejected but i have no idea why as the code is correct.
/*Program to find the square of a 5 digit number and print*/
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
/*Part 1: -
num is used to initially read the number
i,j are used as counters
sq is used to multiply the number with each of its digits
sq1 is used to store the result of sq with proper shift and ading zeros
result is used to store and print fial result
rem used to store remainder
quot used to store quotient
rs use as sum of coloumn*/
int num[5],i=0,j=0,sq[5][10],sq1[5][10],result[10],dig,rem,quot,rs=0;
/* Part 2: -
Reading the 5 digit number*/
printf("Enter the 5 digit number\nPlease press enter after each digit: -");
for(i=4;i>=0;i--)
{
scanf("%d",&num[i]);
}
/*Print the entered number*/
printf("\n\nYou entered the number: ");
for(i=4;i>=0;i--)
{
printf("%d",num[i]);
}
/*Part 3: -
Initialize sq as 0*/
for(i=0;i<=4;i++)
{
for(j=0;j<=9;j++)
{
sq[i][j]=0;
}
}
/*Initialize sq1 as all 0s*/
for(i=0;i<=4;i++)
{
for(j=0;j<=9;j++)
{
sq1[i][j]=0;
}
}
printf("\n\nInitially after assigning array to all 0's: -");
/*Print the 2D array after asigning it to 0*/
printf("\n\n");
for(i=0;i<=4;i++)
{
printf("\n");
for(j=0;j<=9;j++)
{
printf("%d",sq[i][j]);
}
}
/* Part 4: -
Multiply the 5 digit number with each of its own digits and storing rowise*/
for(j=0;j<=4;j++)
{
for(i=0;i<=4;i++)
{
dig=num[i]*num[j];
rem=dig%10;
quot=dig/10;
sq[i][j]=sq[i][j]+rem;
if(sq[i][j]>=10)
{
sq[i][j+1]=sq[i][j+1]+(sq[i][j]/10);
sq[i][j]=sq[i][j]%10;
}
sq[i][j+1]=sq[i][j+1]+quot;
}
}
printf("\n\nAfter multiplying the number with each of its respective digits and storing in respective rows: -");
/*Print the array without shifting*/
printf("\n\n");
for(i=0;i<=4;i++)
{
printf("\n");
for(j=0;j<=9;j++)
{
printf("%d",sq[i][j]);
}
}
/*Part 5: -
Applying shift to the array*/
for(i=0;i<=4;i++)
{
for(j=9;j>=0;j--)
{
sq1[i][j+i]=sq[i][j];
}
}
printf("\n\nAfter applying proper shift to the rows of the array: -");
/*Print after applying shift to the array*/
printf("\n\n");
for(i=0;i<=4;i++)
{
printf("\n");
for(j=0;j<=9;j++)
{
printf("%d",sq1[i][j]);
}
}
/* Part 6: -
Initialize result to 0*/
for(i=0;i<=9;i++)
{
result[i]=0;
}
/*add up the coloumns*/
quot=0;
rem=0;
for(j=0;j<=9;j++)
{
for(i=0;i<=4;i++)
{
rs=rs+sq1[i][j];
}
quot=rs/10;
rem=rs%10;
result[j]=result[j]+rem;
rs=quot;
}
printf("\n\nAfter adding up the respective coloumns of the above array \n and reversing the order: -");
/*print the result*/
printf("\n\nThe square of the numer is :- ");
for(i=9;i>=0;i--)
{
printf("%d",result[i]);
}
return 0;
}
Is This Answer Correct ? | 2 Yes | 11 No |
Post New Answer View All Answers
What is the significance of scope resolution operator?
Tell us the use of fflush() function in c language?
List the difference between a "copy constructor" and a "assignment operator"?
What does the characters “r” and “w” mean when writing programs that will make use of files?
a number whose only prime factors are 2,3,5, and 7 is call humble number,,write a program to find and display the nth element in this sequence.. sample input : 2,3,4,11,12,13, and 100.. sample output : the 2nd humble number is 2,the 3rd humble number is 3,the 4th humble number is ,the 11th humble number is 12, the 12th humble number is 14, the 13th humble number is 15, the 100th humble number is 450.
#include
what is stack , heap ,code segment,and data segment
how to execute a program using if else condition and the output should enter number and the number is odd only...
which of the following is not a character constant a) 'thank you' b) 'enter values of p, n ,r' c) '23.56E-o3' d) all of the above
What is the general form of a C program?
What are pointers really good for, anyway?
using for loop sum 2 number of any 4 digit number in c language
i got 75% in all semester am i eligible for your company
How can I swap two values without using a temporary?
Explain continue keyword in c