A natural number n > 0 is said to be an abundant number if the sum of its proper divisors (excluding itself) is greater than itself. For example, the number 12 is an abundant number because the sum of its divisors is 1+2+3+4+6=16, which is greater than 12 itself. in contrast, 16 is not an abundant number because the sum of its divisors is 1+2+4+8=15, which is not greater than 16. I have to write a program which will, for any entered natural number k, print all abundant numbers smaller or same as k. I have written a following program:
#include <stdio.h>
int main (void) {
int k, sum=0, i, n;
printf ("Enter k: ");
scanf ("%d", &k);
for (i=1; i<=k; i++) {
int n=1;
if (n%i==0);
sum+=i;
}
if (n<sum);
printf ("%d", &n);
return 0;
}
Output of this program is not as expected. For example, if I enter 16 (or any other natural number), output is 6356716, which is definitely not correct. I just started learning programming and we just started with functions, so I wrote this:
#include <stdio.h>
int abundant (int n)
{
int i, sum=0;
for (i=0; i<n; i++)
if (n%i==0) sum+=i;
if (n<sum) return 1;
else return 0;
}
int main (void)
{
int i, k;
printf ("Enter a number: ");
scanf ("%d", &k);
for (i=1; i<=k; i++) {
if (abundant(i))
printf ("%d",&i);
}
return 0;
}
but output is exactly the same. Can somebody please tell me what I have done wrong?
This code should work
#include <stdio.h>
int main(void)
{
int k, i, j;
printf("Enter k: ");
scanf("%d", &k);
for (i = 1; i <= k; i++)
{
int sum = 0;
for (j = 1; j < i; j++)
{
if (i % j == 0)
sum += j;
}
if (sum > i)
printf("%d is abundant\n", i);
}
return 0;
}
Related
I want to write a program where a user tells me an integer(n) and i calculate The sum of 1+(1-2)+(1-2+3)+(1-2+3-n)... where even integers are -k and odd integers are +k.
Ive made a function which does that But the sum is never correct. For example for n=2 it should be sum=0 but shows sum=-1 for n=3 should be sum=+2 but i shows sum=3. (Ignore the debugging printfs)
#include <stdio.h>
int athroismaAkolouthias(int n); // i sinartisi me tin opoia ypologizete to athroisma akolouthias 1+(1-2)+(1-2+3)+(1-2+3-4).....
int main(){
int n;
printf("give n: ");
scanf("%d", &n);
printf("the sum is %d", athroismaAkolouthias(n));
}
int athroismaAkolouthias(int n){
int sum1=0, sum2=0,sum=0;
int i, temp, j;
for (i=1; i<=n; i++){
for (j=1; j<=i; j++){
temp=j;
}
if (i%2==0){sum=sum-temp; printf("test1 %d%d",sum,temp);}
else{sum=temp; printf("test2 %d%d",sum,temp);}
}
return sum;
}
Your issue is with our loop which iterate with j, it should update the inner_sum based on j even/odd condition as follows:
#include <stdio.h>
int akl(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
int inner_sum = 0;
for (int j = 1; j <= i; j++) {
if (j % 2 == 0) {
inner_sum -= j;
} else {
inner_sum += j;
}
}
sum += inner_sum;
}
return sum;
}
int main() {
int n;
scanf("%d", &n);
printf("%d\n", akl(n));
}
You only need two variables for sum that I named them inner_sum and sum which shows the sum of each term and sum over all terms.
Suspicious Line: else {sum = temp; ...
Shouldn't you be adding or subtracting to sum every time??
Why are you assigning to it here, without an addition or subtraction?
You also have variables sum, sum1, and sum2.
You print sum1 and sum2, but never modify them.
Here's my solution:
// The sum of 1+(1-2)+(1-2+3)+(1-2+3-n)... where even integers are -k and odd integers are +k.
#include <stdio.h>
int ancho(int n)
{
int sum=0;
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=i; ++j)
{
sum += (2*(j%2)-1)*j;
}
}
return sum;
}
int main(void)
{
int n = 5;
printf("Solution is %d\n", ancho(n));
}
// Solution is 3 for n = 5,
// because: 1 + (1-2) + (1-2+3) + (1-2+3-4) + (1-2+3-4+5) =
// 1-1+2-2+3 = 3
Output
Success #stdin #stdout 0s 5476KB
Solution is 3
IDEOne Link
/*This is a c program I made to print prime numbers between 0 and n,
the loop in this program runs once and terminates.*/
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n\n");
scanf("%d", &n);
int i, j;
for (i = 0; i <= n; i++)
{
int c = 0;
for (j = 0; j <= i / 2; j++)
{
if (i % j == 0)
{
c++;
}
}
if (c == 1)
{
printf("%d\n", i);
}
}
}
This is the output of the program:
Enter the value of n
10
...Program finished with exit code 0
Press ENTER to exit console.
/This is a c program I made to print prime numbers between 0 and n,
the loop in this program runs once and terminates./
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n\n");
scanf("%d",&n);
int i,j;
for(i=0;i<=n;i++)
{
int c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==1)
{
printf("%d\n",i);
}
}
}
The use of second for loop is to find that no. is prime or not and to prove that you have to check two possibilities, that is should be divisible by one and by istself. so, you have to initialize if(c==2) ,j=1 and j<=i.
I am trying to rearrange my first array (vector1) in accordance to the sequence of the indices second array (vector2)
For example, if:
vector1 = 1 2 3 4
and vector2 = 0 3 1 2
my desired output should be:
1 4 2 3
However, the execution of my code is continuously interrupted due to using an "illegal array index." Would someone be able to point out where I am going wrong? Thanks so much in advance.
#include <stdio.h>
#include <math.h>
int main(void) {
int n;
printf("Enter vector length: ");
scanf("%d", &n);
printf("Enter vector: ");
int vector1[n];
int i = 0;
while (i < n) {
scanf("%d", &vector1[i]);
i++;
}
printf("Enter permutation: ");
int vector2[n];
i = 0;
int j = 0;
while (i < n) {
scanf("%d", &vector2[j]);
i++;
}
i = 0;
int vector3[n];
while (i < n) {
vector3[i] = vector1[vector2[i]];
i++;
}
printf("%d", vector3[i]);
}
While using vector2 you are getting the value of vector2[j] but increasing the value of i. You have assigned j=0 above so the loop is scanning the same value again and again i.e vector2[0] until the loop goes off. So to correct your program either you increase j in your loop by replacing j++ instead of i++ or you can just replace vector2[j] with vector2[i] in the loop.
#include <stdio.h>
#include <math.h>
int main(void)
{
int n;
printf("Enter vector length: ");
scanf("%d", &n);
printf("Enter vector: ");
int vector1[n];
int i = 0;
while (i < n)
{
scanf("%d", &vector1[i]);
i++;
}
printf("Enter permutation: ");
int vector2[n];
i = 0;
while (i < n)
{
scanf("%d", &vector2[i]);
i++;
}
i = 0;
int vector3[n];
while (i < n)
{
vector3[i] = vector1[vector2[i]];
i++;
}
for(i=0;i<n;i++)
printf("%d", vector3[i]);
}
This is a program to print the smallest value and its position in an array (defined by user).
#include <stdio.h>
int position_smallest(int a[],int n)
{
int smallest = a[0];
int i,k;
for(i=0; i<=n-1; i=i+1)
{
if(a[i]<a[0])
{
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main()
{
int n,j;
int a[n];
printf("Enter the size of the array: ");
scanf("%d", &n);
for(j=0; j<=n; j=j+1)
{
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
}
But upon running it, it shows following error:
Segmentation fault (core dumped)
What can be the possible reason(s) for it?
First error, as stated in one of the comments, is declaring an array of size n before even knowing how much n is.
Second mistake is for loop in your main function that goes from 0 to n, i.e. index of an array is out of bounds.
Try this:
int main() {
int n = 0, j = 0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[n];
for (j = 0; j < n; j++) {
printf("a[%d] = ", j + 1);
scanf("%d", &a[j]);
}
position_smallest(a,n);
}
If this solved your problem, please mark it.
First of all:
int n, j;
both uninitialized. Initialize them otherwise you will get garbage values.
int n = 0, j = 0;
What happens if n by chance (very likely) is 0 in following line?
int a[n];
You allocate 0 bytes for array a[]. You then enter 10 in following line
scanf("%d", &n);
You will get segmentation fault in for() loop below because your loop is trying to put 10 elements where you allocated no memory at all.
What happens if uninitialized n by chance (very likely) is 2^32 * 4 bytes (ints max)?
int a[n];
You allocate 2^32 bytes for array a[]. You then enter 10 in following line
scanf("%d", &n);
You will not get segmentation fault but you will allocate 2^32 * 4 bytes of memory for your program and you will use only 10
Second if that is not enough:
for (j = 0; j <= n; ++j)
scanf("%d", arr[n];
will access 11th element of array which is undefined behavior, and you might even get segmentation fault there. As you know arrays in C arrays are indexed from 0 to n - 1 (if n is size of array).
Third your loop inside function:
for(i=0; i<=n-1; i=i+1)
is the same as:
for(i=0; i < n; ++i)
And finally, you have a bug in your code, I believe:
if(a[i]<a[0])
should be:
if (a[i] < smallest)
because it is most likely that you would like compare other number to already smallest element not to a[0]. Here is my code
#include <stdio.h>
int position_smallest(int a[],int n) {
int smallest = a[0];
int i = 0, k=0;
for(i=0; i<n; ++i) {
if(a[i]<smallest) {
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main() {
int n=0, j=0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[n];
for(j=0; j<n; ++j) {
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
return 0;
}
The version above is legit for C99 and up standards. If you are using C89 and earlier compilers you are stack with fixed size as mentioned in #Saurabh's answer or preferably use malloc().
Here is malloc() version:
#include <stdio.h>
#include <stdlib.h>
int position_smallest(int a[],int n) {
int smallest = a[0];
int i=0,k=0;
for(i=0; i<=n-1; i=i+1) {
if(a[i]<smallest) {
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main() {
int n=0, j=0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int *a = malloc(sizeof(int) * n);
for(j=0; j<n; j=j+1) {
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
return 0;
}
Use this one:
#include <stdio.h>
int position_smallest(int a[],int n)
{
int smallest = a[0];
int i,k;
for(i=0; i<=n-1; i=i+1)
{
if(a[i]<a[0])
{
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main()
{
int n,j;
int a[100];
printf("Enter the size of the array: ");
scanf("%d", &n);
for(j=0; j<n; j=j+1)
{
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
}
Specify the size of the array anything but run the loop according to user's input.
And there is some problems in your function position_smallest. So if you want correct it then take a look at #Gox's answer.
I made a program for making a pascal triangle and for the input of numbers ( rows ) > 5 , there is an alignment problem i.e for ncr > 10. Help me out please.
I have included the images for output of the program.
Output Image
#include<stdio.h>
int factorial(int number)
{
int fact=1;
for(int i=1; i<=number; ++i )
{
fact*=i;
}
return fact;
}
int ncr(int n, int r)
{
int ncr;
int fact1=factorial(n);
int fact2=factorial(n-r);
int fact3=factorial(r);
ncr = fact1 /(fact2 * fact3);
return ncr;
}
int main()
{
int rows;
printf("enter the number of rows :\n");
scanf("%d",&rows);
for(int n=0; n<rows; n++)
{
for(int i=1; i<=rows-n; i++)
{
printf(" ");
}
for(int r=0; r<=n; r++)
{
printf("%d ",ncr(n,r));
}
printf("\n");
}
return 0;
}
You can change the inner loop like this
for(int i=1; i<=rows-n; i++)
{
printf(" "); // Note the extra space
}
for(int r=0; r<=n; r++)
{
printf("%3d ",ncr(n,r)); // Changed to %3d
}
This will work upto 9 rows. If you want it to work for more rows, you can add another space in the first printf and change the second printf to %5d
printf can take a precision before the formatter. Change printf("%d ",ncr(n,r)); to printf("%3d ",ncr(n,r)); to make the numbers 3 characters wide. Also change printf(" "); to printf(" ");.
If you use
printf ("Width trick: %*d \n", 5, 10);
this will add 5 more spaces before the digit value.