Function that will find a prime number in an array of numbers - c

The function takes three elements of the array , and of those three elements it finds the first prime number and puts it on first position,then it moves to the next element and finds a prime number between those three elements,if none of those numbers are prime , there is no transforming. The last two numbers stay untouched. Here is my code , having some trouble with it.Would like some help.
To make it clearer , first three items lets say A[i] A[i+1] A[i+2] The prime number is put on A[i]
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define ITEMS 10
int Transform(int *num,int k,int j,int x){
while(j!=ITEMS-3){
if(num[j]%2==0)
break;
if(num[j]%2!=0)
num[x]=num[j];
j++;
k++;
x++;
if(num[k]%2==0)
break;
if(num[k]%2!=0)
num[x]=num[k];
j++;
k++;
x++;
if(num[x]%2==0)
break;
if(num[x]%2!=0)
num[x]=num[x];
j++;
k++;
x++;
}
}
int main(void){
int num[ITEMS];
int i,j,k,x,pom;
i=0;
k=i+1;
j=i+2;
x=i;
for(i=0;i<ITEMS;i++){
printf("Enter the array : /n");
scanf("%d",num[i]);
pom=Transform(num,j,k,x);
printf("%d",pom);
}
return 0;
}

Given int num[ITEMS] you probably meant:
scanf("%d", &num[i]);
^
If you omit that you trick scanf into thinking that whatever is in num[i] is an address.

You want to find the first prime of every three numbers in an array.
for(int i = 0; i < len; i += 3)
for(int j = i; j < i+3; j++)
if(isPrime(nums(j)))
{
primes[i/3] = nums[j];
break;
}
The code is something like this, but I have not tested it.

Related

C program to read 'n' numbers and find out the sum of odd valued digits of each number and print them

