Beginner in C - Programming arithmetic algorithm? - c

Here is what I'm supposed to do:
Write a program that reads a positive integer and displays the maximum positive integer n for which the sum 1^2 + 2^2 + 3^2 + ... + n^2 is less than the given number.
So far I am only able to just add the sum of all natural numbers until n:
#include <stdio.h>
int main ()
{
unsigned int n;
int sum = 0;
int i;
sum = 0;
printf("Print your number");
scanf("%d", &n);
for (i = 1; i <= n; ++i)
{
sum += i;
}
printf("sum = %d", sum);
return 0;
}
Appreciate the help!

you can try this
#include <stdio.h>
int max_positive_integer(int given_number)
{
int sum = 0;
int n = 1;
while (sum < given_number) {
sum += n * n;
n++;
}
printf("sum= %d\n", sum);
return n;
}
int main ()
{
printf("Print your number:");
int n;
scanf("%d", &n);
int max_integer = max_positive_integer(n);
printf("max_integer = %d\n", max_integer);
return 0;
}

How to sum a series of squares in a loop is the easy part
sum = 0;
for (n=0; TBD; n++) {
sum += n*n;
}
printf(..., n);
The trick is when to stop given "sum ... is less than the given number."
Code could use
for (n=0; sum + n*n < given_number; n++) {
sum += n*n;
}
n--;
That works up to a point, yet seems redundant. It has a problem in that sum + i*i may overflow. Perhaps subtract i*i each time as we do not need to report the sum, just n.
for (n=0; n*n < given_number; n++) {
given_number -= n*n;
}
n--;
What it nice about this is that the compare on the right side gets smaller as n*n increases, yet n*n does not overflow. Note: n will be about the cubic root of given_number
If you want to avoid the loop, research Sum of First n Squares
As #user4581301 commented, use "%u" with unsigned.

Related

Write a program in C to display the n terms of even and odd natural number and their sum using functions

#include <stdio.h>
int sum_even(int n);
int sum_odd(int m);
int main() {
int n;
scanf("%d", &n);
int m;
scanf("%d", &m);
int evensum;
evensum = sum_even(int n);
int oddsum;
oddsum = sum_odd(int m);
printf("the sum of even numbers is %d", evensum);
printf("the sum of odd numbers is %d", oddsum);
return 0;
}
int sum_even(int n) {
int sum = 0, i;
for (i = 2; i <= n; i += 2) {
sum += i;
}
return sum;
}
int sum_odd(int m) {
int SUM = 0, j;
for (j = 1; j <= m; j = j + 2) {
SUM = SUM + j;
}
return SUM;
}
please tell me what is wrong with my code, I am new to coding, I am able to solve questions without using functions but I am confused when I have to use functions
While calling functions you just pass the arguments and not specify its data type.
i.e. You would write it like this :-
evensum = sum_even(n);
oddsum = sum_odd(m);
The general syntax of calling a function which returns a value is :-
return-value = function-name(arg-list);

Read Multi integer from user and do some iteration and calculation

instructions are:
Write a program which reads 10 different integers from the user and finds and prints
a) Minimum element
b) Sum of all elements and
c) Average of elements.
Note: do not use arrays to store the user-entered integers.
i did b and c part like this but i can't a
there is my code:
#include <stdio.h>
void main() {
int n, i, sum = 0;
double avarage = 0;
int min = 0;
i = 1;
while (i <= 10) {
printf("Enter an integer: ");
scanf ("%d", &n);
sum += n;
if (n < min) {
min = n;
}
++i;
}
printf("Sum = %d \n", sum);
avarage = sum / 10;
printf("Avg = %.2lf \n", avarage);
printf("Min = %d \n", min);
}
this is output of my code:
How can i print Minimum of those.
you min variable starts with 0, so every number you entered is larger then that.
int min = INT_MAX;
start min with the largest possible integer will guarantee every number you take as input will be smaller
another approach is the use a flag (like boolean) for the first input, and if so directly put it into min:
int min = 0;
i = 1;
int my_flag=0;
while (i <= 10) {
printf("Enter an integer: ");
scanf ("%d", &n);
sum += n;
if (n < min) {
min = n;
}
if(my_flag==0){
min=n;
my_flag=1;
}
++i;
}

Addition of number of polynomials without using structure

