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
Related
I have this super simple code for calculating averages of given even and odd numbers, until the user gives 0. (I would use for loop but we can't).
I'm having a really strange problem with program rounding results like 25/2 is 2.00000. Sorry if this question is stupid but I just can't find a problem.
What am I doing completely wrong?
#include <stdio.h>
#include <stdlib.h>
void funkcja()
{
int sumaNiep = 0;
int sumaPa = 0;
int userInput = 1;
int i = 0;
while(userInput != 0)
{
//wprow zmiennej
printf("%d. Podaj calkowita liczbe: ", i+1);
scanf("%d", &userInput);
//jesli parzysta
if (userInput % 2 == 0)
{
sumaPa += userInput;
} else {
sumaNiep += userInput;
}
i++;
}
double sredniaNiep = sumaNiep/(i-1);
double sredniaPa = sumaPa/(i-1);
printf("\nsrednia parzysta %d / %d : %lf", sumaPa, i, sredniaPa);
printf("\nsrednia parzysta %d / %d : %lf", sumaNiep, i, sredniaNiep);
}
int main()
{
funkcja();
}
The problem is that you do an integer division at the end.
You should break out of the loop if the user enters 0 and make at least one operand a double when you do the division. You also need to count the number of evens and odds:
#include <stdio.h>
#include <stdlib.h>
void funkcja() {
int sumaNiep = 0;
int sumaPa = 0;
int userInput = 1;
int iPa = 0;
int iNiep = 0;
int i = 0;
while(1) {
printf("%d. Podaj calkowita liczbe: ", ++i);
if(scanf(" %d", &userInput) != 1 || userInput == 0) break; // break out
// jesli parzysta
if(userInput % 2 == 0) {
sumaPa += userInput;
++iPa; // count evens
} else {
sumaNiep += userInput;
++iNiep; // count odds
}
}
if(iPa) { // avoid div by zero
double sredniaPa = (double)sumaPa / iPa; // double div
printf("srednia parzysta %d / %d : %lf\n", sumaPa, iPa, sredniaPa);
}
if(iNiep) { // avoid div by zero
double sredniaNiep = (double)sumaNiep / iNiep; // double div
printf("srednia parzysta %d / %d : %lf\n", sumaNiep, iNiep, sredniaNiep);
}
}
The problem was I divided by the number of all digits (odd and even) to calculate both averages. Here is improved code:
#include <stdio.h>
#include <stdlib.h>
void funkcja()
{
int sumaNiep = 0;
int sumaPa = 0;
int userInput = 1;
int i_p = 0, i_np = 0;
while(userInput != 0)
{
//wprow zmiennej
printf("%d. Podaj calkowita liczbe: ", i_p+i_np+1);
scanf("%d", &userInput);
//jesli parzysta
if (userInput % 2 == 0)
{
sumaPa += userInput;
if (userInput != 0)
{
i_p++;
}
} else {
sumaNiep += userInput;
i_np++;
}
}
if (i_np != 0)
{
double sredniaNiep = sumaNiep/(i_np);
printf("\nSrednia nieparzysta %d / %d : %lf", sumaNiep, i_np, sredniaNiep);
}
if (i_p != 0)
{
double sredniaPa = sumaPa/(i_p);
printf("\nSrednia parzysta %d / %d : %lf", sumaPa, i_p, sredniaPa);
}
}
int main()
{
funkcja();
}
just a newbie in C here. I'm trying to print out the factorial given the input using recursion & pointers, but when my input is 5, the output was 2293620. Can somebody help me with this? I'm not sure where did that number come from, because factorial of 5 should give me 120. Thanks for your help!
#include<stdio.h>
int countlength(int *num) {
int x = 1;
if (*num == 1) {
return 1;
} else {
return *num * countlength(num-1);
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(&n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}
Remove references from your code.
#include<stdio.h>
int countlength(int num) {
int x = 1;
if (num == 1) {
return 1;
} else {
return num * countlength(num-1);
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}
In this case, I think you don't need to use pass by reference. You should use pass by value. Then your code will work perfectlyly
Change this line:
return num * countlength(num - 1);
to this:
int val = *num - 1;
return *num * countlength(&val);
So you correctly assign the value and pass it as a pointer.
int countlength(int *num) {
int x = *num - 1;
if (*num == 1) {
return 1;
} else {
return *num * countlength(&x);
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(&n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}```
**Try this**
int countlength(int *num) {
if (*num == 1) {
return 1;
} else {
return *num * countlength(&(*num - 1));
}
}
int main() {
int n, l;
printf("Enter number: ");
scanf("%d", &n);
l = countlength(&n);
printf("The factorial of %d is %d\n", n, l);
return 0;
}
try this
I am learning about functions and how to call upon them and use them in class. I don't quite understand where I've gone wrong here. I know that there are some mistakes around the int main part. I have asked my teacher and he is reluctant on giving me an example that would solve my problems or help me out. I think my main problem is at factorial_result = factorial();
#include <stdio.h>
void mystamp(void)
{
printf("My name is John Appleseed\n");
printf("My lab time is 12:30 on Sunday\n");
return;
}
int getnum(void)
{
int local_var;
printf("Please enter an integer: ");
scanf("%d%*c", local_var);
return(local_var);
}
int factorial(void)
{
int x,f=1,local_var;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial();
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
Declare local_var as a global variable and do:
local_var = getnum();
OR
Change main() to:
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial(result);
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
And factorial() to:
int factorial(int n)
{
int x,f=1,local_var=n;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
Your factorial should be calculated based on the input( i.e in your case int result ).
So, your method factorial() should looks as follows :
int factorial( int number )
{
int factorial_value = 1;
while( number > 0 )
{
factorial_value *= number;
number--;
}
return factorial_value;
}
Then, the correct factorial would be returned and printed accordingly ! Regarding the scope of the variables that you have used, see the comments under your question.
#include <stdio.h>
int factorial(int);
int main()
{
int num;
int result;
printf("Enter a number to find it's Factorial: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}
This is a simple factorial program using recursion calling function !
include
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate its factorial\n"); scanf("%d", &n);
for (c = 1; c <= n; c++) fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
Hi managed to make my program checked if i have repeated 8's. I also want to print out how many repeated 8's i have stored in the array.
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
bool digit_seen[10] = {false};
int digit=0;
long n;
printf("Enter number: ");
scanf("%ld", &n);
while (n>0)
{
digit = n % 10;
if (digit_seen[digit])
{
break;
}
digit_seen[digit] = true;
n/=10;
}
if (n>0 && digit ==8)
{
printf("Repeated 8's");
}
else
{
printf("No 8's found");
}
return 0;
}
If I understand your problem, you want to know the number of occurences a 8 is in your number. Like 88 has 2 8s. If that's the case, I don't see why you use a boolean array. First, you need a counter. Second, you need to know if digit is 8 for every digit and increment this counter if that's an 8. Here's an example :
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int digit=0;
long n;
int counter = 0;
printf("Enter number: ");
scanf("%ld", &n);
while (n>0)
{
digit = n % 10;
if(digit == 8)
{
counter++;
}
n/=10;
}
if (counter > 0)
{
printf("Repeated 8's");
}
else
{
printf("No 8's found");
}
return 0;
}
In this example, counter would have the number of occurrences of 8s in your number. Just display it in the printf and it's done.
EDIT : here's a solution using an array:
#include <stdio.h>
int main(void)
{
int digit = 0;
long n;
int arrayNumber[10] = {0};
printf("Enter number: ");
scanf("%ld", &n);
while (n>0)
{
digit = n % 10;
arrayNumber[digit]++;
n /= 10;
}
if (arrayNumber[8] > 0)
{
printf("Repeated 8's");
}
else
{
printf("No 8's found");
}
return 0;
}
This way, you would know occurrence of every number from 0 to 9 in your integer. I'd also point out that you need to define what is a repeated number. If it's when there's at least 2 occurrence, you need to change arrayNumber[8] > 0 by arrayNumber[8] > 1
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;
}