How to double digits in number - c

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;
}

Related

I want to accept only odd numbers

I am trying to write a code to accept only odd number. If the number is even, it will ask for another number until an odd number is entered.
If the number is odd, it will continue to run the remaining of the program using that odd value.
I am not sure how to write the syntax for the for loop in this program.
Here is part of the odd number validator code:
#include <stdio.h>
int main()
{
int num;
printf("Enter odd value for n: ");
scanf("%d", &num);
for(num; num % 2 == 0; num)
{
printf("%d is even. TRY AGAIN!: ");
scanf("%d",&num);
}
}
`#include <stdio.h>
int main()
{
int num;
odd: //used label
printf("Enter odd value for n: ");
scanf("%d", &num);
if(num%2==0)
{
printf("%d is even. TRY AGAIN!: \n",num);
goto odd;
}
printf("%d is Odd ",num);
}

C program giving extremely wrong answer

Program takes an integer input num from the keyboard and computes the sum of square of i for all I from 1 to num (inclusive).
#include <iostream>
#include <math.h>
int main()
{
int num;
int total;
printf("Please enter a number:");
scanf("%d", &num);
for (double i = 1; i <= num; i++) {
total += (i*i);
printf("%d", total);
}
}
The code above compiles correctly, but when inputting 5 it prints 15143055. Why is it doing this?
#include <iostream> is c++. #include <math> is not used. total is uninitialized. Comparing floating point values may not behave the way you want, so using a (unsigned) integer type instead as a loop counter. Loop values by convention start at 0 instead of 1 in c (you could increment i fist thing in the loop to avoid the double (i+1) but this will be optimized out anyways). Also, your loop not run for a negative value so just require unsigned values. As you sum integers the result ought to be an integer, but double would give you a larger range so I left it as such. Missing return:
#include <stdio.h>
int main() {
unsigned num;
printf("Please enter a number: ");
scanf("%u", &num);
double total = 0.0;
for (unsigned i = 0; i < num; i++) {
total += (i+1)*(i+1);
}
printf("%lf\n", total);
return 0;
}
and the resulting output:
Please enter a number: 3
14.000000

c program for finding the highest divisible number is not working

So I am new to C programming. The purpose of this program is to use a function to find the largest number divisible. Three numbers should be given and the answer should be the number that is higher than the first number and lower than the second number and should be the largest number between them that can be divided evenly by the third number.
#include <stdio.h>
#include <stdlib.h>
int my_function(int first, int second, int third) {
int i, answer;
for (i = second; i <= 0; i--) {
if (i < first || i > third) {
answer = 0;
break;
}
if (i % third == 0 && i != 0 && i > first)
answer = i;
}
return answer;
}
int main() {
printf("enter number one:\n");
int one, two, three, final;
scanf("%d", &one);
printf("enter number two\n");
scanf("%d", &two);
printf("enter number three\n");
scanf("%d", &three);
final = my_function(one, two, three);
printf("the number is %d", final );
return 0;
}
This program is not working. Can someone help me with my mistake?
Your function does not work for multiple reasons:
the loop test is always false if second is a positive number.
the test i > third will cause an early exit from the loop for no good reason.
when your find a divisible number, you should then exit the loop.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
int my_function(int first, int second, int third) {
int i;
for (i = second - 1; i > first; i--) {
if (i % third == 0)
return i;
}
return 0;
}
int main() {
int one = 0, two = 0, three = 0, final;
printf("enter number one:\n");
scanf("%d", &one);
printf("enter number two\n");
scanf("%d", &two);
printf("enter number three\n");
scanf("%d", &three);
final = my_function(one, two, three);
printf("the number is %d\n", final);
return 0;
}

How to increase th range of integers in C?

I tried to solve the Small Factorial problem on SPOJ and got 'Wrong answer'. I wrote the following code :
#include <stdio.h>
int main() {
int t,i, num;
unsigned long long int fact=1;
scanf ("%d", &t);
while (t-- >0) {
fact=1;
scanf ("%d", &num);
for (i=num; i>0; i--) {
fact*=i;
}
printf ("%llu\n",fact);
}
return 0;
}
This code is not finding factorial for large inputs like 100. What are the changes required?
In C and C++ you have the maximum allowed range from -2^63+1 to +2^63-1 which is for long long data type. As you can see this range is still too small to store >20 digits.
You have to use a char array to store single digit per array index.
one way to do this in C++:
#include<stdio.h>
#include<iostream>
int main()
{
int t;
char a[200]; //array will have the capacity to store 200 digits.
int n,i,j,temp,m,x;
scanf("%d",&t); //enter total elements
while(t--)
{
scanf("%d",&n); // enter no. one at a time
a[0]=1; //initializes array with only 1 digit, the digit 1.
m=1; // initializes digit counter
temp = 0; //Initializes carry variable to 0.
for(i=1;i<=n;i++)
{
for(j=0;j<m;j++)
{
x = a[j]*i+temp; //x contains the digit by digit product
a[j]=x%10; //Contains the digit to store at position j
temp = x/10; //Contains the carry value that will be stored on later indeces
}
while(temp>0) //while loop that will store the carry value on array.
{
a[m]=temp%10;
temp = temp/10;
m++; // increments digit counter
}
}
for(i=m-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
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;
}

Resources