Taking power with loop(with-out pow() function) [closed] - c

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 4 years ago.
Improve this question
Im trying the powers of an integer. For example if my integers is 2 first 10 power is 2^1,2^2...2^10. Im using
while (expnt < 10)
{
preExpnt = expnt;
while (preExpnt)
{
preExpnt *= num;
printf("%lld\n", preExpnt);
}
expnt++;
}
but it doesn't work.`

Here is a way you could achieve your purpose.
int num = 2; // for example
int out = 1;
for (int exp = 1; exp <= 10; exp++)
{
out *= num;
printf("%d\n", out);
}
Remarks about your code:
Your inner while loop is infinite if num and expnt are both different from 0.
Assigning preExpnt to the value of expnt at each step and multiplying by num would display a something like: 1*n 2*n 3*n 4*n ... if expnt starts at 1.

Related

Sum of carryover on adding two 4 digit numbers [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 6 years ago.
Improve this question
How to get sum of carryover on adding two 4 digit numbers?
An example would be:
Carryovers 111
First 4 digit 9999
2nd 4 digit 7777
Answer 17776
Here I want to calculate the sum of the carries (answer of summing carries = 3), what should I do?
This is a rather simple task. You may not know it sice you are new here, but SO is not a coding plateform, so you won't get answers unless you show us your code first.
But, since I wanted to try myself on the algorithm, here's a simple answer. I warn you, it won't work on all examples (I won't tell you when this code won't work) and I'm not commenting the code on purpose. I'm smelling the assignment here, so the code is just to give you some pointers, not to make your homework.
Beware: It's just a skeleton and you won't get a good grade if you copy paste it as it is.
int main(int argc, char** argv)
{
int numberA = 9999;
int numberB = 7777;
int sum_of_carryovers = 0;
while (numberA > 9 && numberB > 9)
{
int digitA = numberA % 10;
int digitB = numberB % 10;
int sum = digitA + digitB;
if (sum > 9)
{
sum_of_carryovers += 1;
}
numberA /= 10;
numberB /= 10;
}
printf("The sum of carryovers = %d\n", sum_of_carryovers);
}

Card Hangover C [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 6 years ago.
Improve this question
I am trying to do this problem: http://poj.org/problem?id=1003
#include <stdio.h>
int c;
int a = 0;
int i;
int main()
{
scanf("%.2f", &c);
if (0.01 <= c <= 5.20){
for (i = 1; a < c; ++i){
a += (1/(i + 1));
}
printf("%d card(s)", i + 1);
}
return 0;
}
My code isn't working? For some reason it always returns 2 card(s) no matter what I enter. Can someone find the problem?
Thanks!
Problem 1: This is not how you test if a variable is between two values:
if (0.01 <= c <= 5.20){
The correct way is
if (0.01 <= c && c <= 5.20){
Your code is interpreted as if you'd written:
if ((0.01 <= c) <= 5.20){
(0.01 <= c) will be either 0 or 1, and both of these are less than 5.20, so it's always true.
Problem 2: The variables a and c need to be float, not int, because int variables can't have fractions in them, and %f format in scanf requires that the corresponding argument be a pointer to float.

Unable to use variable value outside for loop in C program [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am following a introductory C programming course and the first assignment is to find number of perfect squares in a given range.
I am trying to get the first perfect square root, but when I try to assign the first square root to a variable, i am unable to do so and it always shows 0.
This is the program that I have written:
#include<stdio.h>
void main()
{
float y= 0;
float k = 1.0;
float n;
int i=0;
int first_sqrt;
first_sqrt = 0;
printf("enter number: \n");
scanf("%f",&n);
// finding the first perfect square
for(y = 0; y<=10000; y++)
{
while((k*k - n)>0.0001 || (n - k*k)> 0.0001)
{
k = (k + n/k) / 2;
//printf("%f\n", k);
}
i = (int)k;
if(i*i == n)
{
printf("perfect squareroot: %d\n", i);
i = first_sqrt;
y = 10001;
//break;
}
else
{
printf("not perfect square: %f\n", n);
n = n+1;
}
}
printf("first perfect square root: %d\n", first_sqrt);
}
I am sorry for posting the whole program, but I have no idea where the problem might be. This is the first assignment of the first week so I don't have understanding of a lot functions in C yet and I can't use math function for this assignment.
Any help would be appreciated. Have been searching all day about this but couldn't understand much.
A basic direction towards the problem would be most appreciated. Thanks.
The expression i = first_sqrt; assigns first_sqrtto i and not i to first_sqrt. Change it to first_sqrt = i;. Apart from this you can remove the comment from //break;.
Reverse this line
i = first_sqrt;
to first_sqrt = i;
You mixed up an assignment. This:
i = first_sqrt;
Should be:
first_sqrt = i;

Binary To Decimal [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 8 years ago.
Improve this question
The program is suppose to convert binary number to decimal form. Only using scanf() and printf() library functions. Takes in a char array from user ---no prompt outputs decimal form, function must be used with parameter (char binaryString[]) after conversion result must be printed out in main. Program does not work don't think I'm converting the binary form to decimal form correctly in function binaryToDecimal since i cant use pow() I'm lost
#include <stdio.h>
#include <math.h>
int binaryToDecimal(char binaryString[]) {
int c, j = 1, decimalNumber = 0;
for (binaryString[c = 0]; binaryString[c] > binaryString[33];
binaryString[++c]) {
while (binaryString[c] != 0) {
remainder = binaryString[c] % 10;
decimalNumber = decimalNumber + remainder * j;
j = j * 2;
binaryString[c] = binaryString[c] / 10;
}
}
return decimalNumber;
}
int binaryToDecimalMain() {
int arraysize = 33;
char binaryString[arraysize];
scanf("%32s", binaryString);
printf("%d",binaryToDecimal(binaryString []);
return 0;
}
I not give you the algorithm because it's seems that you are learning how to program and it is important to you to learn to discover how to solve the problems that are given to you.But I can give you some hints:
use binaryString only to compare with '0' or '1'. Don't try to make any operations like '%' on it.
iterate on the binaryString character by character (no while inside for [this is only for this case, there some algorithm that is necessary to do something like this])
your logic to convert is on the right track
Also you should call your main function main.

How to solve using bitwise operators [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 9 years ago.
Improve this question
I wanted to find out efficiently which drawers are Full. But the output has to be a number corresponding to the binary representation. For example if only the second Drawer is full (from left to right), then the output is 4:
8 4 2 1
0 1 0 0 (Drawer Two is Full)
So I used this approach.
int DrawersFull[4] = {0,0,0,0}; //Initially all are empty
for(i=0;i<4;i++)
{
if(IsDrawerFull) // the api was provided by the interviewer
DrawerFull[i]=1;
}
I am not sure how to generate the output. Any suggestions will be helpful. Thanks.
Interviewer gave me hint that it can be done using bitwise operators but I am not sure.
This is the same as converting binary number to decimal. Try this one:
int res = 0;
for (i = 4; i >= 1; i--) {
res = res * 2 + DrawerFull[i]; // Assuming DrawerFull will contain only 1 or 0.
}
char fullDrawers = 0;
for( char i = 0; i < 4; ++i )
{
if( IsDrawerFull )
fullDrawers &= 1;
fullDrawers <<= 1;
}

Resources