Accept a number and print its alternate digits, - c

int Rearrange(int a)
{
long int b,j,i=0,num=0,count=0,arr[100];
while(a>0)
{
b=a%10;a=a/10;
arr[i]=b;
i++;
count ++;
}
j=count;
for(i=0;i<=count/2;i++)
{
t=arr[i];
arr[i]=arr[count-i-1];
arr[count-i-1]=t;
count--;
}
for(i=0;i<j;i+=2)
{
num=num*10 + arr[i]%10;
}
return num;
}
I want to write a function in c rearrange which prints the alternate digits of a number it is given.
for example:
input:- 12345
output:- 135
Thank you

Why complicating a simple problem?
If you don't mind an alternative approach, please check the below code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int input = 0;
int len = 0;
int i = 0;
char sinput[64] = {0, };
printf("Enter the number :");
scanf("%d", &input);
sprintf(sinput, "%d", input);
len = strlen(sinput);
printf("Output : ");
for (i = 0; i < len; i+=2)
{
printf("%c\t", sinput[i]);
}
printf("\n");
return 0;
}
Sample i/o:
[sourav#braodsword temp]$ ./a.out
Enter the number :123456
Output : 1 3 5
[sourav#braodsword temp]$

Your for loops are faulty. Change those to:
for(i=0;i<=count/2;i++)
{
int t=arr[i];
arr[i]=arr[j]; /* Use j */
arr[j]=t; /* Use j */
/* count--; Dont decrement */
j--;
}
for(i=0;i<count;i+=2) /* Should be count */
{
num=num*10 + arr[i]%10;
}
Demo
There can be many alternate ways to solve, but I just want to show you how the approach in thought process can be implemented correctly.

In your code problem is with the first for loop.
Please check the below code.
int Rearrange(int a)
{
long int b = 0, j = 0, i = 0, num = 0, count = 0, arr[100];
while (a > 0)
{
b = a % 10; a = a/10;
arr[i] = b;
i++;
count++;
}
j = count;
for (i = 0; i < count/2; i++) // Condition is problematic
{
long int t = arr[i];
arr[i] = arr[count-i-1];
arr[count - i - 1] = t;
// count--; // this is problamatic.
}
for (i = 0; i < j; i += 2)
{
num = num * 10 + arr[i] % 10;
}
return num;
}

#include<stdio.h>
int main()
{
int n,arr[40];
scanf("%d",&n);
printf("%d",n);
int s=0,i=0;
while(n!=0)
{
arr[i]=n%10;
printf("%d",arr[i]);
n=n/10;
i++;
}
for(int j=i-1;j>=0;j-=2)
{
s =s*10+arr[j];
}
printf("\n%d",s);
return 0;
}

int alternatedigits(int n)
{
int a[10],i=0,count=0,sum=0;
while(n!=0)
{
a[i]=n%10;
i++;
count++;
n=n/10;
}
for(int i=count;i>=0;i++)
{
if(i%2==0)
{
sum=sum*10+a[i];
}
}
return sum;
}

Related

where to put statement for powerful number?

I have make this program that calculates number factorization such as 60 = 2^2 * 5 * 3.
How can i modify my code in order to print POWERFUL NUMBERS such as 9000 = 2^3 * 3^2 * 5^3 without using math.h library and without using arrays?
Thank you very much!!
#include<stdio.h>
#define MAX 1000
int main(){
int num;
int counter;
int number;
char factorizationOutput;
int isAchiles = 0;
int factor=2;
for(counter=2;counter<=MAX;counter++){
isAchiles = 1;
number=counter;
int factor=2;
while(factor<number){
int power=0;
if(number%factor==0){
while(number%factor==0){
number=number/factor;
power++;
}
if(power == 1){
isAchiles = 0;
}
printf("%d^%d",factor,power);
if(number!=1)
printf(" X ");
}
factor++;
}
if(number!=1)
printf("%d^1.\n",factor);
if(isAchiles == 1){
printf("factorazation of number %d is: ",counter);
}
}
}
#include<stdio.h>
int main(void)
{
int n;
scanf("%d", &n);
printf("%d = ", n);
for(int i = 1; i <= n; i++)
{
int count = 0;
for(int j = 1; j <= i; j++)
{
if(i % j == 0)
{
count++;
}
}
int l = 0;
if(count == 2)
{
while(n % i == 0)
{
l++;
n = n/i;
}
printf("%d^%d*", i, l);
}
}
}

why the output of this program is wrong?.Frequency of number

Program for the frequency of a number
Please help me with this code to get clear output. I am a beginner
I have made the program using an array. I don't know whether it is correct or not. Made with my own logic
int count(int a)
{
int c;
while(a>=1)
{
c++;
a=a/10;
}
return c;
}
int main()
{
//program to find frquency of the number
int a,n,d;
int b[100];
int e[100];
scanf("%d",&a);
n=count(a);
for(int i=n;a>0;i--)
{
b[i]=a%10;
a=a/10;
}
for(int i=1;i<=n;i++)
{
d=b[i];
e[d]++;//most probably this part error occurs
printf("%d\n",d); //used this this to confirm that i have correctly stored value in d.
}
for(int i=1;i<=n;i++)
{
printf("%d ",e[i]);
}
return 0;
}
The line int c; should be int c = 0;
The line int e[100]; should be int e[100] = {0};
The following code could work:
#include <stdio.h>
int count(int a) {
int c = 0;
while (a >= 1) {
c++;
a = a / 10;
}
return c;
}
int main() {
// program to find frquency of the number
int a, n, d;
int b[100];
int e[100] = {0};
scanf("%d", &a);
n = count(a);
for (int i = n; a > 0; i--) {
b[i] = a % 10;
a = a / 10;
}
for (int i = 1; i <= n; i++) {
d = b[i];
e[d]++; // most probably this part error occurs
printf("%d\n", d); // used this this to confirm that i have correctly
// stored value in d.
}
for (int i = 1; i <= n; i++) {
printf("%d ", e[i]);
}
return 0;
}
Also, you can do it use snprintf:
#include <stdio.h>
int main() {
int a;
int max = -1;
char buf[100];
int count[10] = {0};
scanf("%d", &a);
snprintf(buf, sizeof(buf), "%d", a);
for (int i = 0; buf[i] != '\0'; ++i) {
int temp = buf[i] - '0';
++count[temp];
if (temp > max)
max = temp;
}
for (int i = 0; i <= max; ++i)
printf("%d ", count[i]);
return 0;
}

sort function won't print sorted array?

My sort function won't print the sorted array?
I'm trying to write a program that gathers array elements, sorts the array, then prints the factorial of each element. I don't want to get ahead of myself and write the recursive function if the array isn't being sorted correctly. The sort seems fine to me; people have criticized me using while loop but I don't know another way yet. Any input is appreciated.
#include <stdio.h>
int sorter(int numbList[]);
int getData(int numList[]);
//int recursive(int numList[]);
int main(void) {
int x;
int numberList[x];
getData(&numberList[x]);
sorter(&numberList[x]);
//recursive(&numberList[x]);
return 0;
}
//gets user input-data
int getData(int numbList[]) {
int i;
int x;
printf("Enter number of Elements:\n");
scanf("%d", &x);
printf("Enter the values for each element starting from first
element:\n");
for (i = 0; i < x; i++) {
scanf("%d", &numbList[i]);
}
printf("\nYou have filled the array list with\n");
for (i = 0; i < x; i++) {
printf("%d\n", numbList[i]);
}
return numbList[x];
}
//sorter function
int sorter(int numbList[]) {
int x;
int temp;
int swapped;
while (1) {
swapped = 0;
for (int i = 0; i < x; i++) {
if (i > numbList[i + 1]) {
temp = numbList[x];
numbList[x] = numbList[x + 1];
numbList[x + 1] = numbList[x];
swapped = 1;
}
if (swapped == 0) {
break;
}
}
printf("Array as sorted:\n");
for (int i = 0; i < x; i++) {
printf("%d\t", numbList[x]);
}
return(numbList[x]);
}
}
//recursive factorial function
/* int recursive(int numbList[]) {
int b = 0;
numbList[b] *= numbList[b - 1];
return 0;
} */
Some hints as comments in your code:
It still won't do the job, but get you in better shape...
int main(void)
{
//uninitialized x!
int x;
//Even if you get a value for x, VLAs are depreciated
int numberList[x];
//Both calls get the adress of last value + 1 in numberList.
//So you a) go out of the array bounds, and b) why would you want
//the last element's address??
//Just use it like getData(numberList);
getData(&numberList[x]);
sorter(&numberList[x]);
return 0;
}
//gets user input-data
//What is the return value for?
int getData(int numbList[])
{
int i;
int x;
printf("Enter number of Elements:\n");
scanf("%d",&x);
printf("Enter the values for each element starting from first element:\n");
for(i=0;i<x;i++){
scanf("%d",&numbList[i]);
}
printf("\nYou have filled the array list with\n");
for(i=0;i<x;i++){
printf("%d\n",numbList[i]);
}
//see above
return numbList[x];
}
//sorter function
//Again, what and why return?
int sorter(int numbList[])
{
//uninitialized x!
int x;
int temp;
int swapped;
while(1)
{
swapped=0;
for(int i=0;i<x;i++)
{
//What do you compare here? Think.
if(i>numbList[i+1])
{
temp=numbList[x];
numbList[x]=numbList[x+1];
//What do you have temp for??
numbList[x+1]=numbList[x];
swapped=1;
}
//Pretty sure you want an else clause here
if(swapped==0)
{
break;
}
}
printf("Array as sorted:\n");
for(int i=0;i<x;i++)
{
printf("%d\t",numbList[x]);
}
return(numbList[x]);
}
}
There are multiple problems in your code:
the number of elements x is uninitialized when you define the array numbList[x]. This has undefined behavior. You should pass a pointer to the count to getData and this function should update this value, allocate the array, read the values and return a pointer to the array.
You should not break strings on multiple lines without a \
The swap code is broken: the test if (i > numbList[i + 1]) is incorrect, it should be
if (numbList[i] > numbList[i + 1])
the swap code should use i instead of x as an index and the last assignment in the swap code should store temp into numbList[i + 1].
the inner loop should stop at x - 1 to avoid reading past the end of the array.
you should let the inner loop run to the end and break from the outer loop if swapped == 0.
Here is a corrected version:
#include <stdio.h>
#include <stdlib.h>
int *getData(int *count);
void sorter(int numList[], int count);
int main(void) {
int x;
int *numberList;
numberList = getData(&x);
if (numberList != NULL) {
printf("Elements entered:");
for (int i = 0; i < x; i++) {
printf(" %d", numberList[i]);
}
printf("\n");
sorter(numberList, x);
printf("Sorted array:");
for (int i = 0; i < x; i++) {
printf(" %d", numberList[i]);
}
printf("\n");
free(numberList);
}
return 0;
}
//gets user input-data
int *getData(int *countp) {
int i, x;
int *numbList;
printf("Enter the number of elements: ");
if (scanf("%d", &x) != 1 || x <= 0) {
printf("Invalid size:");
return NULL;
}
numbList = calloc(sizeof *numbList, x);
if (numbList == NULL) {
printf("Memory allocation error:");
return NULL;
}
printf("Enter the element values: ");
for (i = 0; i < x; i++) {
if (scanf("%d", &numbList[i]) != 1) {
free(numbList);
return NULL;
}
}
*countp = x;
return numbList;
}
//sorter function
void sorter(int numbList[], int x) {
for (;;) {
int swapped = 0;
for (int i = 0; i < x - 1; i++) {
if (numbList[i] > numbList[i + 1]) {
int temp = numbList[i];
numbList[i] = numbList[i + 1];
numbList[i + 1] = temp;
swapped = 1;
}
}
if (swapped == 0) {
break;
}
}
}
You can use bubble sort algorithm technique which is fast sorting algorithm and it uses for loop instead of while loop
int bubbleSorter(int numbList[])
{
int temp;
int i, x;
bool swapped = false;
for (i = 0; i < x - 1; i++)
{
swapped = false;
for (j = 0; j < x - 1 - i; j++)
{
if (list[j] > list[j + 1])
{
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
swapped = true;
}
else {
swapped = false;
}
}
// if no number was swapped that means
// array is sorted now, break the loop.
if (!swapped) {
break;
}
printf("Array as sorted:\n");
for (int i = 0; i<x; i++)
{
printf("%d\t", numbList[x]);
}
return(numbList[x]);
}
}

how to generate number pattern in triangular form [duplicate]

I want to print this pattern like right angled triangle
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
I wrote the following code:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
for(j=1;j<=f;j++,k--)
{
k=i;
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
for(x=1;x<f;x++,z--)
{
z=9;
printf("%d",z);
}
printf("%d/n");
}
getch();
}
What is wrong with this code? When I check manually it seems correct but when compiled gives different pattern
Fairly simple: use two loops, one for counting up and one for counting down. Print literal "0" between the two.
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 10 - i; j < 10; j++)
printf("%d", j);
printf("0");
for (int j = 9; j >= 10 - i; j--)
printf("%d", j);
printf("\n");
}
return 0;
}
Like H2CO3's, but since we're only printing single digits why not use putchar():
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
for(i = 0; i < 10; ++i)
{
// Left half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
// Center zero.
putchar('0');
// Right half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
putchar('\n');
}
return EXIT_SUCCESS;
}
Modified Code:
Check your errors:
# include<stdio.h>
# include<conio.h>
int main()
{
// clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
k=i; // K=i should be outside of loop.
for(j=1;j<=f;j++,k++)
{
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
z=9; //z=9 should be outside loop.
for(x=1;x<f;x++,z--)
{
printf("%d",z);
}
printf("\n");
}
//getch();
return 0;
}
You are defining k=i inside the for loop(loop which has j) so every time k gets value of i and thus it always get value of i and prints that value and your another condition(if(k==10)) will never be true because every time k takes value of i and i is less than 10 after first iteration of loop and z=9 inside loop so every time loop is executed it is taking value z=9 so it is printing wrong value.
Here's a C# version:
static void DrawNumberTriangle()
{
for (int line = 10; line >=1; line--)
{
for (int number = line; number < 10; number++)
{
System.Console.Write(number);
}
System.Console.Write("0");
for (int number = 9; number > line - 1; number--)
{
System.Console.Write(number);
}
System.Console.WriteLine();
}
}
I'd suggest renaming your i,j,x,z,k,f variables to ones that have meaning like the one's I used. This helps making your code easier to follow.
Rather than output the mid 0 using printf, why not print it using the loops itself.
The following short and simple code can be used:
int main()
{
int m = 10, n, p;
while(m >= 1)
{
for(n = m; n <= 10; n++)
printf("%d", n % 10);
for(p = n - 2; p >= m; p--)
printf("%d", p );
printf("\n");
m--;
}
return 1;
}
For high throughput (though of questionable merit in terms of clarity):
#include <stdio.h>
int main() {
char const digits[] = "1234567890";
char const rdigits[] = "9876543210";
for (int i = 0; i < 30; ++i) {
int k = i % 10;
fputs(digits + 9 - k, stdout);
for (int j = 9; j < i; j += 10) fputs(digits, stdout);
for (int j = 9; j < i; j += 10) fputs(rdigits, stdout);
fwrite(rdigits, 1, k, stdout);
fputs("\n", stdout);
}
}
#include <stdio.h>
void print(int i){
if(i == 10){
putchar('0');
return ;
} else {
printf("%d", i);
print(i+1);
printf("%d", i);
}
}
int main(void){
int i;
for(i = 10; i>0; --i){
print(i);
putchar('\n');
}
return 0;
}

