Decimal to octal conversion c.using code blocks 16.01.the conversion works until 63.This is my first time posting here
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// conversion of decimal to octal
int main()
{
int num,sum,count,x,y;
printf("enter a number\t");
scanf("%d",&num);
count = 0;
sum = 0;
while (num>0){
x=num%8;
x=x*pow(10,count);
count=count+1;
num=num/8;
sum=sum+x;
}
printf("\n%d",sum);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// conversion of decimal to octal
int main()
{
int num,i,len=0,x[100];
printf("enter a number\t");
scanf("%d",&num);
while (num>0){
x[len]=num%8;
num=num/8;
len++;
}
for(i=len;i>=0;i--)
printf("\n%d",x[i]);
return 0;
}
This will work for the conversion from decimal to octal.
Note: Check the logic works!.Make the sum long int. After 63 your int no longer able to hold the answer.
The problem with the code is actually reported by the compiler if you turn on some warnings, however the problem is that you are implicitly converting from double to int, and depending on the implementation, you'd lose precision. pow returns a double but you're storing it in an int (x)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// conversion of decimal to octal
int main()
{
int num,sum,count,x,y;
printf("enter a number\t");
scanf("%d",&num);
count = 0;
sum = 0;
while (num>0){
x=num%8;
x=x*pow(10,count);
/* ^^^^^^^^^^^^^^^^ problem here */
count=count+1;
num=num/8;
sum=sum+x;
}
printf("\n%d",sum);
return 0;
}
now the fix
Instead of using the library pow, you should implement your own that's integer only (because you KNOW you are using ints only)
/* a trivial implementation */
long pow10(int n) {
long result = 1;
while(n--) {
result *= 10;
}
return result;
}
Now to clean up the program a bit:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long pow10(int n);
/* a trivial implementation */
long pow10(int n) {
long result = 1;
while(n--) {
result *= 10;
}
return result;
}
int main()
{
int num, count;
long sum, x;
printf("enter a number\t");
scanf("%d",&num);
count = 0;
sum = 0;
while (num>0){
x=num%8;
x=x*pow10(count);
count=count+1;
num=num/8;
sum=sum+x;
}
printf("\n%ld",sum);
return 0;
}
Related
I want to turn a number like 0.1235 to 1235.
i tried to do it through a loop by multiplying by 10 but i didnt know how to stop the loop.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main (){
double a;
printf("a:");
scanf("%lf", &a);
while (/* condition */)
{
a = a*10;
}
printf("a: %lf",a)
getch ();
return 0;
}
int var = (int)round(0.1235 * 10000);
This is not an easy problem to solve as it may seem at first due to decimal precision and lack of implementation details in your question. Here is an attempt of a solution to your problem, but you might need to adjust it depending on your needs.
#include <stdio.h>
#include <math.h>
#define DELTA 0.00001
int get_number_of_decimal_places(double num)
{
int count = 0;
do {
num = num * 10;
++count;
} while (num - (int)num > DELTA);
return count;
}
int main()
{
double a;
int result = 0;
printf("a:");
scanf("%lf", &a);
int decimal_places = get_number_of_decimal_places(a);
do {
a *= 10;
result += (int)a * pow(10, --decimal_places);
a -= (int)a;
} while (decimal_places != 0);
printf("result: %d", result);
getch();
return 0;
}
For input value 0.12345, the output is:
12345
Keep in mind that this solution treats input values 0.1, 0.0001, 0.010 etc. the same way, so the output would be:
1
#include<stdbool.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf_s("%lf", &InputNum);
for (int i = 0; i < InputNum; --NumLoops) //incrementally goes down
{
if (isdigit(InputNum))
{
printf("%lf\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}
program incrementally increases by one starting at user's input, this happens 11 times and than ends program, unfortunately it does not do that and keeps printing out the else statement in this code, Any suggestions?
So the problem is that scanf() is converting the integer you enter to a double. So when you type an int for example 5, will be converted to 5.00000, and isdigit() will not convert this to a digit so it always fails.
This is how I would do it.
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf("%lf", &InputNum);
for (int i = 0; i < NumLoops; ++i)
{
if ((InputNum - floor(InputNum)) == 0)
{
printf("%f\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}
I Wrote this code which can identify whether a number is a Armstrong number or not
#include <stdio.h>
#include <stdlib.h>
int n;
const int input()
{
printf("insert n:");
scanf("%d",&n);
return n;
}
int Num_amount()
{
int amount=0;
while(n>=10)
{
amount++;
n=n/10;
if(n<10)
amount++;
}
return amount;
}
int Armstrong()
{
n=input();
int v;
int z=0;
int y=10
int x=Num_amount();
int m[100]={};
int i;
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
return z;
}
int main()
{
int z=Armstrong();
printf("%d",z);
}
When run with n=153 i always get 0.After several debugging,I found out the problem is somewhere in the Armstrong function(most likely)
int Armstrong()
{
n=input();
int v;
int z=0;
int y=10
int x=Num_amount();
int m[100]={};
int i;
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
return z;
}
The debug watches indicate that instead of execute the for loop,it went straight to the return z line,I have tried everything but still can't figure it out.Can you tell me what the problem is?
You are getting the wrong result because of some logical error. When you are choosing a variable to be global, you need to consider that the variable value can be modified by any function and in this case, you have already modified its value in num_amount function. You have also made some logical error in Num_amount and Armstrong function.
You haven't included math.h header file for pow.
Here is your modified code,
#include <stdio.h>
#include <stdlib.h>
#include<math.h> //<-------------Should have included
int n;
const int input()
{
printf("insert n:");
scanf("%d",&n);
return n;
}
int Num_amount() //<------------modified
{
int z = n; //<--------take a copy of global n
int amount=0;
while(z>0)
{
amount++;
z=z/10;
}
return amount;
}
int Armstrong() //<------------modified
{
n=input();
int v;
int z=0;
int x=Num_amount();
int i;
while(n>0)
{
v=n%10;
z+=pow(v,x);
n/=10; //<-------modification of global n
}
return z;
}
int main()
{
int z=Armstrong();
printf("%d",z);
}
Found a lot problems with the code. Here is a modified version.
1. Do not use a global variable.
2. Make calculation for power easier.
3. Return the status of result, not the result. You want to check whether number is Armstrong or not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int no_digits(int n){
int digits = 0;
while(n){
digits++;
n = n/10;
}
return digits;
}
int armstrong(){
int n;
printf("insert n:");
scanf("%d",&n);
int digits = no_digits(n);
int curnum = 0,original = n;
while(n){
curnum += pow(n%10,digits);
n /= 10;
}
if(original == curnum)
return 1;
return 0;
}
int main(){
if(armstrong())
printf("Is Armstrong\n");
else printf("Not Armstrong\n");
}
Let's take a look at your loop:
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
What's the value of n at this point? You've set it in the previous call to Num_amount like so:
while(n>=10)
{
amount++;
n=n/10;
if(n<10)
amount++;
}
So, after Num_amount has finished executing, n must be less than 10, meaning the loop in Armstrong won't execute.
This is a big reason why you shouldn't use globals, even in a toy program like this. If you use it for different purposes in different places, you just create headaches like this.
At the very least, you should change your code such that n is passed as a parameter to Num_amount and Armstrong.
Your function Num_amount() return "n" value is already less than 10 and for loop never run.
Problem Statement
You are given an array of integers of size . You need to print the sum of the elements of the array.
Note: A signed 32-bit integer value uses bit to represent the sign of the number and remaining 31 bits to represent the magnitude. The range of the 32-bit integer is . When we add several integer values, the resulting sum might exceed this range. You might need to uselong long int in C/C++ or long data type in Java to store such sums.
Input Format
The first line of the input consists of an integer. The next lines contain space separated integers describing the array.
Constraints
Output Format
Output a single value equal to the sum of the elements of the array.
Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005`
Sample Output
5000000015
My program
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i;
scanf("%d",&n);
long int a[5],sum;
for(i=0;i<=n-1;i++){
scanf("%ld %ld %ld %ld %ld",&a[1],&a[2],&a[3],&a[4],&a[5]);
}
for(i=0;i<=n-1;i++){
sum = sum + a[i];
}
printf("%ld",sum);
return 0;
}
Error description
Input (stdin):
5
1000000001 1000000002 1000000003 1000000004 1000000005
Your Output (stdout):
140692151765426
Expected Output:
5000000015
Compiler Message:
Wrong Answer
This works fine :
#include <inttypes.h>
int main()
{
int n,i;
scanf("%d",&n);
unsigned long long int a[5];
unsigned long long int sum=0;
for(i=0;i<n;i++)
{
scanf("%llu",&a[i]);
}
for(i=0;i<n;i++)
{
printf("%llu\n",sum);
sum = sum + a[i];
}
printf("\nSum is : %llu",sum);
return 0;
}
Use the ll long-long modifier with the u (unsigned) conversion
You don't basically need an array here. Just
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
long long int a, sum = 0;
while (n--) {
scanf("%lld", &a);
sum += a;
}
printf("%lld", sum);
return 0;
}
The program has following issues:
Array indexing is wrong : Array is of size 5 starting from 0 , you can use only till a[4] , but for scanf() , it tries to read value as &a[5].
No use of for loop when you are using hard coded index a[1],a[2],etc. Instead the below code will be better to get input as follows:
for (i = 0; i < n; i++ ) {
scanf("%ld",&a[i]);
}
Sum includes : sum = sum + a[i];
when i = 0 --> a[0] will have have junk values because input was not taken from user as scanf started from a[1] . Since array is not initialized this is uninitialized auto variable, it might have junk values which will get added in the sum .
sum itself is also not initialized, so it will contain junk value and for very first addition : sum = sum + a[0]; thsi junk value will get added .
Hope this answer your query for unexpected output.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n,i;
cin >> n;
long long int s=0;
long int a[10];
if(n>=1 && n<=10)
{
for(i=0;i<n;i++)
{
cin>>a[i];
s=s+a[i];
}
}
cout<<s;
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i;
scanf("%d",&n);
long int a[10];
long long sum=0;
for(i=0;i<=n-1;i++){
scanf("%ld",&a[i]);
}
for(i=0;i<=n-1;i++){
sum = sum + a[i];
}
printf("%ld",sum);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
long long int a[10^9],sum=0;
int n,i,length;
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(0<=a[i]<=10^10)
{
scanf("%lld",&a[i]);
}
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("%lld",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
i dont know the reason why i am getting the segentation fault this code runs fine for this input 1000000001 1000000002 1000000003 1000000004 1000000005
Issues in your code:
0<=a[i]<=10^10 is not correct, should change to 0<=a[i] && a[i]<=(10^10)
^ is a bitwise xor, not power,
In your for loop, you always compare before read element of a[], so you need to read first, then compare.
use unsigned long long, don't need int at end.
Check this code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX_NUM 1000000000ULL
#define MIN_NUM 0ULL
int main() {
int n,i;
printf("input number count: ");
scanf("%d",&n);
unsigned long long a[n],sum=0;
for(i=0;i<n;i++) {
printf("input number[%d]: ", i);
scanf("%llu",&a[i]);
if(a[i]<MIN_NUM || a[i]>MAX_NUM) {
a[i] = 0;
printf("\t(ignored, due to out of range [%llu, %llu])\n", MIN_NUM, MAX_NUM);
}
}
for(i=0;i<n;i++) {
sum+=a[i];
}
printf("\nsum: %llu\n",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}