i am new to programing, i want to know that how we can find the odd digits in a number.
the condition in this program is we should only use concept of arrays.I tried a code for this as follows:
#include <stdio.h>
int main()
{
int A[50],i,x,y,n,sum=0;
scanf("%d",&n);
printf("the value is %d\n",n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
for(i=0;i<n;i++){
x=A[i]%10;
if(x%2!=0)
sum=sum+x;
A[i]=A[i]/10;
printf("the sum of odd numbers is %d\n",sum);
}
return 0;
}
but in this the code is checking for only one digit of the first number in the loop and then next time it is going to check the digit of second number.
so, i need to write a code to check all digits in the number and then it goes to next number and check all digits and should continue the same process in the loop.So, for this how should i modify my code?
You were missing a loop that would iterate through every digit of A[i] - the inner while loop below,
#include <stdio.h>
int main()
{
int A[50], i, x, y, n, sum=0;
printf("How many numbers will you input?\n");
scanf("%d",&n);
printf("the value is %d\n",n);
for(i=0; i<n; i++) {
scanf("%d",&A[i]);
}
for(i=0; i<n; i++) {
sum = 0;
while (A[i] > 0) {
x = A[i]%10;
if(x%2 != 0) {
sum = sum + x;
}
A[i] = A[i]/10;
}
printf("the sum of odd numbers is %d\n",sum);
}
return 0;
}
The exact algorithm for iterating through each digit in a nice form can be found in this post - although for a different language. Here, apart from the while loop, you also need to reset the sum each time unless you want a cumulative sum over all provided numbers.
Note that I changed the formatting a bit - more space, extra braces, and a message about what you're prompting the user to input.
int temp;
int sum = 0;
temp = number;
do {
lastDigit = temp % 10;
temp = temp / 10;
sum += (lastDigit %2 != 0) ? lastDigit : 0;
} while(temp > 0);

(Lotto 6/49 simulation) Calculate how many draws will take place until a user-entered combination hits the jackpot

I have my code and data for my Lotto 6/49 simulation. I just don't know how to create a simple mathematical expression to calculate how many draws will take place until the user hits the jackpot
Code:
#include <stdio.h>
#include <stdlib.h> //need this library to use srand and rand without warnings
#include <time.h> //need this library to use time function
int main(void) {
int i, j, k , temp, count1=0;
unsigned int count2=1;
int select[6];
int jackpot[6];
printf("Welcome to Lotto 6/49! The 6 numbers you choose must match the 6 randomly chosen numbers in the lottery in order to win the jackpot. Select your 6 numbers!\n\n");
for(i=0; i<6; i++)
{
printf("Enter a number between 1-49: ");
scanf("%d", &select[i]);
}
srand(time(NULL));
for(;;){
for(i=0; i!= 6 ; ) //This loop produces the winning number combination
{
temp= (rand() %49)+1; //produce a random number between 1 and 49
for( k =0 ; k<i && (temp!=jackpot[k]) ; k++); //make sure we do not have duplicate
if (k==i){ //only write to the array if we do not have a duplicate
jackpot[i] = temp;
i++; //only increment the loop if we write a value to the array
}
}
printf("\nThe winning numbers are: "); //displays the winning numbers
for(i=0;i<6;i++)
printf("\t%d\t",jackpot[i]);
printf("\n");
// This loop will check the user array with the jackpot array to determine if it is a winning combination
for(i=0; i<6 && i ==count1; i++){ //if we do not match the first element, we skip the rest
for(j=0; j<6 ; j++)
{
if(select[i]==jackpot[j]){
count1+=1; // only increment if we match
}
}
}
if(count1==6){ //all 6 numbers matched
printf("\n\nYou win the JACKPOT! Congratulations! \nIt took you %d draws to win", count2);
break;
}
else{
count2+=1;
count1=0; //reset match count
}
}
return 0;
}
Data (number range limited to 8 numbers for easier recording, eg temp= (rand() %8)+1 instead of temp= (rand() %49)+1 # line 19.)
Count1 = user input matched
Count2 = # of draws
↑
I have no clue how to calculate this

How to compare the digits of 2 integer numbers in c (without arrays and strings)

I am making simple example of little game about guessing numbers.
And I want to build a function which check the numbers and make two values as follows:
1) hits-the number of digits that contain in both number and in same place for both numbers.
2) misses-the number of the digits which contain in both number but not in the same place.
For example:
int systemNumber=1653;
int userGuess=5243;
in this example, in both numbers there are the digits 5 and 3. In both numbers the digit 3 in the same place. But, the digit 5 in systemNumber is not in the same place as userNumber. So, we have here 1 hit and 1 miss.
I've written the code for it with arrays, and I'd like to know if there is a way that I will be able to do this without array and strings.
Here is my code. Please, if you have any improvement for my code, I'd like to know it :)
#include <stdio.h>
#include <stdlib.h>
void checkUserCode(int num1[4], int num2[4]); // declare the function which check the guess
int hits=0, misses=0; // hits and misses in the guess
int main(void)
{
int userCode=0;
int userCodeArray[4];
int systemCodeArray[4]={1, 4, 6, 3};
int i=0;
// printing description
printf("welcome to the guessing game!\n");
printf("your goal is to guess what is the number of the system!\n");
printf("the number have 4 digits. Each digit can be between 1 to 6\nGood Luck!\n");
// input of user guess
printf("enter number: ");
scanf("%d", &userCode);
for (i=3; i>=0; i--)
{
userCodeArray[i]=userCode%10;
userCode=userCode/10;
}
checkUserCode(systemCodeArray, userCodeArray);
printf("there are %d hits and %d misess", hits, misses); // output
return 0;
}
/*
this function gets two arrays and check its elements
input (parameters): the two arrays (codes) to check
output (returning): number of hits and misses
if the element in one array also contains in the other array but not the same index: add a miss
if the element in one array also contains in the other array and they have the same index: add a hits
*/
void checkUserCode(int num1[4], int num2[4])
{
int i=0, j=0;
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
if(num1[i]==num2[j])
{
if (j==i)
hits++;
else
misses++;
}
}
}
}
Here is an example I wrote a while ago, which I tweaked for your problem:
I basically uses two for loops, the outer loop going over the first number, 1653, and the inner loop going over the second number, 5243. It basically compares each individual number in the first number against all the numbers in the second number.
Depending on the counters, it evaluates if equal numbers have been matched in the same positions, using modulo %10 to compare each number.
This is the code:
#include <stdio.h>
#include <stdlib.h>
int
main(void) {
int num1 = 1653;
int num2 = 5243;
int pos1, pos2, hit = 0, miss = 0, i, j;
pos1 = 0;
for (i = num1; i > 0; i /= 10) {
pos2 = 0;
for (j = num2; j > 0; j /= 10) {
if (i % 10 == j % 10) {
if (pos1 == pos2) {
hit++;
} else {
miss++;
}
}
pos2++;
}
pos1++;
}
printf("hits = %d\n", hit);
printf("misses = %d\n", miss);
return 0;
}

