|
 |
|
Dev Articles Community Forums
> Programming
> C/C++ Help
|
Need help with my code
Discuss Need help with my code in the C/C++ Help forum on Dev Articles. Need help with my code C/C++ Help forum discussing building and maintaining applications in C/C++. Find out why these languages are the foundation on which other languages are built.
|
|
 |
|
|
|

Dev Articles Community Forums Sponsor:
|
|

November 12th, 2012, 10:25 AM
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 5 h 39 m 29 sec
Reputation Power: 0
|
|
Need help with my code
hi, i'm trying to build a code that gets 2 integers,
and the output is how many primal numbers (2,3,5,7...)
gives num%primal ==0. they both have in common
with repeats.
meaning:
20 - 2,2,5
40 - 2,2,2,5
output will be: 3
here's what i've written so far, don't understand where's the probleme...
Code:
#include <stdio.h>
int main(){
int min, max, i, j, k, l, m, count1, count2, count3, temp, divider;
scanf("%d",&min);
while (min!=0){
count1=0;
count2=0;
count3=0;
scanf("%d",&max);
for (divider=2; divider*divider<=min; divider++){
if (min % divider == 0)
break;
if (divider*divider >= min){ ///divider is a primal number///
temp = min;
while (temp >= divider){
if (temp % divider == 0){
count2=count2+1;
temp= temp/divider;
}
}
for (l=2; l<=count2; l++){
temp=max;
while (temp>=divider){
if (temp % divider == 0){
count3 = count3 + 1;
temp = temp/divider;
}
}
}
}
if (count3>count2)
count1=count1+count2;
else
count1=count1+count3;
}
printf("%d",count1);
scanf("%d",&min);
}
}
i have some extra variables for other parts that will follow...
can only use if, and loops.... no math, arrays or anything else...
thanks!
|

November 12th, 2012, 03:40 PM
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 5 h 39 m 29 sec
Reputation Power: 0
|
|
i was able to make my code shorter and simpler,
but still doesn't work...
help?
Code:
#include <stdio.h>
int main(){
int menu_select, menu_err, num1, num2, min, max, i, j, k, l, m, count1, count2, count3, temp_min, temp_max, divider, flag;
menu_err=0;
///////////////////////////////////////////////////////////////////////////////
count1=0;
count2=0;
count3=0;
do{
printf("please enter the 1st positive number ( up to 5 digits ) :\n"); //check first num if ok//
scanf("%d",&num1);
}while (num1<0 || num1>99999);
do{
printf("please enter the 2nd positive number ( up to 5 digits ) :\n"); //check second num if ok//
scanf("%d",&num2);
}while (num2<0 || num2>99999);
if (num1>num2){ //checking for the bigger number//
max=num1; //and put them into max and min //
min=num2; //variables //
}
else{
max=num2;
min=num1;
}
//////////////////////////////////////////////////////////////////////////////
printf(" ");
for (k=0; k<10; k=k+1) //creats first row//
printf("%7d",min+k);
///////////////////////////////
for (i=0; i<19; i=i+1){ //rows//
printf("\n");
for (j=0; j<=10; j=j+1){ //columns//
count1=0;
if (j==0) //creates first number in each row//
printf("%7d",max+i);
else{
divider=2;
count1=0;
temp_min=min+j-1;
temp_max=max+i;
while(temp_min>1 && temp_max>1){
flag=1;
//check if divider is primal//
for (k=2; k*k<divider && flag; k++)
if (divider%k == 0)
flag=0;
//////////
if (flag){
if (temp_min % divider == 0 && temp_max % divider == 0){
count1++;
temp_min=temp_min/divider;
temp_max=temp_max/divider;
}
if (temp_min%divider==0 && temp_max!=0)
temp_min=temp_min/divider;
else
if (temp_max%divider==0 && temp_min!=0)
temp_max=temp_max/divider;
else
divider++;
}
else
divider++;
}
printf("%7d",count1);
}
}
}
printf("\n");
}
|
Developer Shed Advertisers and Affiliates
Thread Tools |
Search this Thread |
|
|
Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|