I need the cursor to come down - c

The program is just a base 10 to base 2 converter it works fine. I just want the cursor at the new line.
I have put "\n" everywhere I can logically put it but it just does not bring the cursor to the new line. The program is doing what I need it to do.
EX
"Enter a decimal number from 0 to 18,446, 744,073,709,551,615: 156"
The binary value for 156 is:10011100{{CURSOR}}
(I want the {{CURSOR}} here)
#include <stdio.h>
int main(void)
{
long int dec, num;
int store[100], i=1,j;
printf("Enter a decimal number from 0 to 18,446,744,073,709,551,615: ");
scanf("%ld",&dec);
num = dec;
while(num!=0)
{
store[i++]=num%2;
num=num/2;
}
printf("The binary value for %d is: ",dec);
for(j=i-1;j>0;j--)
{
printf("%d",store[j]);
}
return 0;
}

When all is said and done, you need to print a newline after the number is done using printf("\n"). The rest, like you said, does what it should:
#include <stdio.h>
int main(void)
{
long int dec, num;
int store[100], i=1,j;
printf("Enter a decimal number from 0 to 18,446,744,073,709,551,615: ");
scanf("%ld",&dec);
num = dec;
while(num!=0)
{
store[i++]=num%2;
num=num/2;
}
printf("The binary value for %d is: ",dec);
for(j=i-1;j>0;j--)
{
printf("%d",store[j]);
}
printf("\n");
return 0;
}

Related

Perfect numbers representation problem with function

The task is:
Write a program that prints the first n perfect numbers. Checking that the number is perfect should be done in the perfect function.
I did it like this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 100
/*
*/
int perfect_number(long int n)
{
long int sum=0;
for(int j=1;j<n;j++)
{
long int candidate=j;
if(n%candidate==0)
{
sum=sum+candidate;
}
}
if(sum==n)
{
return n;
}
return 0;
}
int main()
{
int n;
printf("Enter how much perfect numbers you want :");
scanf("%d",&n);
while(n<1)
{
printf("Enter how much perfect numbers you want:");
scanf("%d",&n);
}
int counter=0;
for(long int i=1;counter<n;i++)
{
if(perfect_number(i))
{
printf("%ld ",i);
counter++;
}
}
return 0;
}
The problem arises when I type that I want, the first 5 perfect numbers or more. The program will print only 4 and will continue to work, it will search for numbers but will not print anything.
If I type in the first four perfect numbers, they will print 4 and finish the program, they will do everything right.
I thought the problem was in representing the fifth number, so I replaced int with long int, but that didn't help.

How to double digits in number

