for example, if I typed in 12345 then sum = 1+2+3+4+5
Using C
I used some kind of approach that I'm not sure about it and there is a problem with the line referred to down in the code
int i ;
int sum;
int individual;
int n;
printf("enter the number: ");
scanf("%d",&n);
for (i=0;i<5;++i){
**indvidual=+n[i];**
sum=+invidual;
}
printf("%d",sum)
return 0;
You can divide by 10 and see the remainder to obtain each digits.
Also you should use +=, not =+, to add things to variable and initialize the variables before using their values.
int i ;
int sum = 0; /* initialize */
int individual;
int n;
printf("enter the number: ");
scanf("%d",&n);
for (i=0;i<5;++i){
indvidual=+(n%10); /* obtain a digit (+ is not required, but left by respect) */
n/=10; /* eliminate last dight and proceed to next digit */
sum+=invidual; /* use += instead of =+ */
}
printf("%d",sum); /* also add semicolon here */
return 0;
#include <stdlib.h>
#include <stdio.h>
int main()
{
int i = 12345;
int sum;
i = abs(i);
sum = i?i%9?i%9:9:0;
printf ("Sum of digits is %d\n", sum);
}
Result is 6
Because 1 + 2 + 3 + 4 + 5 = 15
and then 1 + 5 = 6
(or is the expected output 15, after just a single pass through the digits?)
If you want only a single pass through the digits:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int i = 12345;
int sum=0;
i = abs(i);
while(i)
{
sum += i%10;
i /= 10;
}
printf ("Sum of digits is %d\n", sum);
return 0;
}
Related
I am trying to make an array store each number I type in.
Some like:
Enter number:687544
And return as an array like:
6 8 7 5 4 4
I only figure out how to store the nums in order in the "store (num) " function below:
#include<stdio.h>
int store(int num);
int main()
{
int num;
printf("Enter number: ");
scanf("%d",&num);
return store(num);
}
int store(int num)
{
if(num!= 0)
{
int mod = num % 10; //split last digit from number
store(num/10); //recuring it back to the right order
printf("%i\n",mod); //divide num by 10. num /= 10 also a valid one
}
}
Following is I tried but not working code
#include<stdio.h>
int store(int num);
int main()
{
int num,d;
printf("Enter number: ");
scanf("%d",&num);
for(d=0; num<0;d++);
{
num /= 10;
}
int a[d];
int i;
for(i=0;i<d;i++){
a[d]=store(num);
}
printf("%d \n",a[d]);
}
int store(int num)
{
if(num!= 0)
{
int mod = num % 10;
store(num/10);
return mod;
}
}
Unexpected Result.......
Enter number: 123
115183680
Feel like I almost there but I have no idea which part goes wrong. May I ask how to fix this?
I only figure out how to store the nums in order in the "store (num) " function, however I tried the to expand my code that the result is not I expected.
Here is you code fixed, based on the same routing you have wrote, (using recursive calls) and reading int and parsing digits, etc
#include<stdio.h>
void store(int num, int* a, int* d);
int main()
{
int a[10]; // int is 32bit, (Depending on compiler, you can not store more than 10 digit) log10(2**32) ~ 9.xx
int num,d = 0;
printf("Enter number: ");
scanf("%d",&num);
// the recursive function, should need the pointer to array, and the index which is currently parsing
store(num, a, &d);
// print your array till valid index,
for(int i = 0; i< d; i++)
printf("%d ",a[i]);
printf("\n");
}
void store(int num, int* a, int* d)
{
if(num!= 0)
{
store(num/10, a, d);
int mod = num % 10;
a[(*d)++] = mod;
}
}
Here's a modifed version with comments on what I changed
int store(int num, int digit);
int a[20]; // Don't bother with dynamic allocation
int main()
{
int num,d;
printf("Enter number: ");
scanf("%d",&num);
int digit = 0;
int i;
digit = store(num, 0);
// Always print at least 1 digit
for(i=0; i< (digit ? digit : 1); i++){
printf(" %d", a[i]);
}
printf("\n");
}
// Pass in which digit you are on
int store(int num, int digit)
{
if(num!= 0)
{
int mod = num % 10;
int last = store(num/10, digit+1); // next digit
// Fill array right-to-left
a[last - digit - 1] = mod;
return last;
}
return digit; // return number of digits
}
Output
prog$ ./foo
Enter number: 0
0
prog$ ./foo
Enter number: 5
5
prog$ ./foo
Enter number: 123
1 2 3
Note that if you don't want a global array, you can add it as a third parameter to store. Don't forget to initialize a[0] in main in that case.
#include <stdio.h>
#include<math.h>
int main(void){
int Num;
int i;
int N;
int x = 0;
int a[x];
scanf("%d", &Num);
N = Num;
for(i = 0; i < Num; i++)
{if (Num%2 == 0)
{a[i] = 0;}
else
{a[i] = 1;}
Num = Num/2;
}printf("%d in base 2 is %d", N, a[x]);
return 0;
}
program should convert an integer Num to base 2 eg 17 to 10001.
ideally using an array as the output
the remainder of the division of Num by 2 should be the last number in the output
then number is divided by 2 and the process repeats with the second output becoming the 2nd last output of the array
Sorry if this question is worded badly
Any help is appreciated
Thanks
#include <stdio.h>
#include <math.h>
int main(void){
int Num;
int N;
scanf("%d", &Num);
int x = floor(log2(Num)) +1;
int a[x];
N = Num;
for(size_t i=0;i<x;i++){
if(Num%2 == 0){
a[i]=0;
}else{
a[i]=1;
}
Num /= 2;
}
printf("%d in base 2 is ",N);
for(int i=x-1; i>=0;i--){
printf("%d",a[i]);
}
return 0;
}
I think the above code works fine for what you need (although it's not the best way to code this program).
In line 7 of your code, you defined x as 0; so, in line 8, the length of your array is 0 – and that's not what we need.
At the end, when you want to output the result, you only output the xth element of your array. Instead, we want to output every element that we stored. (I used i-- because, if I did not, the binary of Num would be reversed.)
I have to find a missing number in a sequence of numbers.
The input consists of a positive integer n, between 0 and 35000, and n unique numbers with range [0..n]. (So this range contains n+1 numbers).
I already tried some things with sum={n*(n+1)}/2 and then misNum=sum-SumOfNum;, but I couldn't find a way to make this work.
I wrote some code, but not with the examples I mentioned before. Obviously, this code is not complete, but I don't know how to make it complete.
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *v[]) {
int length;
int num;
scanf("%d", &length);
/*scanf(???)*/
int goal=length;
int i;
for(i=0; i!=length; i++){
goal=goal+i-num[i];
};
return goal;
}
Input and outcome should be:
Input: 2 "enter" 0 2. Output: 1
Input: 3 "enter" 0 3 1. Output: 2
Sum of all numbers from 0 to n is
n(a1+an)/2 = (in your case a1 = 0 and an = n+1) n*(n+1)/2
so the missing number is n*(n+1)/2 - (sum of input numbers after the length)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* v[]) {
int length;
int i = 0;
int sum = 0;
scanf_s("%d", &length);
// calculate arithmetic series sum
auto series_sum = ((length + 1) * (length)) / 2;
while (i < length)
{
int next;
scanf_s("%d", &next);
sum += next;
++i;
}
printf("missing num is %d ", series_sum - sum);
}
You have n number of integers to be scanned. Use mathematical equation to calculate the sum of first n+1 natural numbers. Then run a loop for n times and then run a loop to add all the n numbers scanned. Then subtract this sum with the sum of n+1 natural number. Result will be the missing number.
The calculation from the question is also correct and can be made to work with a few modifications.
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *v[]) {
int length;
int num;
// printf("enter maximum number: ");
scanf("%d", &length);
int goal=length;
int i;
for(i=0; i!=length; i++){
// printf("number[%d]: ", i);
if(scanf("%d", &num) != 1) {
fprintf(stderr, "invalid input\n");
return 1;
}
if((num < 0) || (num > length)) {
fprintf(stderr, "invalid number %d\n", num);
return 2;
}
goal=goal+i-num;
};
// printf("missing number: ");
printf("%d\n", goal);
return 0;
}
I need to make a function that sums my array, which is filled with random values. My function only returns 0 and not an actual summation of the array. I don't have much experience with arrays or random values, how can I code my arrSum function to give me the sum of the array when called?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 1000
int arrSum(int arr[SIZE], int b) {
if (b < 0) {
return 0;
} else {
return arr[b] + arrSum(arr, b - 1);
}
}
int main() {
int inputNum;
int i;
int arr1[SIZE];
int sum;
srand(time(0));
printf("Enter an integer between 0 and 1000: ");
scanf("%d", &inputNum);
sum = arrSum(arr1, inputNum);
printf("sum: %6d\n\n", sum );
printf(" Pos | Val\n");
printf("-------------\n");
for (i = 0; i < inputNum; i++) {
printf("%4d |%4d\n", i, rand() % 1001);
}
return 0;
}
The reason is initially there are no values in the array. But only 0.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 1000
int arrSum(int arr[SIZE], int b){
if (b < 0) return 0;
else return arr[b] + arrSum(arr, b-1);
}
int main(){
int inputNum;
int i,q;
int arr1[SIZE] = {0};
int sum;
srand(time(0));
printf("Enter an integer between 0 and 1000: ");
scanf("%d",&inputNum);
for(q=0;q<inputNum;q++){
arr1[q] = rand() % 1001;
}
sum = arrSum(arr1, inputNum);
printf("sum: %6d\n\n", sum );
printf(" Pos | Val\n");
printf("-------------\n");
for (i = 0; i < inputNum; i++){
printf("%4d |%4d\n", i,arr1[i]);
}
return 0;
}
The logic behind finding the sum inside the function is correct, assuming that b is within the range of the array indices. The real problem is that your array is not filled with random numbers - it is filled with zeros.
When you do
int arr1[SIZE] = {0};
you are initializing each element to 0, and are not changing it at any point during the program. So, your function returns the sum from indices 0 to b, but this turns out to be 0.
im trying to determine how many times the user has guess right the number and place of a random number.
for example, if the number is 1234 and the user's input is 7214
so he guess right only the number 2 and 4.
because of this: 1[2]3[4] == 7[2]1[4].
problem: the program throws me out after i take the user's input. im getting an error which says: "Expression: result_pointer != nullptr"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int hit(int num);
int strike(int num);
int rndNum(int num);
void main()
{
int num = 0;
int chosenNum;
int saveHits;
srand(time(NULL));
printf("The Random number: %d", chosenNum = rndNum(num));
printf("\nPlease enter a 4 digit number: ");
scanf("%d", num);
saveHits = hit(num, chosenNum);
printf("\nThe number of hits: %d", saveHits);
getch();
}
int rndNum(int num)
{
int rndNum = rand() % 9000 + 1000;
return rndNum;
}
int hit(int num1, int chosenNum1)
{
int i, hit1 = 0;
for (i = 0; i < 4; i++)
{
if (num1 % 10 == chosenNum1 % 10)
hit1++;
num1 /= 10;
chosenNum1 /= 10;
}
return hit1;
}
You're missing a &
scanf("%d", &num);
^