C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf
Hi! Thanks for posting this code! Could you explain why you subtract 48 from each array element, and then do the %10 for temp, and /10 for r? What do temp and r represent?
we subtract 48 to get the ascii for each digit. as we are using char array. And here r is remainder to carry over to the next element & temp[MAX] is a array for temporary calculations.
ASCII value of character '0' is 48, '1' is 49 and so on. So if we will subtract 48 from it will become equivalent numeric value from character string. For example: '0' - 48 = 0 '1' - 48 = 49-48 = 1 '9' - 48 = 57-48 = 9
In C % is reminder operator while / is division operator. For example: 10 % 3 = 1 10 / 3= 3
I wrote another program and if i do a[i] - 48....i'm getting weird characters of hearts and shapes. Not numbers. But i input abc...i get 1 2 3. Is it cuz of my input keyboard layout?
Won't work. What if carry results in two or even more digits. Say, if there are 1000 digits numbers and each have digit '9'. So in that case, resultant array will cause overflow causing segmentation fault due to carry generated.
#include #include #include #define max 1000 int main() { char A[max],B[max]; int i,j,m,n,k,a,b,c,l; int *r=(int *)calloc(max,sizeof (int)); printf("enter the two large number's \n'"); scanf("%s %s",&A,&B);
There is a problem with the above code. When we multiply any two numbers of the form 10^a with 10^b (For example: 100 * 100 = 010000) then the product of the two numbers is preceded by an extra zero
19 comments:
Hi! Thanks for posting this code! Could you explain why you subtract 48 from each array element, and then do the %10 for temp, and /10 for r? What do temp and r represent?
good code , very interesting.
Hey @above this is just simple calculation as we do on paper.
thank you for the code
we subtract 48 to get the ascii for each digit. as we are using char array. And here r is remainder to carry over to the next element & temp[MAX] is a array for temporary calculations.
what does la+lb+2 signify?and for loops after that.?
Can u please clarify what happens after that!
why if(i <= la+j) and y += j + la + 1; is used?
Can u please clarify
ASCII value of character '0' is 48, '1' is 49 and so on. So if we will subtract 48 from it will become equivalent numeric value from character string. For example:
'0' - 48 = 0
'1' - 48 = 49-48 = 1
'9' - 48 = 57-48 = 9
In C % is reminder operator while / is division operator. For example:
10 % 3 = 1
10 / 3= 3
thnks for the code....
great code......
I wrote another program and if i do a[i] - 48....i'm getting weird characters of hearts and shapes. Not numbers. But i input abc...i get 1 2 3. Is it cuz of my input keyboard layout?
how will the code modify if use of pointers is avoided??
very difficult logic
yaar it is giving segmentation fault on spoj..:(
Won't work. What if carry results in two or even more digits. Say, if there are 1000 digits numbers and each have digit '9'. So in that case, resultant array will cause overflow causing segmentation fault due to carry generated.
48 is an exceedingly retarded way of writing '0' as it assumes an ASCII character set.
CODE....
#include
#include
#include
#define max 1000
int main()
{
char A[max],B[max];
int i,j,m,n,k,a,b,c,l;
int *r=(int *)calloc(max,sizeof (int));
printf("enter the two large number's \n'");
scanf("%s %s",&A,&B);
for(i=(strlen(B)-1);i>=0;--i)
{
int *p=(int *)calloc(max,sizeof (int));
for(j=(strlen(A)-1),k=0;j>=0;j--,k++)
{
b=(A[j]-48)*(B[i]-48);
b=*(p+k) +b;
*(p+k)=b%10;
*(p+k+1)=b/10;
}
if(*(p+k)==0)
--k;
for(m=(strlen(B)-1)-i,l=0;l<=k;l++,m++)
{
c=*(r+m)+*(p+l);
*(r+m)=c%10;
*(r+m+1)=*(r+m+1)+(c/10);
}
}
if(*(r+m)==0)
--m;
printf("the multiplication of the numbers is :");
for(i=m;i>=0;i--)
printf("%d",*(r+i));
return 0;
}
what about 10 or 11??
can u plz explain the logic for y+=la+1...
10q
There is a problem with the above code. When we multiply any two numbers of the form 10^a with 10^b (For example: 100 * 100 = 010000) then the product of the two numbers is preceded by an extra zero
#include
#include
int main()
{
int n=0,i,j,k,m=0,len,length,temp,toadd=0;
int *first,*second,*f=NULL;
char c[2];
first = (int *)malloc(sizeof(int)*100000);
second = (int *)malloc(sizeof(int)*100000);
printf("enter first number : ");
for(i=0;(c[0]=getchar())!='\n';i++,m++)
first[i] = atoi(c);
printf("enter second number : ");
for(i=0;(c[0]=getchar())!='\n';i++,n++)
second[i] = atoi(c);
first = (int *)realloc(first,sizeof(int)*(n));
second = (int *)realloc(second,sizeof(int)*(m));
length = len = n+m;
f = ( int *)malloc(len*sizeof(int));
for(i=0;i=0;i--,k=--len-1)
for(j=n-1;j>=0;j--)
{
temp = first[j]*second[i];
f[k] += temp % 10;
f[k-1] += f[k]/10 + temp/10;
f[k]%=10;
k--;
}
printf("product is :\n");
if(*(f)==0)
i=1;
else
i=0;
for(i=i;i<length;i++)
printf("%d",f[i]);
free(f);
free(first);
free(second);
return 0;
}
Post a Comment