I am having a problem on how to double digits of any number.
For example, the number: 12345 output would be 1122334455 using functions and loops.
#include <stdio.h>
int main() {
int num;
printf("Please Enter a number");
scanf("%d",&num);
for(int i=0;i<=num%10;i++) {
if(i==num%10)
newNum+=i;
for(int i=1;i<=num%10;i++) {
if(i==num/10%10)
newNum+=i;
I am assuming that you do not have to store the value with duplicated digits, as storing it as int will quickly overflow. If you have to, you can use long long or an array.
Your for loop does not make sense. You have to loop until all intergers have been duplicated. To do so, determine the ones place with mod 10, then divide number by 10. It will loop until number is 0. Try this.
#include <stdio.h>
int main(void) {
int number;
int temp;
printf("Enter an integer: ");
scanf("%d", &number);
while(number) {
temp = number % 10;
printf("%d%d", temp, temp);
number /= 10;
}
return 0;
}

Write a program that multiplies user entered number till product of these numbers reach 1000

I've trying to do it for about an hour, but I can't seem to get it right. How is it done?
The code I have at the moment is:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int j=-1;
while(j<0){
printf("Enter a number: \n");
scanf("%d", &j);
}
int i=j;
for(i=j; i<=100; i++){
printf("%d \n", i);
}
return 0;
}
The original specification (before code was added) was a little vague but, in terms of the process to follow, that's irrelevant. Let's assume they're as follows:
get two numbers from the user.
if their product is greater than a thousand, print it and stop.
otherwise, print product and go back to first bullet point.
(if that's not quite what you're after, the process is still the same, you just have to adjust the individual steps).
Translating that in to pseudo-code is often a first good step when developing. That would give you something like:
def program:
set product to -1
while product <= 1000:
print prompt asking for numbers
get num1 and num2 from user
set product to num1 * num2
print product
print "target reached"
From that point, it's a matter of converting the pseudo-code into a formal computer language, which is generally close to a one-to-one mapping operation.
A good first attempt would be along the lines of:
#include <stdio.h>
int main (void) {
int num1, num2, product = -1;
while (product < 1000) {
printf ("Please enter two whole numbers, separated by a space: ");
scanf ("%d %d", &num1, &num2);
product = num1 * num2;
printf ("Product is %d\n", product);
}
puts ("Target reached");
return 0;
}
although there will no doubt be problems with this since it doesn't robustly handle invalid input. However, at the level you're operating, it would be a good start.
In terms of the code you've supplied (which probably should have been in the original question, though I've added it now):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int j=-1;
while(j<0){
printf("Enter a number: \n");
scanf("%d", &j);
}
int i=j;
for(i=j; i<=100; i++){
printf("%d \n", i);
}
return 0;
}
a better way to do the final loop would be along the lines of:
int i = 1;
while (i < 1000) {
i = i * j;
printf ("%n\n", i);
}
This uses the correct terminating condition of the multiplied number being a thousand or more rather than what you had, a fixed number of multiplications.
You may also want to catch the possibility that the user enters one, which would result in an infinite loop.
A (relatively) professional program to do this would be similar to:
#include <stdio.h>
int main (void) {
// Get starting point, two or more.
int start = 0;
while (start < 2) {
printf("Enter a number greater than one: ");
if (scanf("%d", &start) != 1) {
// No integer available, clear to end of input line.
for (int ch = 0; ch != '\n'; ch = getchar());
}
}
// Start with one, continue while less than a thousand.
int curr = 1;
while (curr < 1000) {
// Multiply then print.
curr *= start;
printf ("%d\n", curr);
}
return 0;
}
This has the following features:
more suitable variable names.
detection and repair of most invalid input.
comments.
That code is included just as an educational example showing how to do a reasonably good job. If you use it as-is for your classwork, don't be surprised if your educators fail you for plagiarism. I'm pretty certain most of them would be using web-search tools to detect that sort of stuff.
I'm not 100% clear on what you are asking for so I'm assuming the following that you want to get user to keep on entering numbers (I've assumed positive integers) until the all of them multiplied together is greater than or equal to 1000).
The code here starts with the value 1 (because starting with 0 will mean it will never get to anything other than 0) and multiples positive integers to it while the product of all of them remains under 1000. Finally it prints the total (which may be over 1000) and also the number of values entered by the user.
I hope this helps.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char input[10];
unsigned currentTotal = 1;
unsigned value;
unsigned numEntered = 0;
while( currentTotal < 1000 )
{
printf( "Enter a number: \n" );
fgets( input, sizeof(input), stdin );
value = atoi( input );
if( value > 0 )
{
currentTotal *= value;
numEntered += 1;
}
else
{
printf( "Please enter a positive integer value\n" );
}
}
printf( "You entered %u numbers which when multiplied together equal %u\n", numEntered, currentTotal );
return 0;
}
Try this one:
#include <stdio.h>
int main()
{
int input,output=1;
while(1)
{
scanf("%d",&input);
if(input<=0)
printf("Please enter a positive integer not less than 1 :\n");
else if(input>0)
output*=input;
if(output>1000)
{
printf("\nThe result is: %d",output);
break;
}
}
return 0;
}

Finding biggest and smallest numbers using user input

Well it is a problem about finding the biggest and smallest number in a group of numbers, but we do not know how many numbers the user wants-
So far this is what i have done:
#include <stdio.h>
#include <conio.h>
int main()
{
int num;
int i;
int maxi=0;
int minim=0;
int cont = 0;
printf ("\nQuantity of numbers?: ");
scanf ("%d", &num);
while (num>0)
{
printf ("\nEnter number:");
scanf ("%d", &i);
if (num>i)
minim=i++;
else
if (i>num)
max=i++;
cont++;
}
printf ("\nBiggest number is es: %d", maxi);
printf ("\nSmallest number is: %d", minim);
getch();
return 0;
}
I did my program to ask how many numbers the user will want to put and i made the program to read them, BUT when it reads the biggest or/and smallest numbers it will sometimes changes biggest with small and it will not read negative numbers.
How do i do to make my program better?
You're comparing against the wrong values.
do
{
printf("Enter a number.\n");
scanf("%i", &input);
if min > input
min = input
if max < input
max = input
} while (input > 0);
#include <stdio.h>
#include <conio.h>
#include <limits.h>
int main(){
int num;
int i;
int maxi=0;
int minim=INT_MAX;
int cont = 0;
printf ("\nQuantity of numbers?: ");
scanf("%d", &num);
if(num > 0){
while (num>0){
printf ("\nEnter number:");
if(scanf("%d", &i) == 1 && !(i<0)){
if(minim > i)
minim = i;
if (maxi < i)
maxi = i;
++cont;
--num;
} else {
//fprintf(stderr, "redo input!\n")
;
}
scanf("%*[^\n]%*c");
}
printf ("\nBiggest number is : %d", maxi);
printf ("\nSmallest number is : %d\n", minim);
}
getch();
return 0;
}
You should initialize mini to the largest possible int, i.e. INT_MAX and maxi to the smallest possible int, i.e., INT_MIN. This way, even if the first number is negative, it will be considered for maxi, and if the first number is positive it will still be considered for mini. The constants INT_MAX and INT_MIN are included in <climits> or <limits.h>.
Also, you are comparing the current entered number with num, which is the counter of numbers entered by user, not one of the values he wants to compare. A better modified code would be :
#include<limits.h>
#include<stdio.h>
int main()
{
int num;
int maxi=INT_MIN; //initialize max value
int mini=INT_MAX; //initialize min value
int temp;
scanf("%d", &num); //take in number of numbers
while(num--) //loop "num" times, num decrements once each iteration of loop
{
scanf("%d", &temp); //Take in new number
if(temp>maxi) //see if it is new maximum
maxi=temp; //set to new maximum
if(temp<mini) //see if new minimum
mini=temp; //set to new minimum
}
printf("\nMaxi is:\t%d\nMini is:\t%d\n", maxi, mini); //print answer
return 0;
}

