C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 12th, 2012, 11:25 AM
mritay mritay is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 mritay User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old November 12th, 2012, 04:40 PM
mritay mritay is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 mritay User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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");
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Need help with my code


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap