Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
There are N students in a class, and Teacher wants to divide these students into some groups. Teacher says that groups consisting of two or less students is not allowed, so Teacher wants to us to have as many groups consisting of three or more students as possible.
Divide the students so that the number of groups consisting of three or more students is maximized.
I have written the code up to the following, but it is not giving the correct result for some test cases. Could anyone please tell me what's wrong
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
if(n%3 == 0){
printf("%d", n/3);
}
else if(n%4 == 0 && n/4 == n/3){
printf("%d", n/4);
}
else if((n-4)%3 == 0){
printf("%d", ((n - 4)/3)+1);
}
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
printf("%d", n/3);
}
Let me know it outs right answer
I believe this is the most simple one
💎
You're not printing anything for (e.g.) n = 20.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 days ago.
This post was edited and submitted for review 6 days ago.
Improve this question
I know there are standard solution source codes for this problem. I'll look at them once im done trying it my way. pls help me get my way right.
I've attached the source code ive perfected over two days.
the output is correct except for some 1's everytime no. of digits changes(of the natural numbers being checked for being armstrong).
Pls run the source code urself and see. i hope the code and its purpose is understandable.
gist: i know i can get the solution to this problem, i just want to know what's wrong with the code ive whipped up.
/*for finding armstromg numbers til n-digit numbers. */
#include<stdio.h>
#include<math.h>
void main()
{
int n,i,j;
int num;
int rem=1,sum,temp;//variables of armstrong checking loop.
int dig_pow;//isolated digits raise to the power
printf("Enter the number of digits you want the last armstrong number to be:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
int lim=pow(10,i+1);//number next to the last number in the digit range requested.
for(j=1;j<lim;j++)
{
sum=0;
temp=j;
num=j;
while(num>0)
{
rem=num%10;
sum=sum+pow(rem,i);
num/=10;
}
if(sum==temp)
{
printf("%d, ",sum);
}
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Now im having problems with the new code in terms of compiling. I have two great answers but chux's answer is addressed to rectify my code . So by his/her directions my new code is:
#include <math.h>
#include <conio.h>
int main()
{
int n,i,r;
printf("Enter A Number to know its prime or non prime");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{r==1;
break;
}
}
if(r==1)
printf("%d is a non-prime number",n);
else
printf("%d is a prime number",n);
return 0;
}
But on the output it show as 87 is a prime number. I don't know why. But can someone spot my mistake?
At few problems
Assignment vs. compare
if (r=1) assigns 1 to r, so if (r=1) is always true. Certainly a compare was needed, #Ry
// if (r=1)
if (r == 1)
No early break
OP's code: The value of r depends on the last iteration. Certainly once a factor is found, loop should exit.
for(i=2;i<=n-1;i++) {
if(n%i==0)
// r=1;
{ r = 1; break; }
else
r=0;
}
Incorrect functionality for n == 0,1
All values n < 2 incorrectly report as prime.
Inefficient
Code performs up to n loops. Only need to perform sqrt(n) loops. Tip: Do not use floating point math here for an integer problem.
// for(i=2;i<=n-1;i++)
for(i = 2; i <= n/i; i++)
Alternate
Only peek if you must code.
First off, " ... conio.h is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C .." I was able to get the code to compile without that library file, so you may wish to consider removing it. As for as the code goes, well here is what I came up with:
#include <math.h>
#include <stdio.h>
int isPrime(int value) {
int i = 2;
for(; i < value; i++) {
if((value % i) == 0) {
return 0;
}
}
return value > 1;
}
int main(void){
int n=0,i=0, r=0;
char * s;
printf("\nPlase enter a number to learn if it is prime:");
scanf("%d",&n);
r = isPrime(n);
printf("\n%d is ", n);
s = (r==0)? " not a prime number" : "a prime number";
puts(s);
return 0;
}
After the user inputs a number, the code checks whether it is prime by calling the function isPrime(), a function that returns an int. isPrime is a simple function that attempts to factor a number.
See here for similar live code that I devised.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I just want to know what is the wrong with it, and how to fix it.
It looks like every thing is right, but when you run the .exe it crash every time
#include <stdio.h>
#include <stdlib.h>
// first list filling function:
void T_filling(int T[],int n){
int i;
for(i=1 ;i<=n ;i++){
printf("enter the number:",i+1);
scanf("%d",&T[i]);
}
}
//then the main algorithm:
int main()
{
int j,k,l;
int n,x;
// you can order up to 100 integer number
int T[100];
printf("This program is to order numbers decreasingly\n");
printf("how many numbers you want to order?\n");
// scanning the number of elements in the list
scanf(n);
//filling the list
T_filling(T[100],n);
//bubble sort Algorithm
for(j=1;j<=n-1;j++){
for(k=1;k<=n-j;k++){
if(T[k+1]>T[k]){
x=T[k];
T[k]=T[k+1];
T[k+1]=x;
}
}
}
for(l=1;l<=n;l++){
//printing the result on screen
printf("%d;",T[l]);
}
printf("\n");
system("pause");
return 0;
}
You are not using scanf and passing the array to function properly.
Please modify these lines in your code as follows and your program will work as expected.
scanf("%d", &n);
//filling the list
while(n > 100)
{
printf("Exceeding size, please re enter the size");
scanf("%d", &n);
}
T_filling(T,n);
Hope this helps.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
the output numbers are wrong why ?
the program is used to get factorial of a number using recursion
and if you know sites to practice more examples I will be thankful for you
#include <stdio.h>
#include <stdlib.h>
int factorial(int a);
int main()
{
int n,x;
printf("enter ur number ");
scanf("%d",&x);
n=factorial(x);
printf("the factorial = %d",n);
return 0;
}
int factorial(int a)
{
int fac;
if(a<=1)
{
if (a<1)
{
fac=0;
}
return fac;
}
printf("the number = %d\n",a);
printf("the factorial = %d\n",fac);
fac = a * factorial(a-1);
This will always return 0, because the last fac will always be 0 and it multiplies all the other results. Change that to
if(a<1)
fac=1;
Also indent your code properly.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Perhaps someone could help me out with this.
Using the concept of cycle generate the Fibonacci series until reaching 10000 or a little more than that.
So I have this code and it's supossed to work and show me what I want but it doesn't.
Can somebody tell me what's wrong with it? It opens but it doesn't work #_#
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0,j=0,sum=1,num;
while(sum>=1000){
{
printf("%d\n",sum);
i=j;
j=sum;
sum=i+j;
}
system("pause");
}
The code I made for calculating the Fibonacci sequence is the following:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0,j=0,sum=1,num;
printf("Introduce the limit for the Fibonacci sequence: ");
scanf("%d",&num);
while(sum<num)
{
printf("%d\n",sum);
i=j;
j=sum;
sum=i+j;
}
system("pause");
}
In the first snippet, you have a typo
while(sum>=1000){
should be
while (sum < 10000){
I said 'less than' rather than 'less than or equal to' because of the wording of your assignment.
You want to print out Fn where Fn is the first such number > 10000. Since j is really Fn-1 change the while loop condition to
while (j <= 10000)
{
while sum >= 1000 means it will never start because sum = 1. I think you want <=. The second is an infinite loop because sum is always greater than num