Function won't print anything back [duplicate] - c

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 2 years ago.
#include<stdio.h>
#include<math.h>
float ar(int a);
int main()
{
int a;
scanf("%d",&a);
printf("%.2f",ar(a));
return 0;
}
float ar(int a)
{
int i,br=0,uk=0;
float ar;
for(i=0;i<=a;i++)
{
if(a%i==0)
{
br++;
uk=uk+i;
}
}
ar=uk/br;
return ar;
}
I'm trying to return the arithmetic value of all of the divisors of the number I enter.
Why isn't anything getting printed back?
#include<stdio.h>
#include<math.h>
int razlika(int a);
int main(){
printf("%d",razlika(26931));
return 0;
}
int razlika(int a)
{
int i,min=10000000,max=0,br=0,p,z;
do{
a=a/10;
br++;
}
while (a!=0);
for(i=0;i<br;i++)
{
p=a%10;
a=a/10;
if(p<min) min=p;
if(p>max) max=p;
}
z=max-min;
return z;
}
In this one im supposed to find the difference between the largest and the smallest digit of the number but it always prints out 0. I think it is because of the do while loop where i think i turn my number into 0? But I dont know how to count the number of the digits without making that mistake.

In your first program, your loop starts from 0, and you cant divide by zero.
As to your second program, you are correct, you changed the number to zero so you cant use it in the second loop, simply create a temporary variable and set it = a, use it in the do- while loop instead of a itself.
also in the do-while loop, let the condition be while (a>0). Help this helps!

Related

How to print out each digit of a number by place values in ascending order [duplicate]

This question already has answers here:
Getting each individual digit from a whole integer
(10 answers)
Closed 1 year ago.
i need to create scanf function that accepts random positive integer and using while loop print out each digit of the inputted integer in separate lines, starting from its rightmost digit until the leftmost digit of the number.
input sample: 214
out put sample:
4
1
2
this is my code so far:
#include<stdio.h>
int main(){
int num;
int i;
scanf("%d",&num);
for(i=0; num>i; i++){
if(num>=i){
i = i%10;
printf("%d", i);
}
}
return 0;
}
edit:
i finally got the code
#include<stdio.h>
int main() {
int num;
int reminder;
int rev=0;
int count=1;
scanf("%d", &num);
while(num!=0)
{
reminder=num%10;
rev=rev*10+reminder;
num/=10;
printf("%d\n",(reminder%10));
count++;
}
return 0;
}
You have to keep on dividing the number by 10 to discard the last digit once you have printed it out using % operator.
For loop could look like:
for(i=num; i>0; i/=10)
{
print("%d",(i%10));
}

how do i add all the elements of the for loop?

#include <stdio.h>
int even_numbers_sum(){
int d;
for(d=0;d<=100;d++){
if(d%2==0){
printf("\n this is even number loop %d",d);
}
}
int sum;
sum=sum+d;
printf("\n the value of total sum of the loop %d",sum);
return sum;
}
int main(){
even_numbers_sum();
}
my main aim is to calculate the sum of all the elemets of for loop i took a for loop and described its value and put a equation (if) to pass anynumber if its only even other wise it will not work and i have to add all those even number till 100 how do i do it
Initialised sum to 0 at the beginning. Changed the return type of the function to void as int was unnecessary. Sum has to be incremented in the if statement.
#include <stdio.h>
void even_numbers_sum(){
int d;
int sum=0;
for(d=0;d<=100;d++){
if(d%2==0){
printf("\n this is even number loop %d",d);
sum=sum+d;
}
}
printf("\n the value of total sum of the loop %d",sum);
}
int main(){
even_numbers_sum();
}
look the following code. I've written this code for any number if you want to change the upper bound instead of 100 , you just have to pass the integer to the function i.e. if you want to get the sum of all even integers from 0 to 100, you have to pass 100 while calling the function.
#include <stdio.h>
void even_numbers_sum(int i){
int d;
int sum=0;
for(d=0;d<=i;d+=2){
printf("\n this is even number loop %d",d);
sum=sum+d;
}
}
printf("\n the value of total sum of the loop %d",sum);
}
int main(){
even_numbers_sum(100);
return 0;
}
1.first change is here in function declaration and definition you have to write int i in the parentheses.
void even_numbers_sum(int i)
2.Secondly there is no need to put the condition if you want to start from 0. it can be done by simply increment d by 2.
for(d=0;d<=i;d+=2)
3.Your main method has return type int so there should be some integer returned.
return 0;
I hope this will help you to solve your problem. If there is anything which you want to ask I'll be glad to help you.
Firstly, you should initialize sum as 0 at begin. sum may be not 0 without initialization.
Secondly, in your code, sum will be only add with the final even number (100), not add each each even number from 0 to 100.
A correct example as below:
#include <stdio.h>
int even_numbers_sum()
{
int d, sum = 0; // initialize sum as 0
for (d = 0; d <= 100; d++) {
if (d % 2 == 0) {
printf("\n this is even number loop %d", d);
sum = sum + d; // add each even number to sum
}
}
// sum=sum+d; // This line code will add final even number and sum
printf("\n the value of total sum of the loop %d", sum);
return sum;
}
int main()
{
even_numbers_sum();
}