How do I generate number pattern in triangular form

I want to print this pattern like right angled triangle
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
I wrote the following code:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
for(j=1;j<=f;j++,k--)
{
k=i;
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
for(x=1;x<f;x++,z--)
{
z=9;
printf("%d",z);
}
printf("%d/n");
}
getch();
}
What is wrong with this code? When I check manually it seems correct but when compiled gives different pattern
Fairly simple: use two loops, one for counting up and one for counting down. Print literal "0" between the two.
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 10 - i; j < 10; j++)
printf("%d", j);
printf("0");
for (int j = 9; j >= 10 - i; j--)
printf("%d", j);
printf("\n");
}
return 0;
}
Like H2CO3's, but since we're only printing single digits why not use putchar():
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
for(i = 0; i < 10; ++i)
{
// Left half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
// Center zero.
putchar('0');
// Right half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
putchar('\n');
}
return EXIT_SUCCESS;
}
Modified Code:
Check your errors:
# include<stdio.h>
# include<conio.h>
int main()
{
// clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
k=i; // K=i should be outside of loop.
for(j=1;j<=f;j++,k++)
{
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
z=9; //z=9 should be outside loop.
for(x=1;x<f;x++,z--)
{
printf("%d",z);
}
printf("\n");
}
//getch();
return 0;
}
You are defining k=i inside the for loop(loop which has j) so every time k gets value of i and thus it always get value of i and prints that value and your another condition(if(k==10)) will never be true because every time k takes value of i and i is less than 10 after first iteration of loop and z=9 inside loop so every time loop is executed it is taking value z=9 so it is printing wrong value.
Here's a C# version:
static void DrawNumberTriangle()
{
for (int line = 10; line >=1; line--)
{
for (int number = line; number < 10; number++)
{
System.Console.Write(number);
}
System.Console.Write("0");
for (int number = 9; number > line - 1; number--)
{
System.Console.Write(number);
}
System.Console.WriteLine();
}
}
I'd suggest renaming your i,j,x,z,k,f variables to ones that have meaning like the one's I used. This helps making your code easier to follow.
Rather than output the mid 0 using printf, why not print it using the loops itself.
The following short and simple code can be used:
int main()
{
int m = 10, n, p;
while(m >= 1)
{
for(n = m; n <= 10; n++)
printf("%d", n % 10);
for(p = n - 2; p >= m; p--)
printf("%d", p );
printf("\n");
m--;
}
return 1;
}
For high throughput (though of questionable merit in terms of clarity):
#include <stdio.h>
int main() {
char const digits[] = "1234567890";
char const rdigits[] = "9876543210";
for (int i = 0; i < 30; ++i) {
int k = i % 10;
fputs(digits + 9 - k, stdout);
for (int j = 9; j < i; j += 10) fputs(digits, stdout);
for (int j = 9; j < i; j += 10) fputs(rdigits, stdout);
fwrite(rdigits, 1, k, stdout);
fputs("\n", stdout);
}
}
#include <stdio.h>
void print(int i){
if(i == 10){
putchar('0');
return ;
} else {
printf("%d", i);
print(i+1);
printf("%d", i);
}
}
int main(void){
int i;
for(i = 10; i>0; --i){
print(i);
putchar('\n');
}
return 0;
}

Resources