For loop subtraction issue in c [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
#include <stdio.h>
int main() {
int a , b, i , sum = 0 ;
printf("\a");
printf("\n\t\t🔸 What is the number you want to begin"
" subtraction with ? ");
scanf("%d",&a);
printf("\n\t\t🔸 What is the number you want to end"
" subtraction with ? ");
scanf("%d",&b);
printf("\a");
printf("\n\t\t🔸 The result of the subtraction of numbers from %d to %d :\n"
, a , b);
printf("\t\t ");
for (i = a ; i >= b+1 ; i--){
printf("%d - ", i);
sum = sum - i ;
printf("%d = %d\n\n", b , sum);
return 0;
}
I want to create a program that subtracts a sequence of numbers from a to b using a for loop in C programming language. I tried for many times, and couldn't find a solution. I want a program that runs like this: for example, if I chose (a) as 8 and (b) as 5, I want the program to write 8-7-6-5 = -10. I want a program that runs like that depending on the value I choose as (a) and (b) using a for loop in c programming.

if I chose (a) as 8 and (b) as 5, I want the program to write 8-6-5 = -3
I believe that you forgot about number 7 and it should be 8-7-6-5 = -10
If yes:
int myfunc(int start, int end)
{
int result = start;
for(int index = start - 1; index >= end; index--)
{
result -= index;
}
return result;
}
int main(void)
{
printf("%d", myfunc(8,5));
}
https://godbolt.org/z/6133cjbK1
If you want to print the whole expression:
int myfunc(int start, int end)
{
int result = start;
printf("%d", start);
for(int index = start - 1; index >= end; index--)
{
printf("-%d", index);
result -= index;
}
return result;
}
int main(void)
{
printf("=%d", myfunc(8,5));
}

Related

Sum of odd numbers using recursion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This might be simple but I'm new to recursion in c. I want to find the sum of odd integers based on user's input. For example if user inputs 3, function returns 9 (1 + 3 + 5 = 9)
int recursiveSumNOdd(int n)
{
int start = -2; //later start = start+2, so it starts at 0
int n1 = n*2; //total number of digits with rec
int num = 0;
int sum=0;
int count=0;
if(start>=n1)
{
return 0;
}
else
{
start = start+2;
count++;
sum = sum +recursiveSumNOdd(start);
}
return sum;
}
Explanations in comment:
int recursiveSumNOdd(int n) {
if (n == 1 || n == 0)// first "if" in a recursive is its stop condition
return n;
return 2 * n - 1 + recursiveSumNOdd(n-1); // formula for 2->3, 3->5 etc
}
int main(void) {
printf("%d\n", recursiveSumNOdd(3));
return 0;
}
NB: You may want to handle integer overflow
NB2: You can have a mathematics formula to return instantly the result, it is way better, but I guess it was to understand better recursion?
return n * n; // the sum of odd numbers is the square of user's input
You are over-complicating things.
You cannot have the sum of negative elements.
int sum_n_odd(unsigned n)
{
What is the sum of 0 (zero) elements?
if (n == 0) return 0;
If you knew the sum of n - 1 numbers, what is the sum of n numbers?
return sum_n_odd(n - 1) + something; // something is easy to figure out
}

C program - sum of digits without include the same digit twice [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I do the sum of the digits like that:
while(number>0)
{
sum+=number%TEN;
number=number/TEN;
}
but I need that if the number is (for example) 123444 so it'll include only one 4 in the sum. how can I do that?
Have an array of all digits initialized to zero
int digits[10] = { 0 };
Then before adding a digit you check if digits[that_digit] is zero, if yes you set it to 1 and add to sum, if no keep going ...
while(number>0)
{
int one = number%TEN;
if ( ! digits[one]) {
sum+=one;
digits[one] = 1;
}
number=number/TEN;
}
Edit, no array version
Add an int initialized to 0, the bit i indicates if that digit i has already been summed.
If 1 was added, bit 1 set to 1, if 2, bit 2 set to 1 etc...
int bits = 0;
while(number>0)
{
int one = number%TEN;
if (!(bits & (1<<one))) {
sum+=one;
bits |= 1<<one;
}
number=number/TEN;
}
First you should put some code here whatever you tried, to give you basic idea to solve your problem I am putting simple code below.
#include<stdio.h>
#include<malloc.h>
int main()
{
int input, digit, temp, sum = 0;
printf("Enter Input Number :\n");
scanf("%d",&input);
temp = input;
//first find how many digits are there
for(digit = 0 ; temp != 0 ;digit++, temp /= 10);
//create one array equal to no of digits, use dynamic array because once you find different digits you can re-allocate memory and save some memory
int *p = malloc(digit * sizeof(int));
//now store all the digits in dynamic array
p[0] = input % 10;//1
for(int i = 0; i < digit ;i++) {
input /= 10;
p[i+1] = input %10;
if(p[i] != p[i+1])
sum = sum + p[i];
}
printf("sum of different digits : = %d \n",sum);
free(p);
p = 0;
return 0;
}
Explanation of this code I mentioned in comments itself, it may not work for all test case, remaining try yourself.

How to reverse the Fibonacci Series in c without using an Array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm Learning C right now. We have been asked by our instructor to learn how to code the fibonacci series. I have found codes in the internet and it was right. Now another problem given to us was the output of the series must be reversed without using an array or functions, now I'm tryong to figure it out but still I can't get it.
Can someone please give me the code to reverse the output. Thank You!
One solution would be to calculate the Fibonacci numbers and remember the last two numbers calculated. Do the inverse operations and print the numbers:
int main(void)
{
printf("number of Fibonacci numbers: ");
int n;
scanf_s( "%d",&n );
if ( n > 47 )
n = 47;
long int f1 = 0;
long int f2 = 1;
for ( int i = 2; i < n; i ++ ) // calculate the first n Fibonacci numbers
{
long int s = f1 + f2;
f1 = f2;
f2 = s;
}
while ( f1 >= 0 ) // do inverse operations and print Fibonacci numbers
{
printf( "%ld ", f2 );
long int t = f2 - f1;
f2 = f1;
f1 = t;
}
return 0;
}
You can do this using recursion. Call the next recursive call and print the result after that. It will give you the result.
void printFibonacci(int);
int main(){
int n;
printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printFibonacci(n);
printf("%d %d ",1,0);
return 0;
}
void printFibonacci(int n){
static long int first=0,second=1;
int sum;
if(n>0){
sum = first + second;
first = second;
second = sum;
printFibonacci(n-1);
printf("%ld ",sum);
}
}
EDIT without using functions or arrays
You can calculate the nth number in the Fibonacci series beforehand and subtract it with the previous value to get the next value.
int prev=0;
int next=1;
for( i=0; i<n; i++ )
{
sum = prev+next;
prev = next;
next = sum;
}
do
{
printf("%ld ",sum);
int k = sum-prev;
sum = prev;
prev = k;
}
while( sum!=0 );
printf("0");

find all the possible combination of coins, implementing by recursion [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Solve the problem by recursion:
using three type coins include 1 yuan, 2 yuan and 5 yuan, plus to 10 yuan, how many combinations?
The following is my code :
int coinNum(int num){
if(num>=0){
if(num==0)
return 1;
else
return coinNum(num-5)+coinNum(num-2)+coinNum(num-1);
}else
return 0;
}
int main(){
int num=coinNum(10);
printf("%d\n",num);//the result is 128
system("pause");
return 0;
}
What's the error of my recursion algorithm or what's your right code ?question supplement :1. (5,2,2,1) and (2,5,2,1) should be counted as 1 combination . 2. the following is my code of the enumeration algorithm .
void coin(){
int i,j,k,count=0;
for(i=0;i<=10;i++)
for(j=0;j<=5;j++)
for(k=0;k<=2;k++)
if((i+2*j+5*k)==10){
count++;
printf("one yuan :%d,2 yuan :%d,5 yuan :%d\n",i,j,k);
}
printf("总方法数%d\n",count);//the result is 10
}
Your code is counting the number of permutations that add up to 10. You want combinations. That means (5,2,2,1) and (2,5,2,1) should be counted as 1 combination.
In this case, the answer should be 10: (5,5), (5,2,2,1), (5,2,1,1,1), (5,1,..1), (2,2,2,2,2), (2,2,2,2,1,1), (2,2,2,1,1,1,1), (2,2,1,..1), (2,1,..1), and (1,..1).
Try this code:
int coinNum(int num, int *coins){
if (num == 0) return 1;
if (num < 0 || !*coins) return 0;
return coinNum(num - *coins, coins) + coinNum(num, coins+1);
}
int main(){
int coins[] = {5,2,1,0}; // don't forget the 0 or the program won't end
int num=coinNum(10,coins);
printf("%d\n",num); // the result is 10
system("pause");
return 0;
}
The code above tries all combinations until the sum equals or exceeds the desired sum. Note that this is not the most efficient algorithm to solve this problem, but the most simple one. For better algorithms, you should probably look for it at Computer Science Stack Exchange.
Another simple algorithm, using idea to generate not decreasing sequence of coins.
int coinNum(int num, int min_coin) {
if (num == 0) {
return 1;
} else if (num < 0) {
return 0;
} else {
int res = coinNum(num - 5, 5);
if (min_coin <= 1) {
res += coinNum(num - 1, 1);
}
if (min_coin <= 2) {
res += coinNum(num - 2, 2);
}
return res;
}
}
int main(){
int num = coinNum(10, 1);
printf("%d\n", num);
return 0;
}

C how to check and point a '0' in a int number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Imagine we've got an int number = 1040 or int number = 105 in a C program, and we want to know if this number contains a 0 and in which position/s are they. How could we do it?
Examples
1040 -> position 0 and 2.
1000 -> position 0, 1 and 2.
104 -> position 1.
56 -> NO ZEROS.
Thanks!
I would divide by 10 and check the remainder. If remainder is 0, then last position of the number is 0. Then repeat the same step until number is less than 10
#include<iostream>
int main(void)
{
long int k = 6050404;
int iter = 0;
while (k > 10) {
long int r = k % 10;
if( r == 0) {
std::cout << iter << " ";
}
k = k / 10;
iter++;
}
}
I would convert it to a string; finding a character in a string is trivial.
As a rule of thumb, if you are doing maths on something, it's a number; otherwise, it's probably (or should be treated as) a string.
Alternatively, something like:
#include <stdio.h>
int main(void) {
int input=1040;
int digitindex;
for (digitindex=0; input>0; digitindex++) {
if (input%10==0) {
printf("0 in position %i\n",digitindex);
}
input/=10;
}
return 0;
}
This basically reports if the LAST digit is 0, then removes the last digit; repeat until there is nothing left. Minor modifications would be required for negative numbers.
You can play with this at http://ideone.com/oEyD7N

Resources