c program to calculate value of e^x, correct upto 10 decimal places

I am writing a program that calculates the value of e^x, according to the expansion formula.I need to print the answer so that it is correct upto 10 decimal places. I have tried to do it using, for, while and do while loop , however I cannot figure out where to terminate the loop , which condition to use to terminate the loop. i have written the code as follows:
#include<stdio.h>
#include<math.h>
int factorial(int x);
int main()
{
int x,n;
float sum,d_1;
printf("Enter the value of power :");
scanf("%d",&x);
n=1;sum=1;
while(sum <= %.10f)
{
d_1=pow(x,n)/factorial(n);
sum=sum+d_1;
n++;
}
printf("Answer is %f",sum);
return 0;
}
int factorial(int y)
{
int fact=1,i;
for(i=1;i<=y;i++)
{
fact=fact*i;
}
return(fact);
}
and i am getting the error message expected expression before %.
Please help.
You want to terminate the loop when the current term is smaller than some value. Also you want the loop to execute at least once, so a do-while loop is preferable:
n = -1;
do {
n++;
d_1 = pow(x, n) / factorial(n);
sum = sum + d_1;
} while (d_1 > epsilon);
As mentioned in the comments, there are other issues with the code when it comes to precision and overflow but that is a different story.

Fibonacci sequence while loop

I have to write code that displays the Fibonacci sequence to the user desired number of terms and must also use a while loop. I'm not sure why this code isn't working.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int max;
printf("Enter the max term of the Fibonacci Sequence:\n");
scanf("%i", &max);
int a=0;
int b=0;
a=2;
while(a<max) {
if((a==0||a==1))
{
printf("%i\n", &a);
++a;
}
else if(a>1)
{
a=(a-1)+(a-2);
printf("%i\n", &a);
++a;
}
}
return 0;
}
You can try this.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int max;
printf("Enter the max term of the Fibonacci Sequence:\n");
scanf("%i", &max);
int n=0;
int a=0;
int b=1;
int next;
while(n<max) {
if ( n <= 1 )
{
next = n;
n++;
}
else
{
next = a + b;
a = b;
b = next;
n++;
}
printf("%d\n", next);
}
return 0;
}
issues with your code:
following declaration & initialisation, you set a=2 => it won't take the true branch of the if statement -- '0' will not be printed in your result.
a=(a-1)+(a-2); i.e a = 1
then you are doing ++a; => a == 2. thus it again else statement with same a==2.
hence it will print the same value and loop executes infinitely.
In the very beginning of your program (before the while loop) a is 2 (see the line a=2).
And in the while loop you do following:
a=(a-1)+(a-2); // a = 2-1+2-2 i.e. a = 1
and right after it
++a; // a == 2
So, after it a==2 again. This loop never ends.
But it is technical problem. More important is that you are trying to calculate not Fibonacci Sequence. In the Fibonacci Sequence each subsequent number is the sum of the previous two. But in your code there is adding of not two previous numbers of Fibonacci Sequence, but previous two Natural numbers.
You have variable b, because someone told you to add it. And it was right! Just remember previous found element of the Fibonacci Sequence in b. When you know previous one element and current one, it is possible to calculate next one.

Debugging a recursive function

#include<stdio.h>
void binary(int n)
{
int bin=0;
if(n!=0)
{
bin=n%2;
binary(n/2);
}
printf("%d",bin);
}
void main()
{
int a;
printf("Decimal value: ");
scanf("%d",&a);
binary(a);
}
When I tried to run above code, it outputs the binary value of the decimal number a preceding with one 0. As I have used recursive function here, it also puts the initial value of bin (i.e. 0) on stack and prints that 0 as well. But I want to display only binary values not that preceding 0.
I would be glad if anyone suggest me how to make this function not to store that initial value of bin on stack.
Try this.
#include<stdio.h>
void binary(int n)
{
bin=n%2;
if(n/2!=0)
binary(n/2);
printf("%d",bin);
}
void main()
{
int a;
printf("Decimal value: ");
scanf("%d",&a);
binary(a);
}
Since it checks whether n/2 == 0 before calling binary() it never prints the intial 0.

Resources