I have written a code in c for long number multiplication but output is not being displayed on the IDE. Please can you point out the error in the given code. Also which language is more efficient for solving these type of problems?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
void main()
{
char ac[MAX];
char bc[MAX];
int a[MAX],b[MAX];
int mul[MAX];
int c[MAX];
int temp[MAX];
int la,lb;
int i,j,k=0,x=0,y;
long int r=0;
long int sum = 0;
la=strlen(ac)-1;
lb=strlen(bc)-1;
printf("Enter the first number : ");
scanf("%s",ac);
printf("Enter the second number : ");
scanf("%s",bc);
for(i=0;i<=la;i++){
a[i] = ac[i] - 48;
}
for(i=0;i<=lb;i++){
b[i] = bc[i] - 48;
}
for(i=lb;i>=0;i--){
r=0;
for(j=la;j>=0;j--){
temp[k++] = (b[i]*a[j] + r)%10;
r = (b[i]*a[j]+r)/10;
}
temp[k++] = r;
x++;
for(y = 0;y<x;y++){
temp[k++] = 0;
}
}
k=0;
r=0;
for(i=0;i<la+lb+2;i++)
{
sum =0;
y=0;
for(j=1;j<=lb+1;j++){
if(i <= la+j){
sum = sum + temp[y+i];
}
y += j + la + 1 ;
}
c[k++] = (sum+r) %10;
r = (sum+r)/10;
}
if (r==1)
{
c[k]=r;
}
j=0;
for(i=k-1;i>=0;i--){
mul[j++]=c[i];
}
for(i=0;i<j;j++)
{
printf("%d",mul[i]);
}
}
You calculate the length of the input strings before assigning the strings:
la=strlen(ac)-1;
lb=strlen(bc)-1;
printf("Enter the first number : ");
scanf("%s",ac);
printf("Enter the second number : ");
scanf("%s",bc);
If you instead do it the other way around the program actually does something:
printf("Enter the first number : ");
scanf("%s",ac);
printf("Enter the second number : ");
scanf("%s",bc);
la=strlen(ac)-1;
lb=strlen(bc)-1;
Your second problem is in the last part of your code:
for(i=0;i<j;j++)
{
printf("%d",mul[i]);
}
You're incrementing j instead of i, it should be:
for(i=0;i<j;i++)
{
printf("%d",mul[i]);
}
Another little thing, this isn't really clear what it does to the uninitiated:
a[i] = ac[i] - 48;
if you write it like this it's easier to understand:
a[i] = ac[i] - '0';
Use remove those la and l assignment and put it after scanning the strings like below.
printf("Enter the first number : ");
scanf("%s",ac);
printf("Enter the second number : ");
scanf("%s",bc);
la=strlen(ac)-1;
lb=strlen(bc)-1;
for(i=0;i<=la;i++){
a[i] = ac[i] - 48;
}
for(i=0;i<=lb;i++){
b[i] = bc[i] - 48;
}
Even in the last part of your code there is an error.Inside the loop you have used for(i=0;i<j;j++) replace it with for(i=0;i<j;i++).
I would also suggest you to change the size of temp array to 2*MAX+2.Because suppose somebody enters a number of 8000 digits then temp wont be able to store these many digits if the size is restricted to MAX only.
you can use java and python languages for doing same. In Java there is separate class known as BigInteger.
Scanner sc=new Scanner(System.in);
BigInteger b1,b2,b3;
String num1,num2;
System.out.println("Enter the first integer");
num1=sc.next();
System.out.println("Enter the second integer");
num2=sc.next();
b1=new BigInteger(num1);
b2=new BigInteger(num2);
b3=b1.multiply(b2);
System.out.println("The product of "+b1+" and "+b2+" is "+b3);
Related
i am new to programing, i want to know that how we can find the odd digits in a number.
the condition in this program is we should only use concept of arrays.I tried a code for this as follows:
#include <stdio.h>
int main()
{
int A[50],i,x,y,n,sum=0;
scanf("%d",&n);
printf("the value is %d\n",n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
for(i=0;i<n;i++){
x=A[i]%10;
if(x%2!=0)
sum=sum+x;
A[i]=A[i]/10;
printf("the sum of odd numbers is %d\n",sum);
}
return 0;
}
but in this the code is checking for only one digit of the first number in the loop and then next time it is going to check the digit of second number.
so, i need to write a code to check all digits in the number and then it goes to next number and check all digits and should continue the same process in the loop.So, for this how should i modify my code?
You were missing a loop that would iterate through every digit of A[i] - the inner while loop below,
#include <stdio.h>
int main()
{
int A[50], i, x, y, n, sum=0;
printf("How many numbers will you input?\n");
scanf("%d",&n);
printf("the value is %d\n",n);
for(i=0; i<n; i++) {
scanf("%d",&A[i]);
}
for(i=0; i<n; i++) {
sum = 0;
while (A[i] > 0) {
x = A[i]%10;
if(x%2 != 0) {
sum = sum + x;
}
A[i] = A[i]/10;
}
printf("the sum of odd numbers is %d\n",sum);
}
return 0;
}
The exact algorithm for iterating through each digit in a nice form can be found in this post - although for a different language. Here, apart from the while loop, you also need to reset the sum each time unless you want a cumulative sum over all provided numbers.
Note that I changed the formatting a bit - more space, extra braces, and a message about what you're prompting the user to input.
int temp;
int sum = 0;
temp = number;
do {
lastDigit = temp % 10;
temp = temp / 10;
sum += (lastDigit %2 != 0) ? lastDigit : 0;
} while(temp > 0);
I'm taking CS courses, and wrote a simple program to find an average of the given (input) amount of inputs.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n = get_int("Please enter the number of scores: ");
int scores = [n];
for(int i=0; i<n; i++)
{
int scores[i] = get_int("Please, enter the score: ");
}
printf("Average: %f\n", average(n, scores));
}
float average(int length, int array[])
{
int sum = 0;
for(int i = 0; i < length; i++)
{
sum += sum + array[i];
}
return sum / (float)length;
}
When compiling, "expected expression" error occurs on line 10 (i.e. {int scores = [n];} —
any help or suggestions would be appreciated!!
This is invalid syntax:
int scores = [n];
If you want to define scores as an array of size n you want:
int scores[n];
Also, this doesn't do what you might expect:
int scores[i] = get_int("Please, enter the score: ");
This creates an array called scores of size i, masking the array defined previously, and attempts to initialize it with a single value instead of a list of values. If you want to assign to the existing array you want:
scores[i] = get_int("Please, enter the score: ");
I am writing a calculator in C, but it will take input like 1+2*9/5.
I am not an expert in C, so I thought I can ask the user how many numbers he has (for example let's say 4), then asking 3 times "enter the number, enter operator" and 1-time "enter number". Then putting all inputs to an empty array respectively then doing the operations by looping through the array. But of course, I've got problems.
#include <stdio.h>
int main(void) {
int how_many;
printf("How many numbers do you have to calculate?");
scanf("%d", &how_many);
int number_entry = how_many; // for example 1+2*9/5 has 4 numbers
int operator_entry = how_many - 1; // and 3 operators
int number_in;
char operator_in;
int all_inputs[(number_entry + operator_entry)];
for (int j = 0; j < operator_entry; j++) {
printf("Enter the number : ");
scanf("%d", &number_in);
all_inputs[j] = number_in;
printf("\n");
printf("Enter the operation as + for add, - for substract, * for "
"multiply and / for division");
scanf(" %c", &operator_in);
all_inputs[j + 1] = operator_in;
}
printf("Enter the number : ");
scanf("%d", &number_in);
all_inputs[operator_entry + 1] = number_in;
for (int k = 0; k < (2 * how_many) - 1; k++)
printf("%s\n", all_inputs[k]);
return 0;
}
As you can see from the code, looping through the code to append inputs into the array is not working because I am doing
j=0
append a number to all_inputs[0]
append operator to all_inputs[0+1]
j=1
Now, the operator will be replaced with a number.
And another problem is, it shows "enter a number" just once and then loops the "enter the operator".
ss for problem
If you know any other way to do this, please let me know. Again, I am not a pro in C.
When the first number is 1 and second number is 2, and the length is 5, it should be 1 2 3 5 8. But then my output is always 1 2 1 3 4. I can't seem to find the problem.
Another input is 2 and 5. Output is 2 5 1 6 7. The 3rd number which is 1 shouldn't be there. What should I change or add?
*This is already a submitted HW and yes its wrong I got the deductions already. Now I just want to fix this so I can study this.
int main()
{
int i, lenght = 0, fib, sum, sum1, sum2, a, b, c;
printf("\nFirst number: ");
scanf("%d", &a);
printf("\nSecond number: ");
scanf("%d", &b);
printf("\nHow long?: ");
scanf("%d", &lenght);
{
while ((a > b) || ((lenght < 2) || (lenght > 100)))
{
printf("\nFirst number: ");
scanf("%d", &a);
printf("\nSecond number: ");
scanf("%d", &b);
printf("\nHow long?: ");
scanf("%d", &lenght);
}
}
printf("%d\t%d\t", a, b);
printf("%d\t", fib);
for (i = 3; i < lenght; i++) {
if (i <= 1) fib = i;
else {
a = b;
b = fib;
fib = a + b;
}
printf("%d\t", fib);
}
}
The first time you print fib (before the for loop), you haven't assigned it anything yet.
Since this is for study, issues with your code: you don't need to duplicate the calls to scanf(), simply initialize one of the variables to fail (which you did: lenght = 0) and let the loop do its thing; pick one indentation style and stick with it; if you're new to C, always include the curly braces, even when the language says they're optional; you (correctly) allow for a length of 2, but then print three numbers; your if (i <= 1) clause is a no-op as the loop starts with for (i = 3; so i is never less than 3.
Putting it all together, we get something like:
#include <stdio.h>
int main() {
int length = 0, a, b;
while (length < 2 || length > 100 || a > b ) {
printf("\nFirst number: ");
(void) scanf("%d", &a);
printf("\nSecond number: ");
(void) scanf("%d", &b);
printf("\nHow long?: ");
(void) scanf("%d", &length);
}
printf("%d\t%d\t", a, b);
for (int i = 2; i < length; i++) {
int fib = a + b;
printf("%d\t", fib);
a = b;
b = fib;
}
printf("\n");
return 0;
}
Note that the input error checking isn't sufficient to prevent problems. E.g. b can be greater than a, but still mess up the sequence if you input random numbers. You're assuming the user knows to put in two adjacent items from fibonacci sequence which is tricky to test.
just add fib=a+b; before printing fib value.
Its a good coding habit to initialize all variable before using it(especially in C).
Your code seems strange to me. I tried to simplify your code a bit. See this once:
int main()
{
int a,b,next,last,i;
printf("Enter the first Value:");
scanf("%d",&a);
printf("Enter the second Value:");
scanf("%d",&b);
printf("Enter the length of Fab. series:");
scanf("%d",&last);
printf("%d,%d,",a,b);
for (i=3; i<= last; i++)
{
next = a + b;
if(i<last)
printf("%d,",next);
else
printf("%d",next);
a = b;
b = next;
}
return 0;
}
Hope it's Helpful!!
I am trying to learn C and here i got a program in which we have to take the input from the user as n number os strings, compare it and arrange it in a alphabetical order. After arranging them in a alphabetical order , i have to only print the last name which was occurring in the order.
Here is the code for the above problem:
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,m,n,len;
char a[50][50],temp[100];
char last ;
printf("Enter the number of elements you wish to order : ");
scanf("%d",&m);
printf("\nEnter the names :\n");
for (i=0;i<m;i++){
scanf("%s",a[i]);
}
for (i=0;i<m;i++){
for (j=i+1;j<m+1;j++) {
if (strcmp(a[i],a[j])>0) {
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
}
printf("\n\nSorted strings are : ");
for (i=0;i<m+1;i++){
printf("%s \n",a[i]);
}
return 0;
}
~
The Answer goes this way:
Enter the number of elements you wish to order : 4
Enter the names :
territory
states
hello
like
Sorted strings are : S$???
hello
like
states
territory
My question goes that why am i getting "S$???" and i want only the last word "territory should be printed out not all the names".
Can anyone let me know where am i going wrong? It will be a great help.
Thanks
Tanya
You are sorting m+1 strings with indexes [0..m]. But you input only m strings.
Your indexes should never go above m-1.
\Check this code
In your code you get input as 4
then
a[0]=territory
a[1]=states
a[2]=hello
a[3]=like
But your code runs upper loop at most 3 time then i=3
In next loop j=i+1 then j=4
but a[4]=not exists
Thats why "S$???" occurs
Solution:
#include<stdio.h>
#include<string.h>
int main() {
int i, j, m, n, len;
char a[50][50], temp[100];
char last;
printf("Enter the number of elements you wish to order : ");
scanf("%d", &m);
printf("\nEnter the names :\n");
for (i = 0; i < m; i++) {
scanf("%s", a[i]);
}
for (i = 0; i < m - 1; i++) { // m - 1 enough maximum i = 2;
for (j = i + 1; j < m; j++) { // j maximum j = i + 1 j = 3
if (strcmp(a[i], a[j]) > 0) {
strcpy(temp, a[i]);
strcpy(a[i], a[j]);
strcpy(a[j], temp);
}
}
}
printf("\n\nSorted strings are : "); // print all words in sorted order
for (i = 0; i < m; i++) {
printf("%s \n", a[i]);
}
printf("Last Word:\n");
printf("%s\n", a[m - 1]); // a[3] contains last name
return 0;
}
this was my program it worked for me in turbo c++
#include<conio.h>
#include<iosream.h>
#include<ctype.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
clrscr();
char name[100];
int c=0;
// to take the name including spaces
gets(name);
//this line is to print the first character of the name
cout<<name[0];
//this loop is to print the fist character which is there after every space
for(int l=0;name[l]!='\0';l++)
{
if(name[l]==' ')
{
cout<<"."<<name[l+1];
//l+1 is used because l is the space and l+1 is the character that we need
}
}
//this loop is to find out the last space in the entire sting
for(int i=0;name[i]!='\0';i++)
{
if(name[l+1]==NULL)
{
//here we fond the last space in the string
for(int j=i;name[j]!=' ';j--)
{
c=j+1;
}
// c=j+1 is used bacause we have already taken the first letter of the that word
//no need of taking it again
}
}
//this loop starts from the last space and the position(of space) is stored in k
for(int k=c;name[k]!='\0';k++)
{
cout<<name[k];
}
getch();
}
output:
anentt ranjan shukla
a.r.shukla