Variable getting the wrong value? - c

Hello guys I need to make a program in which you input a number n.
It's not specified how large is n going to be so theres no limit.
The output is supposed to be: example n = 123456
123456 - 12345 + 1234 - 123 + 12 - 1 = 112233
1 + 12 + 123 + 1234 + 12345 + 123456 = 137171
I have the first part done, and the second as well but when I am printing the second equation it printing values that are not what I calculated.
here's the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned long int num,sum,num2=0,sum2=0,b=0,c=0,d=0,base;
int a=0;
printf("Enter an integer >=0: ");
scanf("%ld",&num);
c=num;
sum=num;
printf("%ld ",num);
while(num>0)
{
if(a==0)
{
num/=10;
sum-=num;
printf(" - %ld",num);
a=1;
}
else if (a==1)
{
num/=10;
sum+=num;
printf(" + %ld",num);
a=0;
}
}
printf("= %ld\n",sum);
d=c;
printf("d: %ld\n ",d);
while(d>10)
{
b++;
d/=10;
printf("%ld\n",d);
}
printf("b:%lu\n",b);
printf("c: %lu\n",c);
for(b;b>0;b--)
{
base=10^b;
num2=c/base;
if (b==1)
{
printf("%ld",num2);
}
else
{
printf("%ld + ",num2);
}
sum2+=num2;
}
printf("= %ld",sum2);
return 0;
}
I know I have extra values and that I'm printing others that are not what I state as needed, I'm just checking what values are incorrect. I think I'm getting the wrong values for num2=c/base; because of how I'm printing it or because of the variable type, I'm trying with the number 5005005, num2 the first time should be 5 and I am getting 417k ish. Any help is appreciated.
edit: I changed the power error, but now the program is crashing.
new code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
unsigned long int num,sum,num2=0,sum2=0,b=0,c=0,d=0,base;
int a=0;
printf("Enter an integer >=0: ");
scanf("%ld",&num);
c=num;
sum=num;
printf("%ld ",num);
while(num>0){
if(a==0){
num/=10;
sum-=num;
printf(" - %ld",num);
a=1;
} else if (a==1){
num/=10;
sum+=num;
printf(" + %ld",num);
a=0;
}
}
printf("= %ld\n",sum);
d=c;
printf("d: %ld\n ",d);
while(d>10){
b++;
d/=10;
printf("%ld\n",d);
}
printf("b:%lu\n",b);
printf("c: %lu\n",c);
for(b;b>=0;b--){
base= powl(10,b);
num2=c/base;
if (b==0){
printf("%ld",num2);
}else{
printf("%ld + ",num2);
}
sum2+=num2;
printf("%ld",sum2);
}
printf("= %ld",sum2);
return 0;
}
edit2: fixed it, I still don't know why it crashes when using >=0
here's the fix:
for(b;b>0;b--){
base= powl(10,b);
num2=c/base;
if (b==1){
printf("%ld + ",num2);
sum2+=num2;
num2=c;
printf("%ld",num2);
}else{
printf("%ld + ",num2);
}
sum2+=num2;
printf("%ld",sum2);
}

You cannot calculate power via ^ operator: it is xor operator.
d>10 will lead to wrong result when the top two digit of input is 10.
Inclemented b because the last number lacked.
Fixed code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned long int num,sum,num2=0,sum2=0,b=0,c=0,d=0;
int a=0;
printf("Enter an integer >=0: ");
scanf("%ld",&num);
c=num;
sum=num;
printf("%ld ",num);
while(num>0){
if(a==0){
num/=10;
sum-=num;
printf(" - %ld",num);
a=1;
} else if (a==1){
num/=10;
sum+=num;
printf(" + %ld",num);
a=0;
}
}
printf("= %ld\n",sum);
d=c;
printf("d: %ld\n ",d);
while(d>=10){
b++;
d/=10;
printf("%ld\n",d);
}
printf("b:%lu\n",b);
printf("c: %lu\n",c);
for(num2=c,b++;b>0;b--){
if (b==1){
printf("%ld",num2);
}else{
printf("%ld + ",num2);
}
sum2+=num2;
num2/=10;
}
printf("= %ld",sum2);
return 0;
}
UPDATE:
To get 1 + 12 + 123 + 1234 + 12345 + 123456,
you can use base to calculate the numbers.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned long int num,sum,num2=0,sum2=0,b=0,c=0,d=0,base=1;
int a=0;
printf("Enter an integer >=0: ");
scanf("%ld",&num);
c=num;
sum=num;
printf("%ld ",num);
while(num>0){
if(a==0){
num/=10;
sum-=num;
printf(" - %ld",num);
a=1;
} else if (a==1){
num/=10;
sum+=num;
printf(" + %ld",num);
a=0;
}
}
printf("= %ld\n",sum);
d=c;
printf("d: %ld\n ",d);
while(d>=10){
b++;
d/=10;
base*=10;
printf("%ld\n",d);
}
printf("b:%lu\n",b);
printf("c: %lu\n",c);
for(b++;b>0;b--){
num2=c/base;
if (b==1){
printf("%ld",num2);
}else{
printf("%ld + ",num2);
}
sum2+=num2;
base/=10;
}
printf("= %ld",sum2);
return 0;
}