minimal primes generator in C

I have to find all the minimal primes that have at least two digits. A minimal prime is a prime number which subsequences are not primes. In order to correctly complete the exercise I cannot use arrays (they are prohibited) and I must #define the maxnumb. The problem I have here is that I cannot print the min. What I have so far is this:
#include <stdio.h>
#define MAXNUMB 100
int IsPrime(int number);
int main (void) {
int i,j,x,k,mask,max=1,mult,sub,c,number;
for (i = 11 ; i < MAXNUMB; i += 2 ){
number=IsPrime(i);
if (number==1) {
int length = 0;
int tmp=i;
while (tmp != 0) {
tmp /= 10;
length++;
}
for (x=1;x<length*2;x++) {
mask=x;
mult=1;
sub=0;
int num=i;
int counter=0;
while(num!=0) {
if ( mask % 2 == 1 ) {
sub += num % 10 * mult;
mult *= 10;
}
num /= 10;
mask /= 2;
}
while (sub!=i){
int min=i;
k=IsPrime(sub);
if(k==1)
counter+=1;
printf("%d sub \n",sub);
printf("%d count \n",counter);
break;
if(counter==0)
printf(" minimal \n",min);
}
}
}
}
return 0;
}
int IsPrime(int i) {
int j;
if (i==1)
return 0;
for (j=2; j*j<=i; j++) {
if (i % j == 0)
return 0;
}
if (j*j>i )
return 1;
}
It is because you have a break statement above the line in question, so it never gets executed.
printf("%d sub \n",sub);
printf("%d count \n",counter);
break; // <<-- remove this line
if(counter==0)
printf(" minimal \n",min);
An additional problem is you don't print any value, change to this
if(counter==0)
printf("%d minimal \n",min); // add format specifier
This is because you're not telling it to print min.
Change this line:
printf(" minimal \n",min);
To this:
printf("%d minimal \n",min);
To achieve what I think you were trying to do.
printf prints variables using format specifiers. It would be worth reading a few tutorials on how to use them :).
I can also see an infinite loop at while (sub!=i) since sub doesn't change inside the loop.
To further test me add a printf("Problematic loop") inside the loop and see it yourself how many times it will print the statement without printing a number.

C program to print next five prime numbers

I have a program that prints five prime numbers within a user input range(m,n).
My problem is i want to print numbers greater than m. and print only next five numbers. I don't want to use upper limit.
How can I do it so?
#include <stdio.h>
#include <conio.h>
int main()
{
int m,n,i,j,k,flag;
printf("\nEnter The Lower Limit: ");
scanf("%d",&m);
printf("\nEnter The Upper Limit: ");
scanf("%d",&n);
printf("\nPrime Numbers Between %d & %d Are:\n",m,n);
for(i=m ; i<=n ; i++)
{
k=i;
flag=1;
for(j=2 ; (j<=k/2)&&flag ; j++)
{
if(k%j==0)
flag=0;
}
if(flag)
printf("%3d \n",i);
}
}
Why not simply count the number of primes you have printed?
int count = 0;
:
for(i=m ; (i<=n) && (count<5) ; i++)
:
if(flag)
{
printf("%3d \n",i);
count++;
}
PS, using longer names than single characters will help make your program more understandable.
Whether you like it or not, you have a higher limit which is set by the data type you use -> int, in your case. Imagine you want the first 5 prime numbers larger than (MAXINT- 10)...you get the point, but let's assume you are not concerned about corner-cases.
int count=0;
int i=m;
int k;
while (count<5)
{
k=i;
flag=1;
for(j=2 ; (j<=k/2)&&flag ; j++)
{
if(k%j==0)
flag=0;
}
if(flag)
{
printf("%3d \n",i);
count++;
}
i++;
}

Resources