Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The program doesn't print the common but instead the initialized value which is intended to increment as coded in the program.

And i think the problem lies in the while loop.

it would be appreciated if the following code is corrected instead of writing totally different code.

What I have tried:

C++
#include <conio.h>
#include <stdio.h>

int main(){
	
	int a, b, i, j;
	int total_a=0, total_b=0;
	
	printf("Enter your integers: \n");
	scanf(" %d%d", &a, &b);
	
	for(i=1, j=1; i<=a,j<=b; i++,j++){
		
		if(a%i==0)
			total_a = total_a*10 + i;
			
		if(b%j==0)
			total_b = total_b*10 + j;
		
	}
	
	/*printf(" %d %d", total_a, total_b);
	int total=0;
	printf(" %d", total);*/

	int rem_a, rem_b, total =0;

	while(total_a!=0){
		
		rem_a = total_a%10;
		
		while(total_b!=0){
			
			rem_b =total_b%10;
			
			if(rem_a==rem_b){
				total = total*10 + rem_a;
			}
			
			total_b/=10;
		}
		
		total_a/=10;
	}
	
	printf(" %d", total);
	
	return 0;
}
Posted
Updated 7-Oct-17 21:30pm
v4

1 solution

Um. You need to think some more about this: your code doesn't do what you think it will.
for(i=1, j=1; i<=a,j<=b; i++,j++){

Inside this loop, i and j will always have the same value; they will both start at one, then they will both be two, and so on. I'm not sure when they will stop, because I've not seen a condition with a comma in it before and I doubt it will compile - I'm on a tablet and have no C compiler at the moment s I can't check, but I don't think it's valid C code.

Probably, you need a nested pair of loops, rather than a single loop...
 
Share this answer
 
Comments
Richard MacCutchan 8-Oct-17 3:14am    
Just tried that and it works fine. The loop continues until the second expression (j<=b) is true. Which may, or may not, be what the programmer wants.
Tarun Jha 8-Oct-17 3:29am    
yes that's the problem, the while loop

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900