#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j, n, m, c[20], s = 0;
int *p[100];
I ask the user to enter the number of polynomial equations to add:
printf("Enter the number of equations to add:\n");
scanf("%d", &m);
and ask the user to enter number of coefficients he is planning to use
printf("Enter the maximum coefficient size of largest equation:\n");
scanf("%d", &n);
for (j = 1; j <= m; j++) {
printf("Enter the coefficients of equation number %d:\n", j);
p[j] = (int*)malloc(n * sizeof(int));
for (i = n; i > 0; i--)
scanf("%d", p[j] + i);
printf("The equation %d becomes as follows:\n", j);
i = n;
printf("%dx^%d", *(p[j] + i), i - 1);
for (i = n - 1; i > 0; i--)
printf("+%dx^%d", *(p[j] + i), i - 1);
printf("\n");
}
the code works ok up to here but I am having problem to add polynomials
printf("On adding all equations we get:\n");
for (i = n; i > 0; i--) {
for (j = 1; j <= m; j++) {
s = s + (*(p[j] + i));
c[i] = s;
}
}
i = n;
printf("%dx^%d", c[i], i - 1);
for (i = n - 1; i > 0; i--)
printf("+%dx^%d", c[i], i - 1);
printf("\n");
return 0;
}
also I dont want to use any kind of other methods if possible... and can we multiply polynomials similarly?
If you change
for(j=1;j<=m;j++){
to
for(s=0,j=1;j<=m;j++){
your code gives correct output if dynamic memory allocated in differenet locations and garbage values are zeros.
You are allocating n*sizeof(int) memory for p[j]th pointer
p[j]=(int*)malloc(n*sizeof(int));
for(i=n;i>0;i--)
scanf("%d",p[j]+i);
That means you can access from p[j]+0 to p[j]+n-1. If read some element to p[j]+n that means you are accessing memory which doesn't belong to you.
I modified code a bit which is working fine
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,n,m,c[20] = {0},s=0;
int *p[100];
printf("Enter the number of equations to add:\n");
scanf("%d",&m);
printf("Enter the maximum coefficient size of largest equation:\n");
scanf("%d",&n);
for(j=1;j<=m;j++){
printf("Enter the coefficients of equation number %d:\n",j);
p[j]=(int*)malloc(n*sizeof(int));
for(i=n-1;i>=0;i--)
scanf("%d",p[j]+i);
printf("The equation %d becomes as follows:\n",j);
i=n-1;
printf("%dx^%d",*(p[j]+i),i);
for(i=n-2;i>=0;i--)
printf("+%dx^%d",*(p[j]+i),i);
printf("\n");
}
printf("On adding all equations we get:\n");
for(i=n-1;i>=0;i--){
for(s=0,j=1;j<=m;j++){
s=s+(*(p[j]+i));
c[i]=s;
}
}
i=n-1;
printf("%dx^%d",c[i],i);
for(i=n-2;i>=0;i--)
printf("+%dx^%d",c[i],i);
printf("\n");
return 0;
}
Note: Don't forgot to add check for scanf and malloc.

C Programming: How to add up divisors of an integer n

I was wondering, how can I add up the divisors displayed once I run my code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
scanf("%d", &n);
for(i = 1; i < n; i++)
{
if(n % i == 0)
{
printf("%d\n", i);
}
}
return 0;
}
If I were to enter, say, 25, it would print out 1, 5. I was wondering how to add up the two numbers 1 and 5?
Could it be as simple as this? You will want to use a simple increment operator (+=), to increment the variable sum.
int main(void)
{
int n, i, sum = 0;
if( scanf("%d", &n)!= 1){
fprintf(stderr,"Error in input\n");
exit(EXIT_FAILURE);
}
for(i = 1; i < n; i++)
{
if(n % i == 0)
{
printf("%d\n", i);
sum += i;
}
}
printf("Sum of divisors: %d\n", sum);
return 0;
}
How to add up divisors of an integer n?
Iterating n times as with for(i = 1; i < n; i++) can take a long time when n is some high value, especially if code used 64-bit integers. Instead only iterate sqrt(n) times - much faster
int factor_count(int number) {
if (number <= 1) {
return TBD_Code(number); // OP needs to define functionality for these cases.
}
int sum = 1;
int quotient;
int divisor = 1;
do {
divisor++;
quotient = number/divisor;
int remainder = number%divisor;
if (remainder == 0) {
sum += divisor;
if (quotient > divisor) sum += quotient;
}
} while (divisor < quotient);
return sum;
}
Additional improvements noted here.

sum elements of matrix and calculate the average of the i-th line

Hi all i have a problem with this exercise in Language C.
The exercise is:
Given a matrix write a function that:
A) Calculate and return the sum of the elements.
B) Calculate and return the average of the i-th line
I did my own procedure but i have a lot errors.
My procedure is:
#include <stdio.h>
#include <stdlib.h>
void main(){
int n=10;
int m=10;
int i;
int j;
int mat [i][j];
int sum=0;
for (i=0;i<n;i++){
for (j=0; j<m;j++)
sum=sum+mat[i][j];}
printf("The sum of all elements of matrix is:%d",sum);
somma=0;
for (j=0;j<m;i++){
sum=sum+mat[i][j];
sum=sum/m
printf("The average of i-th line is:%d",sum);
}
}
i think that i have to put scanf somewhere but i don't know where.
I hope that you can help me
thank you!
You declare a matrix with undefined sizes
int mat [i][j];
where i and j are uninitizlized.
You probably want
int mat [n][m];
Moreover your matrix should be inizialized with values, otherwise you'll get the sum of stack garbage.
At the end, a possible solution is
#include <stdio.h>
int main(void)
{
int n = 2;
int m = 2;
int i;
int j;
int mat[n][m];
int sum = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("Insert value of mat[%d][%d]: ", i, j);
scanf("%d", &mat[i][j]);
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
sum = sum + mat[i][j];
}
}
printf("The sum of all elements of matrix is: %d\n", sum);
for (i = 0; i < n; i++)
{
sum = 0;
for (j = 0; j < m; j++)
{
sum = sum + mat[i][j];
}
sum = sum / m;
printf("The average of line %d is: %d\n", i, sum);
}
}
As you can see I changed the average calculation:
First of all you wrote a j loop incrementing i
You must loop for all lines, so you must add a for loop that inc rows
sum must be reset each time you calculate the row average
take notes that the average is calculated using integers, so no decimals will be available.

Resources