WAP to print the sum of all the PRIME or EVEN integers in the array and ignore the remaining.

Dani AI

Generated

A simple, reliable approach is to scan the input string and extract each contiguous run of digits as a single integer, then test that integer and add it to the total when it is prime or even. This follows 's strtol suggestion and fills the logic hole in 's skeleton: let the library parse numbers and advance a pointer instead of assembling digits by hand.

Algorithm (high level)

  1. Read the input into a buffer.
  2. Walk the buffer with a pointer p. Call strtol(p, &end, 10).
  3. If end == p then p++ (no number at p). Otherwise val = strtol(...); advance p = end.
  4. If val meets your policy (e.g., non-negative) and (is_prime(val) || (val % 2 == 0)), add it to sum.
  5. Print sum.

Example C implementation (compact, uses strtol and a simple sqrt-based primality check):

#include <stdio.h>
#include <stdlib.h>

int is_prime(long n) {
    if (n < 2) return 0;
    if (n % 2 == 0) return n == 2;
    for (long i = 3; i * i <= n; i += 2)
        if (n % i == 0) return 0;
    return 1;
}

int main(int argc, char **argv) {
    char buf[4096];
    if (argc > 1) snprintf(buf, sizeof buf, "%s", argv[1]);
    else if (!fgets(buf, sizeof buf, stdin)) return 0;
    char *p = buf, *end;
    long sum = 0;
    while (*p) {
        long v = strtol(p, &end, 10);
        if (end == p) { ++p; continue; }
        if (v >= 0 && (is_prime(v) || (v % 2 == 0))) sum += v;
        p = end;
    }
    printf("%ld\n", sum);
    return 0;
}

Notes and troubleshooting: this version ignores negative integers (change v >= 0 if you want to include them). The primality test is fine for typical embedded numbers; for very large extracted values use a stronger test (Miller–Rabin). Try this and post your failing input or your attempt if you need help adapting it — the hints from and about trying code first are still good practice.

Recommended Answers

All 9 Replies

OK. What have you tried so far?

Given an array of random characters, integers and symbols, WAP to print the sum of all the PRIME or EVEN integers in the array and ignore the remaining. Consider consecutive numbers as a single number

Example 1: given the below input the output will be 2+13+8+22=45

Given Input [2GSD#13x8xU343%^DGF1@22@]

Expected output 45

Example 2: given the below input the output will be 11+23+164+44=242

Given Input x11@23@33S164DFS44_+}|#555

Expected output 242

what is code to solve that que...?

what is code to solv

What is WAP? Google says it's " Wireless Appication Protico", but I doubt that's what you are talking about.

If you haven't done something then start coding.

Take user input, Then check that each character is int or not.
Then apply prime no. logic to check wether it is prime or not and then sum it up.

And then with your effort of coding, post specific doubt in it.

What is WAP?

i think OP is newbie and doesn't familiar with daniweb homework policy. He take WAP as Write A Programme , i think.

WAP to print the sum of all the PRIME or EVEN integers in the array and ignore the remaining.

if you think that somebody here will write program for you then i think you are at wrong place ,in order to get assistance you need to prove that you made efforts to solve this question(you can prove this by showing us your code you have so far).

create an array variable and then give input to it. then check each element if they are numbers , if they are numbers then check if they are prime or even number then add the number to a variable(sum+=arr[i]).don't forget to initialize the sum variable to 0.

He take WAP as Write A Programme

Yes -- that makes sense. I'll write the program for him, but first me must deposit $10,000.00 USD in my PayPay account (as I've told others like him)

WAP meanc "Write A Program"

Ancient Dragon ...just give mee hint about that program...

Here you go....

#include <stdio.h>

#define ARR_SIZE 10

int main()
{
    size_t i = 0;
    char chr_arr[ARR_SIZE];

    /*populate int_arr*/

    for (; i < ARR_SIZE; ++i)
    {
        if (chr_arr[i] /*meets some condition*/)
        {

        }
    }
    return 0;
}

Now lets see you write the condition part and populate the array.

One hint that I will provide is that a large amount of your work will be reduced if you use something like strtol

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.