Related

how do i make non-numbers into i= form?

Ok, so I have a code that stops only at 0. But wanted to make it so that it will stop when it hit's any non-number text. Could anyone help me turning != 0 into non numbers?
The code for this is:
#include <stdio.h>
int main(){
float sum = 1;
float new_number;
scanf("%f", &new_number);
while (new_number != 0){
sum += new_number;
scanf("%f", &new_number);
}
printf("Sum: %f\n", sum);
return 0;
}
Just check the return value of scanf
#include <stdio.h>
int main(){
float sum = 1;
float new_number;
while (scanf("%f", &new_number) > 0) {
sum += new_number;
}
printf("Sum: %f\n", sum);
return 0;
}

program to find the largest and smallest among three entered numbers and also display whether the identified largest/smallest number is even or odd

This is my homework and i am stuck with how should i identify that the smallest/largest number is even or odd.
#include <stdio.h>
void main()
{
int num1,num2,num3;
printf("Enter three numbers\n");
scanf("%d %d %d",&num1,&num2,&num3);
if(num1<num2 && num1<num3){
printf("\n%d is the smallest",num1);
}
else if(num2<num3){
printf("\n%d is the smallest",num2);
}
else{
printf("\n%d is the smallest",num3);
}
if(num1>num2 && num1>num3){
printf("\n%d is largest",num1);
}
else if(num2>num3){
printf("\n%d is largest",num2);
}
else{
printf("\n%d is largest",num3);
}
getch();
return 0;
}
Use 2 variables, one to store the smallest, one to store the largest.
int min, max;
Then, assign the variable :
if (num1 < num2)
min = num1;
if (num3 < min)
min = num3;
printf("%d is the largest number", min);
To know if a number is odd or even, the remainder (also called modulo) of its division by 2 will be 0 (for even) or 1 (for odd) :
int modulo = min % 2;
if (modulo == 0)
printf("%d is even", min);
else
printf("%d is odd", min);
/*Write a program to find the largest and smallest among three entered numbers and
also display whether the identified largest/smallest number is even or odd*/
#include <stdio.h>
#include <conio.h>
void main()
{
// start the programme
int a, b, c, large, small;
printf("Enter three numbers : \n");
scanf("%d%d%d", &a, &b, &c);
if (a > b && a > c)
{
printf("\n%d is largest", a);
large = a;
}
else if (b > c)
{
printf("\n%d is largest", b);
large = b;
}
else
{
printf("\n%d is largest", c);
large = c;
}
if (a < b && a < c)
{
printf("\n%d is smallest", a);
small = a;
}
else if (b < c)
{
printf("\n%d is smallest", b);
small = b;
}
else
{
printf("\n%d is smallest", c);
small = b;
}
if (large % 2 == 0)
{
printf("\n %d is even", large);
}
else
{
printf("\n %d is odd", large);
}
if (small % 2 == 0)
{
printf("\n %d is even", small);
}
else
{
printf("\n %d is odd", small);
}
getch();
// end the programme
}

How to write a series using C for loop?

I am having trouble with writing code for a series, I believe it is something with my if statement but I am stumped. The series is supposed to be
but I keep getting the wrong output. This is my code:
#include <stdio.h>
int n,t=1,nextTerm,sum=0,i;
int main() {
printf("Enter an integer number:");
scanf("%d",&n);
for(i=1;i<=n;i++) {
if (t%2 == 0) {
nextTerm = 1;
}
else {
nextTerm = -1;
}
t=nextTerm*(t*t);
sum=sum+t;
}
printf("The value of the series is: %d\n",sum);
return (0);
}
You have to check i+1 instead of i and replace t*t with i*i. You don't need any extra variable like t.
#include <stdio.h>
int main()
{
int n,nextTerm,sum=0,i;
printf("Enter an integer number:");
scanf("%d",&n);
for(i=1;i<=n;i++) {
if ((i+1)%2 == 0)
nextTerm = 1;
else
nextTerm = -1;
sum=sum+(nextTerm*i*i);
}
printf("The value of the series is: %d\n",sum);
return 0;
}
Series be like this:
1, -4, 9, -16, ...
Output:
Enter an integer number:4
The value of the series is: -10
I don't know if I understood it correctly, it should be something like this?:
#include <stdio.h>
int main() {
int n,t=1,firstPart,sum=0,i;
printf("Enter an integer number:");
scanf("%d",&n);
printf("The serie is: \n");
for(i=1;i<=n;i++) {
if (i%2 == 0) {
firstPart = 1;
}
else {
firstPart = -1;
}
t=firstPart*(i*i);
printf(" %d\t ", t);
sum=sum+t;
}
printf("\nThe value of the series is: %d\n",sum);
return (0);
}
for n = 4, the series is:
-1 / 4 / -9 / 16 /
and the sum is 10
Replace (t*t) with (i*i)
#include <stdio.h>
int main() {
int n,i,sum=0,nextTerm,t;
printf("Enter an integer number:");
scanf("%d",&n);
for(i=1;i<=n;i++) {
if (t%2 == 0) {
nextTerm = 1;
}
else {
nextTerm = -1;
}
t=nextTerm*(i*i);
sum=sum+t;
}
printf("The value of the series is: %d\n",sum);
return (0);
}
Output -
Enter an integer number:4
The value of the series is: -10

