Hi everyone I couldn't find the actual logic behind below two problems:

(1) how to capture number of occurrences of 3 between 1 to 100? and (2) assigns a function to a pointer and subsequently call it.

all problems are in c++

As I'm new in this field so,please help me to sweep out the problems.
Thanks and regards
sdmahapatra

Recommended Answers

All 16 Replies

"(1) how to capture number of occurrences of 3 between 1 to 100? "

Like this : 3,13,23,33,43,54,63,73,83,93 ?

ike this : 3,13,23,33,43,53,63,73,83,93 ?

yes like this.

>>"(1) how to capture number of occurrences of 3 between 1 to 100? "

Write a for loop that counts from 1 to 100. Convert each int value to a string then test the string if it contains a '3'.

2) Do you know how to declare a function pointer? Almost like declaring a function prototype. For example, if you have a function that takes an int parameter and returns an int, then a function pointer might be int (*fn)(int);

ike this : 3,13,23,33,43,53,63,73,83,93 ?

yes like this.

Looking at this you don't even need a for loop.

Look at the pattern : 3,13,23,33,43,53,63,73,83,93,103,113,123,133...

It seems that there is 10 3's in every 100;

so ask user for max num;

set maxNum %= 100;

multiply maxNum by 10; and you have your answer.

>>It seems that there is 10 3's in every 100;
There are 19 numbers between 1 and 100 that contain one or more 3s. 3 13 23 30 31 32 33 34 35 36 37 38 39 43 53 63 73 83 93 I thought about the mod operator too, but it won't work. Example: Enter max num = 100. 100 % 100 == 0, which is clearly wrong answer.

ike this : 3,13,23,33,43,53,63,73,83,93 ?

yes like this.

30, 31, 32, 34, 35, ...?

Oh wait My bad.

If maxNum is multiple of 100 the I guess you can do this:

if(maxNum%100==0)
maxNum %=99

max*=19;

A common way to separate a number into digits is to use % and / like this.

#include <stdio.h>

int main()
{
    int i;
    for ( i = 1; i <= 100; ++i )
    {
        int value = i;
        printf("value = %d\n", value);
        while ( value )
        {
            int digit = value % 10;
            printf(" digit = %d\n", digit);
            /* now if there were only some way to check if digit were 3 here */
            value /= 10;
        }
    }
    return 0;
}

You might find 20 3's from 1 to 100 if you counted both 3's in 33.

Hi my friend Ancient Dragon, thank you very much for your kind concern on my problem.Yes its true I was not aware on Function Pointer and with your awareness I solved the second problem.It's really a great concept in the programming language.The logic of the first problem almost like my friend Dave Sinkula has said as at the number 33 number 3 has come twice.So number of occurrence of 3 is equal to 20.Actually it my mistake that I'm not clearly express my problem. And first problem also be solved.

Once again thank you for your help.

dear friend firstperson,I'm very thankful that you are really a first person to concern on my problem.As on my problem is about the number of occurrence of 3 between 1 to 100 so it should be 20 as at number 33,3 comes twice.It is my mistake to express the problem clearly.And I've solved this problem with all of your suggestion because logic behind this problem is same for everyone.
Thank you very much for your suggestion and I'll be glad if you will be with me as my friend.

My friend Dave Sinkula. I'm thankful to you as you give the deep attention on my first problem.Your logic is almost same,almost because in your program all the digit has been shown separately but digit should be show when 3 has occurred and simultaneously count each time of occurrence and shown when it occurred and number of total occurrence.So this is only some modification my friend. And second problem has also been solved with the awareness of Ancient Dragon.At the end I can only say be my friend as right now.

Thank you very much for your suggestion Dave Sinkula.

My friend VernonDozier, as I said number of occurrence of 3 so

30, 31, 32, 34, 35, ...?

should also be count.I'm really thank full to your for your attention on my problem.Whenever I'll face any problem on programming language you pleas be with me.

As my two problem solved so I am marking it as solved.Everyone keeps their views and ultimate problem has solved.It's really a very good team work.

Thanks and Regards
sdmahapatra

hey FirstPerson, where did you found the algorithm? Is it proven and works on any other number?


Regards,
Nick

hey FirstPerson, where did you found the algorithm? Is it proven and works on any other number?

Regards,
Nick

which 1?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.