This is a program to print armstrong numbers in a given range entered by the user, but when this program is executed and inputs are entered the outputs are not displayed.
#include <stdio.h>
int main()
{
int low,high,sum,rem,orig_i;
printf("enter lower and higher numbers : ");
scanf("%d %d",&low,&high);
sum=0;
while(low<high)
{
for(int i=low+1;i<high;i++)
{
while(i!=0)
{
rem=i%10;
orig_i=low+1;
sum=sum+(rem*rem*rem);
if(sum==orig_i)
{
printf("%d",orig_i);
}
i=i/10;
}
}
}
return 0;
}
As you are not increasing low, your loop is running infinitely and a few more errors too.
You can do like this. Although there are a lot of efficient ways to write this programs but i have done a little changes only to make it work.
#include <stdio.h>
int main()
{
int low,high,sum,rem,orig_i;
printf("enter lower and higher numbers : ");
scanf("%d%d",&low,&high);
while(low<high)
{
sum=0;
orig_i = low+1;
for(int i=orig_i;i!=0;i=i/10)
{
rem=i%10;
sum=sum+(rem*rem*rem);
}
if(sum==orig_i)
{
printf("%d\n",orig_i);
}
low++;
}
return 0;
}
Related
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.
so I was writing this program to find the number of times a digit is repeated in a number.How can I ask the user to enter inputs again and again even after the execution at the first time
#include <stdio.h>
int main()
{
int rem,flag,n;
int frequency[10]={0};
printf("enter a number");
scanf("%d",&n);
while(n>0){
rem=n%10;
frequency[rem]++;
n=n/10;
}
for(int i=0;i<10;i++)
{
if(frequency[i]>1){
flag=1;
printf("%d is repeated %d times",i,frequency[i]);
}
}
if(flag==0){
printf("no number is repeated");
}
return 0;
}
One way to do this would be to wrap your code in a while (1) loop (loop forever). I've added an extra check: if a user enters the number 0, the program will break out of the loop and exit. There has to be some way to stop.
#include <stdio.h>
int main()
{
while (1){
int rem,flag=0,n;
int frequency[10]={0};
printf("enter a number");
scanf("%d",&n);
if (n==0){
break;
}
while(n>0){
rem=n%10;
frequency[rem]++;
n=n/10;
}
for(int i=0;i<10;i++)
{
if(frequency[i]>1){
flag=1;
printf("%d is repeated %d times",i,frequency[i]);
}
}
if(flag==0){
printf("no number is repeated");
}
}
return 0;
}
For a cleaner solution, you may want to create a function for everything within the while loop, and call that function.
Side note: flag has to be set to 0 at the start of the loop.
This is GA for a timetable problem. I'm trying to create an initial population, but it isn't working as it isn't entering the if condition. can someone point out the error?
I tried inserting statements in each condition, but everything checks out. Still, I don't seem to find a solution.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
int random_number_creator(int upper, int lower)
{
int n;
n = rand() % (upper-lower)+ lower;
return n;
}
struct pop{
int subjects[6];
int days[5][9];
}oldpop, bench;
main()
{
int i,j,s=1,d,h,stop=1,cou=0;
for(i=0;i<5;i++)
{
for(j=0;j<9;j++)
if(j!=6)
bench.days[i][j]=0;
else
bench.days[i][j]=11111;
}
for(i=0;i<6;i++)
{
if(i<4)
oldpop.subjects[i]=3;
else
oldpop.subjects[i]=2;
}
for(i=0;i<5;i++)
{
printf("\n");
for(j=0;j<9;j++)
printf(" %d",bench.days[i][j]);
}
for(i=0;i<6;i++)
{
printf(" \n %d",oldpop.subjects[i]);
}
cou=0;
for(i=0;i<6;i++)
{
cou=cou+ oldpop.subjects[i];
}
printf("\n%d\n",cou);
do
{
s=random_number_creator(5,0);
printf("\nsubject number:%d\n",s);
printf("\nloop 1 entery %d",cou);
do
{
printf("\nloop 2 entry\n");
d=random_number_creator(5,0);h=random_number_creator(8,0);
printf("\nDay Number:%d \nHour Number:%d\n",d,h);
if(bench.days[d][h]==0&&oldpop.subjects[s]!=0)
{
printf("\nif condition reached\n");
oldpop.days[d][h]=10+s;
bench.days[d][h]=11111;
stop=0;
cou--;
oldpop.subjects[s]--;
}
else
{
printf("\nIf condition not satisified.\n");
break;
}
}while(stop!=0);
}while(cou!=0);
for(i=0;i<5;i++)
{
printf("final entery \n");
for(j=0;j<9;j++)
printf(" %d",oldpop.days[i][j]);
}
}
I want the oldpop variable to be initialized for this timetable problem but the code does not enter the if condition in the do while loop.
The problem in your program comes from the subject selection (after adding the missing }):
s=random_number_creator(5,0);
Will return a random number between 0 and 4 included.
To correct this, just replace this line by
s=random_number_creator(7,0);
To pick a number between 0 and 6. So the cou variable will be able to reach 0
Your code can be improved:
Instead of this kind of block:
for(i=0;i<5;i++)
{
printf("final entery \n");
for(j=0;j<9;j++)
printf(" %d",oldpop.days[i][j]);
}
Create a function, learn to use printf:
void print_table(struct pop pop, const char *label)
{
int i, j;
printf("\n%s\n", label);
for (i=0;i<5;i++)
{
for (j=0;j<9;j++)
{
printf(" %5d",pop.days[i][j]);
}
}
}
And use it this way
print_table(oldpop, "oldpop");
The main problem of the code is that finally the programm shows every time that there are not successive integers.
At first, I tried to figure out a solution to this problem, by investigating how to correct the "if" statement and then to fix some small mistakes on the code, but I was unable to find any error. The code is below
#include <stdio.h>
int main() {
int a,i;
int A[10];
for(i=0; i<=9; i++) {
scanf("%d",&a);
A[i]=a;
}
if ((A[i+1]-A[i]==1)||(A[i+1]-A[i]==-1)) {
printf("{%d,%d}",A[i+1],A[i]);
} else {
printf("Den yparxoun diadoxikoi arithmoi");
}
return 0;
}
Well, the expected result is to show, if exist, the successive integers as pairs. For example if I write the integers 4,-1,9,8,3,5,-21,6,7,8 the program should print {9,8}{6,7}{7,8}. The actual result is to show every time that there are not successive integers.
Thank you in advance for your help.
This should do it:
#include <stdio.h>
int main() {
int a,i;
int A[10];
int c =0;
for(i=0; i<=9; i++) {
scanf("%d",&a);
A[i]=a;
}
for(i=0;i<=9;i++)
{
if(A[i+1]==10)
{
break;
}
else if ((A[i+1]-A[i]==1)||(A[i+1]-A[i]==-1))
{
printf("{%d,%d}",A[i],A[i+1]);
c=1;
}
}
if(!c)
printf("Den yparxoun diadoxikoi arithmoi");
return 0;
}
You are supposed to use a loop in order to find out the pair by adding the loop your code looks like this
#include <stdio.h>
int main()
{
int a,i,flag=0;
int A[10];
for(i=0; i<=9; i++)
{
scanf("%d",&a);
A[i]=a;
}
for(i=0;i<9;i++){
if ((A[i+1]-A[i]==1)||(A[i+1]-A[i]==-1))
{
printf("{%d,%d}",A[i+1],A[i]);
flag=1;
}
}
if(!flag)
printf("Den yparxoun diadoxikoi arithmoi");
return 0;
}
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;
}