Prime factorization, changing output.

Hello guys i have this code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned int num,i,j;
int a=0;
printf("Please input a number: ");
scanf("%d",&num);
printf("The Prime Factorization: ");
for (i=2;i<=num;i++){
if (num%i==0){
a=1;
if (i==num){
printf(" %ld ",num);
} else {
printf(" %ld *",i);
num/=i;
}
}
}
if (a==0){
printf("%ld ",num);
}
return 0;
}
so let's say i input 40,
it gives me
The Prime Factorization: 2 * 4 * 5
this is correct but, how could I make it output the "2 * 4 * 5"
as "2 ^ 3 * 5"?
Since a prime can appear more than once in the factorization you can't just move on to the next candidate without first testing the current prime until the number is no longer divisble by it.
And to get the nice printout you're after, you can keep a count variable as shown below:
#include <stdio.h>
int main(void) {
unsigned int num,i,count;
int a=0;
printf("Please input a number: ");
scanf("%d",&num);
printf("The Prime Factorization: ");
i = 2;
while(num>1){
if (num%i==0){
a=1;
count = 1;
num /= i;
// Exhaust each prime fully:
while (num%i==0) {
count++;
num /= i;
}
printf("%ld",i);
if (count > 1) {
printf(" ^ %ld", count);
}
if (num > 1) {
printf(" * ");
}
}
i++;
}
if (a==0){
printf("%ld ",num);
}
return 0;
}
something like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,i,j,count=0;
int a=0;
printf("Please input a number: ");
scanf("%d",&num);
printf("The Prime Factorization: ");
for (i=2;i<=num;i++){
count=0;
a=0;
while(num%i==0){
a=1;
++count;
num/=i;
}
if(a==1)
{
printf("%d ^ %d *",i,count);
}
}
if (a==0){
printf("%ld ",num);
}
return 0;
}

Recursive C program to differentiation a polynomial until it stops

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[10],i,n,d[10],power;
float in[10];
clrscr();
printf("Enter the order ofthe polynomial\n");
scanf("%d",&n);
for(i=n;i>=0;i--)
{
printf("Enter the co-efficient of x^%d:",i);
scanf("%d",&a[i]);
}
printf("Given polynomial is\n");
for(i=n;i>=0;i--)
{
if(power<0)
{
break;
}
if(a[i]>0)
{
if(i!=n)
printf(" + ");
}
else if(a[i]<0)
printf(" - ");
else
printf(" ");
printf("%dx^%d",abs(a[i]),i);
}
//To find derivative
for(i=n;i>=0;i--)
d[i]=a[i]*i;
printf("\n Derivative of the given polynomial is\n");
for(i=n;i>=1;i--)
{
if(power<0)
{
break;
}
if(d[i]>0)
printf(" + ");
else if(d[i]<0)
printf(" - ");
else
printf(" ");
printf("%dx^%d",d[i],i-1);
}
getch();
}
the above program only calculate the first derivative, but i need a program which calculates all the derivatives
ex 2x^3+2x^2+3x+1
f1= 6x^2+4x+3
f2=12x+4
f3=12
like this i need to modify the program,, but am not getting how to do that,,please help me//
try this
#include <stdio.h>
#include <stdlib.h>
typedef struct polynomial {
int order;
int *coefficient;
} Polynomial;
void init_poly(Polynomial *p){
int i;
printf("Enter the order of the polynomial\n");
scanf("%d", &p->order);
p->coefficient = malloc((p->order + 1)*sizeof(int));
for(i = p->order; i >= 0; --i){
printf("Enter the co-efficient of x^%d:", i);
scanf("%d", &p->coefficient[i]);
}
}
void drop_poly(Polynomial *p){
free(p->coefficient);
}
void print_poly(Polynomial *p){
int i;
for(i = p->order; i >= 0; --i){
if(p->coefficient[i]){
if(p->order != i){
printf(" %c ", p->coefficient[i] < 0 ? '-' : '+');
printf("%d", abs(p->coefficient[i]));
} else
printf("%d", p->coefficient[i]);
if(i)
printf("x^%d", i);
}
}
printf("\n");
}
void differential_poly(Polynomial *p){
int i;
for(i = 0; i < p->order; ++i){
p->coefficient[i] = p->coefficient[i+1] * (i+1);
}
p->coefficient[p->order--] = 0;
}
void r_print_differential_poly(Polynomial *p){
int i=1;
while(p->order){
differential_poly(p);
printf("f%d = ", i++);
print_poly(p);
}
}
int main(void){
Polynomial poly;
init_poly(&poly);
printf("Given polynomial is\n");
print_poly(&poly);
r_print_differential_poly(&poly);
drop_poly(&poly);
return 0;
}

Resources