Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<stdio.h>
int main()
{int c=0,n,k,i=1;
printf("Enter 'n'&'k'respectively:\n");
scanf("%d %d",&n,&k);
int t[n];
printf("Enter %d numbers:\n",n);
for(i=0;i<n;i++)
{scanf("%d",t[i]);}
printf("count of numbers divisible by %d is:\n",k);
for(i=0;i<n;i++)
{ if(t[i]%k==0) /*both are variables, what to do*/
c=c++;
}
printf("%d",c);
}

What I have tried:

i tried the above code for finding numbers divisible by k, but in the if condition both t[i] and k are constants, i dont know what to do!
Posted
Updated 7-Oct-17 7:20am
Comments
Patrice T 7-Oct-17 12:26pm    
Describe your problem, say it go wrong.
add sample input, output and actual result.

They aren't constants.
t is an array of integers, containing n elements.
k is an integer variable which is given a value by the user.

The problem is much deeper than that...
Replace this line:
C++
c=c++;
With this:
C++
c++;
And it might work better - then look here and see why: Why does x = ++x + x++ give me the wrong answer?[^]

BTW: Indent your code!
It's a whole load easier to work out what is going on...
C++
#include<stdio.h>
int main()
    {
    int c=0,n,k,i=1;
    printf("Enter 'n'&'k'respectively:\n");
    scanf("%d %d",&n,&k);
    int t[n];
    printf("Enter %d numbers:\n",n);
    for(i=0;i<n;i++)
        {
        scanf("%d",t[i]);
        }
    printf("count of numbers divisible by %d is:\n",k);
    for(i=0;i<n;i++) 
        { 
        if(t[i]%k==0)
            {
            c++;
            }
        }     
    printf("%d",c);
    }
 
Share this answer
 
v2
Comments
Ravi Bhavnani 7-Oct-17 12:55pm    
Minor typo: missing ")" in "if" condition.

/ravi
OriginalGriff 7-Oct-17 13:40pm    
:sigh:
Copy, paste, and lose something...

Fixed! :thumbsup:
First of all, learn to indent properly your code, it show its structure and it helps reading and understanding. Do not try to pack things on same line, it don't make the code faster, just more difficult to read.
C++
#include<stdio.h>
int main()
{
	int c=0,n,k,i=1;
	printf("Enter 'n'&'k'respectively:\n");
	scanf("%d %d",&n,&k);
	int t[n];
	printf("Enter %d numbers:\n",n);
	for(i=0;i<n;i++)
	{
		scanf("%d",t[i]);
	}
	printf("count of numbers divisible by %d is:\n",k);
	for(i=0;i<n;i++)
	{
		if(t[i]%k==0)  /*both are variables, what to do*/
			c=c++;
	}
	printf("%d",c);
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

The correct syntax for
C++
c=c++;

is
C++
c++;

Advice: you don't need an array for this problem, each value you store in the array is reuse inly once.
-----
Quote:
but in the if condition both t[i] and k are constants, i dont know what to do!

The debugger will show you what is qrored in array t.
The debugger will help you to understand what is going on by showing you what it does.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

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