Decimal to binary C while dividing by 2

I have to write 2 programs that convert dec to bin. I've finish the 1st one however when I try to run this one it prints out an array of random number not sure why, here is what I have:
#include <stdio.h>
int getNumber();
int dectoBin(int, int binarray[], int);
void printBin(int binary[], int dec);
int main()
{
int M = 8;
int binarray[M];
int dec = getNumber();
printf("The decimal number you entered was: %d", dec);
decToBin(dec, binarray, M);
printBin(binarray, dec);
return 0;
}
int getNumber()
{
int dec;
printf("Enter any a number between 0 and 255: ");
scanf("%d",&dec);
return dec;
}
int decToBin(int dec, int binarray[], int M)
{
int i, j;
for(i=8; i>=0;i--)
{
while(dec != 0)
{
binarray[i]= dec % 2;
dec = dec / 2; binarray[i] = dec;
}
}
return *binarray;
}
void printBin(int binary[], int dec)
{
int i;
if(dec > 255)
{
printf("please use another number");
main();
}
else
{
for(i =8; i >= 0;i--)
{
printf("%d", binary[i]);
printf("\n");
}
}
}
You're setting binarray[i] twice - once correctly (after the modulus statement) and once incorrectly (after dividing dec). So if your decimal number is 65, your second-to-last number would be 32.
You can also make binarray an array of bits rather than numbers. This would have given you a error on compilation and would have perhaps clued you into the logical error.
EDIT: There are a lot of control flow issues with this program. You should check to see if dec > 255 BEFORE calling decToBin otherwise the program will run incorrectly. decToBinary shouldn't return an int - it should return an array of ints (or bits as I suggested) and that is what you should send to printBin. Right now you're returning the pointer to binArray (do you know what pointers are?) decToBin should also return void since it does not perform any calculations.
EDIT 2: As someone else pointed out your array indexes are incorrect, I don't know how you haven't gotten an array out of bounds error. Also, while printing you're going the wrong way: You need to use a for loop that starts at 0 and ends at 7.
Here are some suggestions.
You have dectoBin in the declaration but decToBin in the function call and function definition. Make sure they are all dectoBin or they are all decToBin.
M is not used in decToBin. Feel free to remove it.
I would change getNumber() such that it checks for the range and makes sure that you get a number between 0 to 255 from its return statement. That would eliminate the need to do that check in printBin.
Implementation of decToBin is quite a bit simpler than what you were thinking. The for-loop can be simplified to:
for(i=7; i>=0;i--, dec /= 2)
{
binarray[i]= dec % 2;
}
And finally, implementation of printBin can be a little bit different to make the output more readable. Instead of printing one number at a time, they can all be printed in one line.
Here's what I came up with:
#include <stdio.h>
int getNumber();
int decToBin(int, int binarray[]);
void printBin(int binary[], int dec);
int main()
{
int M = 8;
int binarray[M];
int dec = getNumber();
printf("The decimal number you entered was: %d\n", dec);
decToBin(dec, binarray);
printBin(binarray, dec);
return 0;
}
int getNumber()
{
int dec;
printf("Enter a number between 0 and 255: ");
scanf("%d",&dec);
if( dec < 0 || dec > 255)
{
printf("Please use another number\n");
return getNumber();
}
return dec;
}
int decToBin(int dec, int binarray[])
{
int i;
for(i=7; i>=0;i--, dec /= 2)
{
binarray[i]= dec % 2;
}
return *binarray;
}
void printBin(int binary[], int dec)
{
int i;
printf("The decimal number in binary: ");
for(i=0; i!=8; ++i)
{
printf("%d", binary[i]);
}
printf("\n");
}
A sample execution and output:
~>>./test-07
Enter a number between 0 and 255: 149
The decimal number you entered was: 149
The decimal number in binary: 10010101

Resources