My program should convert a decimal number into a binary number.
The first for loop is for calculating the binary number.
The second loop puts the msb into the first element of the array.
It is an exercise and the msb has to be in the first element.
I do not understand where the mistake is, help is very much appreciated.
#include<stdio.h>
#include<stdlib.h>
int main(void){
int i;
int bits[16];
int bits_2[16];
int number;
int decimal;
int rest;
int msb;
int result=1;
printf( "Input number smaller than 65536: ");
scanf("%d", &decimal);
if(decimal >= 65536) {
printf( "\n\nincorrect input!");
return EXIT_FAILURE;
}
number=decimal;
for(i=0; result!=0; i++){
result = decimal / 2;
rest = result %2;
bits[i]=rest;
msb = i;
return msb;
}
printf("\n\n %d as binary number : ", number);
for(i=msb; bits[i]>=bits[0]; i--){
bits_2[msb-i] = bits[msb];
printf("%d", bits_2[msb-i]);
}
return 0;
}
result = number = decimal;
for(bits[0]=msb=i=0; result != 0; i++){
bits[i] = result % 2;
result = result / 2;
msb = i;
}
printf("\n\n %d as binary number : ", number);
for(i=msb; i >= 0; i--){
bits_2[msb-i] = bits[i];
printf("%d", bits_2[msb-i]);
}
This is your code with some changes to make it work (you can improve it)
#include<stdio.h>
#include<stdlib.h>
int main(void){
int i;
int bits[16];
int bits_2[16];
int number;
int decimal;
int rest;
int msb;
int result=1;
printf( "Input number smaller than 65536: ");
scanf("%d", &decimal);
if(decimal >= 65536) {
printf( "\n\nincorrect input!");
return EXIT_FAILURE;
}
number=decimal;
for(i=0; result!=0; i++){
result = number / 2; //change this line
rest = number %2; // change this line
number=result; // add this line
bits[i]=rest;
msb = i;
// return msb; //delete this line
}
printf("\n\n %d as binary number : ", decimal); // change this line
for(i=msb; i+1; i--){ // change the condition
// bits_2[msb-i+1] = bits[msb]; // delete this line
printf("%d", bits[i]); // change this line
}
return 0;
}
I noticed that you have a lot of useless variables so this is a compact version of your program:
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i;
int bits[16];
unsigned int decimal;
int rest;
int result=1;
printf( "Input number smaller than 65536: ");
scanf("%d", &decimal);
if(decimal >= 65536) {
printf( "\n\nincorrect input!");
return EXIT_FAILURE;
}
else
printf("\n\n %d as binary number : ", decimal);
for(i=0; result!=0; i++){
result = decimal / 2;
rest = decimal %2;
decimal=result;
bits[i]=rest;
}
i--;
for(; i+1; i--){
printf("%d", bits[i]);
}
return 0;
}
Related
#include <stdio.h>
int bolucu(int n){
int temp;
temp=n;
int basamak=0;
while(temp != 0){
temp/=10;
++basamak;
}
int digits = 0;
int m = n;
while (m) {
digits++;
m /= 10;
}
digits /= 2;
int tmp = 0, lower_half = 0;
while (digits--) {
tmp *= 10;
tmp += n % 10;
n /= 10;
}
while (tmp) {
lower_half *= 10;
lower_half += tmp % 10;
tmp /= 10;
}
if (basamak % 2==1){
n/=10;
}
int a;
int b;
a = n;
b=lower_half;
printf("%d %d\n",a,b);
int loopTemp;
for(int i=0;i<10;i++){
a=3*a+2;
b=2*b+3;
if(a>b){
temp=a;
a=b;
b=temp;
}
if(a==b){
printf("Congratulations you caught one!!!\n");
return 1;
break;
}
}
if(a!=b){
printf("10 tries were not enough!\n");
return 2;
}
}
int main()
{
int number;
printf("\nEnter a number with at least two digits: ");
scanf("%d",&number);
bolucu(number);
while(bolucu(number) != 1){
printf("\nEnter a new number: ");
scanf("%d",&number);
printf("%d",bolucu(number));
}
return 0;
}
e.g:
This is terminal screen.
As you can see there is a second one. First one is true but i don't want second one.
How can i get rid of the second calling?
(Also sorry for bad code writing, i'm new)
What im missing here?
And i cant use any library other than stdio.(Like math.h)
The reason for this is because you call the bolucu() function both inside the while loop and in it's condition check. To fix this, call the function and hold it's result in a variable once, and then use that single result in both the check and your print statement. Your main function can be rewritten like so:
int main()
{
int number;
printf("\nEnter a number with at least two digits: ");
scanf("%d", &number);
int result = bolucu(number);
while (result != 1)
{
printf("\nEnter a new number: ");
scanf("%d", &number);
result = bolucu(number);
printf("%d", result);
}
return 0;
}
I want to input an array of integers then print out the even numbers from the inputted numbers..
example is if I input 2466688992,
it will output 24666882;
I have a my code below:
#include<stdio.h>
int main()
{
int a[5],i;
printf("Enter array of numbers: ");
scanf("%d",&a);
for(i=0; i<sizeof(a); i++){
if(a[i]%2==0)
printf("%d",a[i]);
}
getch();
return 0;
}
It resulted into garbage : 2468000075416640419940000004225568000
This is the function that prints even numbers in an integer :
#include<stdio.h>
int main(){
int num,rem,even=0,digit;
printf(" Enter an integer number: ");
scanf("%d",&num);
printf("\n The even digits present in %d are \n",num);
while(num>0){
digit = num % 10;
num = num / 10;
rem = digit % 2;
if(rem == 0)
even++;
printf("\n %d.",digit);
}
return 0;
}
You should scan the array as a string (unless you want to impose the number of items in the array), and then parse the string to store the different numbers:
long a[50];
char buf[1024];
printf("Enter array of numbers: ");
scanf("%s",buf);
int len = strlen(buf);
int j = 0;
for (int i = 0; i < len; ) {
long sign = 1;
long n = 0;
if (buf[i] == '+') {
++i;
}
else if (buf[i] == '-') {
sign = -1;
++i;
}
if (isdigit(buf[i])) {
while (isdigit(buf[i])) {
n = 10 * n + buf[i++] - '0';
}
a[j] = n * sign;
}
else
i++;
}
for (int i = 0; i < j; i++)
if (!(a[i] ℅ 2)) // true if even
printf("%ld ", a[i]);
This will store all your digits in your array a of size j.
Edit: if you are talking about digits then its easier:
char buf[1024];
printf("Enter array of numbers: ");
scanf("%s",buf);
int len = strlen(buf);
for (int i = 0; i < len; i++)
if (isdigit(buf[i]) && !((buf[i] - '0') ℅ 2)) // true if even, note that '0' equals 0x30 so there is no need to sub it to check for odd/even in reality.
printf("%c ", buf[i]);
(Purpose of the code is write in the title) my code work only if i put the same number once and in the end like - 123455 but if i write 12345566 is dosent work or 11234 it dosent wort to someone know why? i have been trying for a few days and i faild agine and agine.
while(num)
{
dig = num % 10 // dig is the digit in the number
num /= 10 // num is the number the user enter
while(num2) // num2 = num
{
num2 /= 10
dig2 = num2 % 10 // dig2 is is the one digit next to dig
num2 /= 10
if(dig2 == dig) // here I check if I got the same digit twice to
// not include him
{
dig2 = 0
dig = 0
}
}
sum = sum + dig + hold
}
printf("%d", sum)
Well your code's almost correct but there's are somethings wrong ,
When you declared num2 to be equal to num1 it was out of the loop so as soon as one full loop execution is done , num2 still remains to be less than zero or to be zero if it was a unsigned int, so according to the condition the second loop wont execute after its first run.
So mind adding ,
num2 = num1
inside your first loop .
Also your updating num2 twice which i think you wont need to do after the first change .
Full code which i tried
#include<stdio.h>
int main(void)
{
int num;
int num2;
int sum = 0;
int dig, dig2;
scanf_s("%d", &num);
while (num>0)
{
dig = num % 10; //dig is the digit in the number
num /= 10;
num2 = num;// num is the number the user enter
while (num2>0) // num2 = num
{
dig2 = num2 % 10; // dig2 is is the one digit next to dig
if(dig2 == dig) // here i check if i got the same digit twice to //not include him
{
dig = 0;
}
num2 /= 10;
}
sum = sum + dig ;
}
printf("%d", sum);
return 0;
}
O(n)
#include <stdio.h>
int main () {
unsigned int input = 0;
printf("Enter data : ");
scanf("%u", &input);
int sum = 0;
int dig = 0;
int check[10] = {0};
printf("Data input: %u\n", input);
while(input) {
dig = 0;
dig = input%10;
input = input/10;
if (check[dig] == 0) {
check[dig]++;
sum += dig;
}
}
printf("Sum of digits : %d\n", sum);
return 0;
}
My program uses a character array (string) for storing an integer. We convert its every character into an integer and I remove all integers repeated and I calculate the sume of integers without repetition
My code :
#include <stdio.h>
#include <stdlib.h>
int remove_occurences(int size,int arr[size])
{int s=0;
for (int i = 0; i < size; i++)
{
for(int p=i+1;p<size;p++)
{
if (arr[i]==arr[p])
{
for (int j = i+1; j < size ;j++)
{
arr[j-1] = arr[j];
}
size--;
p--;
}
}
}
for(int i=0;i<size;i++)
{
s=s+arr[i];
}
return s;
}
int main()
{
int i=0, sum=0,p=0;
char n[1000];
printf("Input an integer\n");
scanf("%s", n);
int T[strlen(n)];
while (n[i] != '\0')
{
T[p]=n[i] - '0'; // Converting character to integer and make it into the array
p++;i++;
}
printf("\nThe sum of digits of this number :%d\n",remove_occurences(strlen(n),T));
return 0;
}
Example :
Input : 1111111111 Output : 1
Input : 12345566 Output : 21
Or ,you can use this solution :
#include <stdio.h>
#include <stdbool.h>
bool Exist(int *,int,int);
int main ()
{
unsigned int n = 0;
int m=0,s=0;
printf("Add a number please :");
scanf("%u", &n);
int T[(int)floor(log10(abs(n)))+1];
// to calculate the number of digits use : (int)floor(log10(abs(n)))+1
printf("\nThe length of this number is : %d\n",(int)floor(log10(abs(n)))+1);
int p=0;
while(n!=0)
{
m=n%10;
if(Exist(T,p,m)==true)
{
T[p]=m;
p++;
}
n=n/10;
}
for(int i=0;i<p;i++)
{
s=s+T[i];
}
printf("\nSum of digits : %d\n", s);
return 0;
}
bool Exist(int *T,int k,int c)
{
for(int i=0;i<k;i++)
{
if(T[i]==c)
{
return false;
}
}
return true;
}
I am having trouble implementing a reversal of an array in C. I am using linux/nano and we have just started, so very minimal has been taught. The code below is what I have, and the array will print of the binary numbers of whatever integer is entered, but in this code, the binary is reversed from what it should be.
#include "stdio.h"
#define MAX_BITS 32
int main()
{
int num;
printf("Enter a valid positive integer: ");
scanf("%d", &num);
int array[MAX_BITS];
int bit, val;
int numDown = 1;
while (numDown <= num)
{
val = numDown;
while (val > 0)
{
bit = val % 2;
printf("%d",bit);
val = val / 2;
}
printf("\n");
numDown = numDown + 1;
}
return 0;
}
I know I need a while loop but I am unsure as to how to go about it.
#include <stdio.h>
#define MAX_BITS 32
int main()
{
int num;
printf("Enter a valid positive integer: ");
scanf("%d", &num);
char array[MAX_BITS];
int index = MAX_BITS - 1;
int temp = num;
do
{
array[index--] = temp % 2;
temp /= 2;
}while(temp != 0);
printf("Binary of %d is: ", num);
for (int i = index + 1; i < MAX_BITS; ++i )
{
printf("%d", array[i]);
}
printf("\n");
return 0;
}
I made a C program to check if a number is palindrome or not. I used the following code, but it shows numbers like 12321 as non palindrome. Can you please explain me the mistake in the program below?
#include <stdio.h>
int main()
{
int i, x, n, c, j;
int d=0;
printf ("enter total digits in number: ");
scanf ("%d", &i);
printf ("\nenter number: ");
scanf ("%d", &n);
j=n;
for (x=1; x<=i; x++)
{
c= j%10;
d=c*(10^(i-x))+d;
j=(j-c)/10;
}
if (d==n)
{
printf ("\npalindrome");
}
else
{
printf ("\nnon palindrome");
}
return 0;
}
^ is the xor operator.
In order to raise power, you need to include math.h and call pow
d = (c * pow(10, i - x)) + d;
this algorithm is as simple as human thinking, and it works
#include <stdio.h>
int main() {
int i=0,n,ok=1;
char buff[20];
printf("Enter an integer: ");
scanf("%d", &n); // i am ommiting error checking
n=sprintf(buff,"%d",n); //convert it to string, and getting the len in result
if(n<2) return 0;
i=n/2;
n--;
while(i && ok) {
i--;
//printf("%c == %c %s\n", buff[i],buff[n-i],(buff[i]==buff[n-i])?"true":"false");
ok &= (buff[i]==buff[n-i]);
}
printf("%s is %spalindrome\n",buff, ok?"":"not ");
return 0;
}
// Yet another way to check for palindrome.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int n, rn, tn;
printf("Enter an integer: ");
scanf("%d", &n);
// reverse the number by repeatedly extracting last digit, add to the
// previously computed partial reverse times 10, and keep dropping
// last digit by dividing by 10
for (rn = 0, tn = n; tn; tn /= 10) rn = rn * 10 + tn % 10;
if (rn == n) printf("%d is palindrome\n", n);
else printf("%d is not palindrome\n", n);
}
A loop like this might do:
int src; // user input
int n; // no of digits
int res = 0;
int tmp; // copy of src
// .... read the input: n and src ....
tmp = src;
for(int i = 0; i < n; i ++)
{
int digit = tmp % 10; // extract the rightmost digit
tmp /= 10; // and remove it from source
res = 10*res + digit; // apend it to the result
}
// ...and test if(res == src)...