The task is:
Write a program that prints the first n perfect numbers. Checking that the number is perfect should be done in the perfect function.
I did it like this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 100
/*
*/
int perfect_number(long int n)
{
long int sum=0;
for(int j=1;j<n;j++)
{
long int candidate=j;
if(n%candidate==0)
{
sum=sum+candidate;
}
}
if(sum==n)
{
return n;
}
return 0;
}
int main()
{
int n;
printf("Enter how much perfect numbers you want :");
scanf("%d",&n);
while(n<1)
{
printf("Enter how much perfect numbers you want:");
scanf("%d",&n);
}
int counter=0;
for(long int i=1;counter<n;i++)
{
if(perfect_number(i))
{
printf("%ld ",i);
counter++;
}
}
return 0;
}
The problem arises when I type that I want, the first 5 perfect numbers or more. The program will print only 4 and will continue to work, it will search for numbers but will not print anything.
If I type in the first four perfect numbers, they will print 4 and finish the program, they will do everything right.
I thought the problem was in representing the fifth number, so I replaced int with long int, but that didn't help.
Related
what's wrong with this code?
It's supposed to get a digit from me, then show all the numbers between 100 & 1000000 containing that digit...
#include <stdio.h>
int main () {
int n,m;
puts("Enter your digit:\n");
scanf("%d\n", n);
int j=100;
while (j<=1000000) {
m=10;
if (j%m==n) {printf("%d\n",j);}
while (j/m>=1) {
if ((j/m)%10==n) {printf("%d\n",j);}
m=m*10;}
j+=1;}
return 0;
}
Try using this code and see how it works
#include <stdio.h>
#include <math.h>
int main () {
int n,m;
puts("Enter your digit:\n");
scanf("%d", &n);
for(int i = 1;(pow(10,i)*n)<1000000;i++)
{
for(int j = 0;j<(pow(10, i));j++)
printf("%lf\t\t", ((pow(10, i)*n))+j);
printf("\n");
}
return 0;
}
When using scanf use the '&' sign after the comma. The passes a pointer to the variable so scanf can store the value.
It is wise to avoid using '\n' inside scanf; so it should look like this:
scanf("%d", &n);
I am trying to write a program to get the maximum value but it is not working. The calculation is performed inside a function named max_number.
What is the error?
#include <stdio.h>
int max_number(int storeX[], int i)
{
int max=0,x;
for(x=0;x<i;x++)
{
if(storeX[x]<max)
{
max = storeX[x];
}
return max;
}
return 0;
}
int main()
{
int i,x,numbers,max;
printf("how many numbers do you want to compare?\n");
scanf("%d",&i);
int storeX[i];
for(x=0;x<i;x++)
{
printf("the %d number is:",x+1);
scanf("%d",&numbers);
numbers=storeX[x];
}
max=max_number(storeX,i);
printf("the max number is: %d",max);
return 0;
}
There were some problems in the code, have made changes to the code to make it work. Check the comments in the code to understand.
#include <stdio.h>
#include <limits.h>
int max_number(int storeX[], int i)
{
int max=INT_MIN,x; //In case you array has all negative numbers
for(x=0;x<i;x++)
{
if(storeX[x]>max) // This if condition is wrong
{
max = storeX[x];
}
//return max; // You need to iterate the whole array
}
return max;
}
int main()
{
int i,x,numbers,max;
printf("how many numbers do you want to compare?\n");
scanf("%d",&i);
int storeX[i];
for(x=0;x<i;x++)
{
printf("the %d number is:",x+1);
scanf("%d",&storeX[x]); // Need to pass a reference to the array index
//numbers=storeX[x]; //dosen't assign the value to storeX[x] instead the otherway around
}
max=max_number(storeX,i);
printf("the max number is: %d\n",max);
return 0;
}
So I'm really new to programming. I need to write a program that, if I give it any array of integers, it'll be able to find the two numbers closest to each other, and then give the difference between those two numbers. Also, the first number must be the number of integers that are going to be in the array.
So for example, I give it 3 1 4 8. The first 3 means that there will be three integers, so it must find the closest two numbers between those three. In this case, it's 4 - 1 = 3, so the output should be 3, but when I write it it gives me 16.
This is what I have, and I don't know what's wrong:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, str[n*n], minimum, c;
/* here I'll make a new array, and its elements will be all the
differences between all the elements of the previous one */
for(a=0;a<n;a++)
for(b=0+a*n;b<n;b++) {
if(st[b-a*n]==st[a])
str[b]=32000;
else
str[b]=abs(st[b-a*n]-st[a]);
}
// here I'll find the smallest element on the last made array
minimum = str[0];
for(c=0;c<n*n;c++)
{
if(str[c]<minimum);
{
minimum=str[c];
}
}
printf("%d", minimum);
return 0;
}
Edit: I tried to fix it with your answers but it still doesn't work.
New code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, minimum;
minimum = st[0];
for(a=0;a<n;a++)
for(b=0;b<n;b++) {
if((st[b] != st[a]) && (abs(st[b]-st[a]))<minimum)
minimum = abs(st[b]-st[a]);
}
printf("%d", minimum);
return 0;
}
Edit 2: Ok, I fixed it now. Thanks a lot ^^
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
int n, i;
printf("write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, minimum;
minimum = INT_MAX;
for(a=0;a<n;a++)
for(b=a+1;b<n;b++) {
if((abs(st[b]-st[a]))<minimum)
minimum = abs(st[b]-st[a]);
}
printf("%d", minimum);
return 0;
}
The program is just a base 10 to base 2 converter it works fine. I just want the cursor at the new line.
I have put "\n" everywhere I can logically put it but it just does not bring the cursor to the new line. The program is doing what I need it to do.
EX
"Enter a decimal number from 0 to 18,446, 744,073,709,551,615: 156"
The binary value for 156 is:10011100{{CURSOR}}
(I want the {{CURSOR}} here)
#include <stdio.h>
int main(void)
{
long int dec, num;
int store[100], i=1,j;
printf("Enter a decimal number from 0 to 18,446,744,073,709,551,615: ");
scanf("%ld",&dec);
num = dec;
while(num!=0)
{
store[i++]=num%2;
num=num/2;
}
printf("The binary value for %d is: ",dec);
for(j=i-1;j>0;j--)
{
printf("%d",store[j]);
}
return 0;
}
When all is said and done, you need to print a newline after the number is done using printf("\n"). The rest, like you said, does what it should:
#include <stdio.h>
int main(void)
{
long int dec, num;
int store[100], i=1,j;
printf("Enter a decimal number from 0 to 18,446,744,073,709,551,615: ");
scanf("%ld",&dec);
num = dec;
while(num!=0)
{
store[i++]=num%2;
num=num/2;
}
printf("The binary value for %d is: ",dec);
for(j=i-1;j>0;j--)
{
printf("%d",store[j]);
}
printf("\n");
return 0;
}
I know this is going to be something of a silly slip or oversight on my behalf, but I can't get the array in this to print out correctly. When I run the code and put in my inputs, I get seemingly random numbers.
For example,
number of rooms was 1
wattage of lights was 2
hours used was 2
TV/computers was 2
The output I got was 3930804. What did I miss?
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int room[20] = {0.0};
int i;
int rooms = 0;
char option = 0;
int lights[20];
int hrsUsed[20];
int Telly_Computer[20];
printf("Enter number of rooms");
scanf_s("%d", &rooms);
for(i=0;i<rooms;i++)
{
printf("input wattage of lights");
scanf_s("%d", (lights+i));
printf("input number of hours use/day (average)");
scanf_s("%d", (hrsUsed+i));
printf("input number of TV/Computers");
scanf_s("%d", (Telly_Computer+i));
}
printf("%d \n", lights);
}
printf("%d \n", lights);
You're printing the array directly. You need to loop over it and print the elements one at a time.
int i;
for (i = 0; i < 20; ++i)
printf("%d\n", lights[i]);
You are just printing the address of lights (and using UndefinedBehavior by the way, address must be printed with %p). You must use a loop to print out all of the contents of each array slot.
for(int i=0;i<(sizeof(lights)/sizeof(int));i++)
printf("%d\n",lights[i]);