38.3k views
1 vote
Q : Write a simple code, which basically takes a integer value from PC terminal and calculates the solutions on Arduino board and replays the answers to the PC terminal.

a. Power of given value.


b. If number is prime number or not.


you should upload your answers with *.c file with necessary comments.

1 Answer

7 votes

Answer:

I have uploaded my answers with *.c file with necessary comments.

Step-by-step explanation:

a. Power of given value.

A

#include <stdio.h>

int main() {

int base, exp;

long long result = 1;

printf("Enter a base number: ");

scanf("%d", &base);

printf("Enter an exponent: ");

scanf("%d", &exp);

while (exp != 0) {

result *= base;

--exp;

}

printf("Answer = %lld", result);

return 0;

}

b. If number is prime number or not.

#include <stdio.h>

int main() {

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

for (i = 2; i <= n / 2; ++i) {

// condition for non-prime

if (n % i == 0) {

flag = 1;

break;

}

}

if (n == 1) {

printf("1 is neither prime nor composite.");

}

else {

if (flag == 0)

printf("%d is a prime number.", n);

else

printf("%d is not a prime number.", n);

}

return 0;

}

Hope this helps!!!

User Constructor
by
4.8k points