Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9286269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:22:33+00:00 2026-06-18T19:22:33+00:00

printf(\n\tHow many integers: ); scanf(%d, &num); iPtr = (int*) malloc(num * sizeof(int)); for(i =

  • 0
printf("\n\tHow many integers: ");
    scanf("%d", &num);

    iPtr = (int*) malloc(num * sizeof(int));

    for(i = 0; i < num; i++) {
        printf("Enter integer # %d ", i + 1);
        scanf("%d", (iPtr + i));

        temp = *(iPtr + i);
        while(temp != 0) {

            if(i == temp % 10) {
                ary[i]++;
            }
            temp /= 10;
        }
    }

    for(i = 0; i < 10; i++) {
        if(ary[i] > 0) {
            printf("digit %d : %d\n", i, ary[i]);
        }
    }

I am writing a program to store the occurrences of an digit from an integers,but when I run the program, I see the array is not working

so I want to ask why it is not allow me to store the digit from an integer?
and how to fix it?

and 1 more question, what is the different between if(x = y) and if(x == y)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:22:34+00:00Added an answer on June 18, 2026 at 7:22 pm

    Ok so you’ve almost got it right but I think the biggest issue is your array has garbage in it to begin with (when declared).

     int i, temp, num, *iPtr;
    int ary[10] = {0};            //I can only assume you did *not* do this
    
    //printf("\n\tHow many integers: ");
    //scanf("%d", &num);
    num = 2;
    
    iPtr = malloc(num * sizeof(int));
    
    
    
    for(i = 0; i < num; i++) 
    {
        printf("Enter integer # %d ", i+1);
        scanf("%d", (iPtr +1));
    
        temp = *(iPtr+1);
        while(temp != 0) 
        {
    
            if( (i) == temp % 10)     
            {
                ary[i]++;             
            }
    
            temp /= 10;
        }
    }
    
    for(i = 0; i < 10; i++) 
    {
        if(ary[i] > 0) 
        {
            printf("digit %d : %d\n", i, ary[i]);
        }
    }
    

    This program is also incomplete, because your loop might end before you’ve even checked a digit high enough to match. Ie, you’re checking if temp still has a number in it, but you’re only comparing it to i, which doesn’t change (so if the digit in temp is 9, but i is 1, you’ll never trigger to see that there’s a 9 to count).

    So I modified

    if( (i) == temp % 10)     
    {
        ary[i]++;             
    }
    

    to

            for (int zz = 0; zz < 10; zz++)
            {
                if( (zz) == temp % 10) 
                {
                   ary[zz]++;            
                }
            }
    

    And now the program counts all the digits.
    Output:enter image description here

    This is all assuming I’m even understanding your program right. And usually I don’t like straight posting code but I really didn’t know how else to get the point across. Tired atm. :/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code printf(\n); printf(Enter a integer vaule:); scanf(%d , &num3); printf(You entered:
printf(enter the string); scanf(%[^\t\n]s,str); //how the abv code works to accept multi word string
printf(Please enter the start date of your trip Month/Day/Year seperated by a space:); scanf(%d
Program: int main( ) { printf(%d,printf(%d %d,5,5)&printf(%d %d,7,7)); return 0; } Output: 5 57
include<stdio.h> int main() { //char b[10]; char *a=goodone; //a=b; scanf(%s,a);//this scanf fails and thow
$ printf 'int main(){}' | gcc -static -x c - -o hello $ scp
Why the two printf statements are outputting different values? int main() { int n=10;
printf(pointer: %d\n, sizeof(*void)); This line results in a syntax error because of the *.
Possible Duplicate: Using printf function #include<iostream> using namespace std; int main() { long long
For example printf instead of cout , scanf instead of cin , using #define

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.