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 7 years ago.
Improve this question
I need to create a program that gives the sum of the all the numbers between the constants of a and b given by the user. b needs to be greater than a.
#include <stdio.h>
void main()
{
int index, begno, endno, sum = 0;
printf("Program for sum of all numbers in the given range\n");
printf("Enter Beg. No.: ");
scanf("%d", &begno);
printf("Enter End. No.: ");
scanf("%d", &endno);
index = begno;
for(; index <= endno; index ++)
sum = sum + index;
printf("The sum of even numbers between %d and %d is: %d", begno, endno, sum);
}
The code given looks OK, but if you want the sum without including the last number, as is generally the case you should change the for loop like this
for(; index < endno; index ++)
I would start by implementing a loop to compute:
$$\sum_{n=a}^{b}
n$$
Related
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 2 years ago.
Improve this question
So the problem is:" Create a program, that inputs the elements(integers) of a matrix a[n][m] and finds the element with the smallest value amongst the the elements with the biggest value of every column."
So the way I understand it, I have to go through every column and find the biggest number in each, so I will end up with 4 numbers. From these 4 numbers I have to print out the smallest.
int main()
{
int n,m;
int A[n][m];
int B[m];
int max;
int min;
int i,j;
printf("Enter a number for n: ");
scanf("%d",&n);
printf("Enter a number for m: ");
scanf("%d", &m);
for(i=1; i<=n;i++)
{
for(j=1; j<=m;j++)
{
printf("Enter a number [%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
for(j=1; j<=m; j++)
{
max=A[j][1];
for( i=1; i<=n; i++)
{
if(max<A[j][i])
{
max=A[j][i];
B[j]=max;
}
}
}
min=B[1];
for(i=1; i<=m; i++)
{
if(min>B[i])
{
min=B[i];
printf("%d ", B[i]);
}
}
printf("Min is: %d\n", min);
return 0;
}
My question is where would the error be? It must be a logical one.
I apologize for not asking my question as clearly as most of you in this forum, but it's my first time asking here. Please if you answer, explain it like you would to a beginner.
Thanks!
C programs are generally executed from top to bottom. Therefore you cannot do this:
int n,m;
int A[n][m];
Because n and m has not been given any values. To give them values further down doesn't mean that the line int A[n][m]; will somehow get re-executed.
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 2 years ago.
Improve this question
So i want my code actualy had different sum each cases, but the sum keep adding from the other cases, like for example the cases 2 sum are sum from loop 2nd + cases 1, the cases 4 sum are sum from cases 1,2,3 and loop number 4
the Output.
`
#include <stdio.h>
int main() {
int cases;
int day;
int animal;
int n;
int a;
int sum = 0;
animal = 0;
printf("Enter cases \n ");
scanf(" %d", &n);
for (cases = 0; cases < n; cases++)
{
printf("cases #%d\n", cases+1);
printf("Enter how many days.\n");
scanf(" %d", &a);
for(day=1;day<=a;){
printf("Enter how many animal that you capture at day #%d\n", day);
scanf(" %d", &animal);
day++;
sum = sum + animal;
}
animal = 0;
day = 1;
printf("cases#%d = %d\n", cases + 1, sum);
}
return 0;
}`
You need to return sum to 0 in every case
Put sum=0; under for (cases...
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 3 years ago.
Improve this question
Whats the meaning of [i] in the following example?
#include <stdio.h>
int main()
{
int values[5];
printf("Enter 5 integers: ");
for (int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]); // HERE
}
printf("Displaying integers: ");
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
i here is a variable.
In your code, [i] acts as the index of values and is used to access the element in array values.
Edit:
Since there is a //HERE comment in your code, im going to assume you would also want what [i] does there. The expression &value[i] basically gives the address of value[i] ,i.e, the "ith" element of the array.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Problem is :- Write a C code that ask the user to enter 10 numbers, then ask him to enter another number to search on it in the 10 numbers and print its location in case it is found. In case the number is not found, it will print number no exist.
Code I write is :-
#include<stdio.h>
void main(void)
{
int n, i, value,j;
for (i=1;i<=10;++i)
{
printf("enter number %d : ",i);
scanf("%d",&n);
}
printf("Enter the value to search: ");
scanf("%d",&value);
/*next part of code is not correct
that can not search to find the place of number */
for (j=1;j<=10;++j)
{
if (value == n)
{
printf("value is exist at element number %d",n);
}
else
{
printf("value is not exist\n");
}
}
}
output will be:-
(after enter the numbers).
Enter the value to search is 12.
value is exist at element 9
#include <stdio.h>
int main(){
//there are 10 int in n
int n[10], i, value;
for (i=0;i<10;i=i+1)//array count from 0 ,i=i+1 same as i++
{
printf("enter number %d : ",i);
scanf(" %d",&(n[i]));//& mean get address so you will push what you input to n[0]~n[9]
//little tip before %d remain a space for some reason if you keep learn you will know
}
printf("Enter the value to search: ");
scanf(" %d",&value);//& mean get address so you push what you input to value here
for (i=0;i<10;i=i+1)
{
if (value == n[i])
{
printf("value is exist is element number %d\n",n[i]);
break;//break mean out of for loop
//
}
}
if(i==10){//if search all not found then i will be 10 because after loop i will +1
//if break i will not +1
printf("value is not exist\n");
}
return 0;//remember "int" main() so you need return 0
}
Keep learning you will be stronger
Array is simple
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm a beginner and I am having a really hard time while doing this program.
The question is:
(1/1!)+(2/2!)+(3/3!)+(4/4!)- - - -n
So here are the n number of terms(in which a number is divided by its factorial) and I have to display the output of the sum of any number of terms which are given in scanf function.
Only one thing I know is that this program can be done by using "Nested for" loop but I haven't perfect grip yet on C language. So you guys have to have help me out in this. :)
#include <stdio.h>
#include <conio.h>
void main(void){
int s,a,b,n,fact=1;
//clrscr();
printf("Enter number of terms=");
scanf("%d",&n);
for(a=1;a<=n;a++) {
fact=fact*a;
b=(a/fact);
printf("Sum=%d",s);
}
getche();
}
P.S It's must for me to do it with "Nested for" loop.
No you do not need any Nested for loops to solve your problem. Here's a procedure you may follow:
function factorial
Input: numbers L.
Output: factorial of L.
function sum
Input: n.
Output: sum.
sum = 0;
for i = 1 to n, do
sum ← sum + (i / factorial(i))
return sum
#include <stdio.h>
int main(void) {
// your code goes here
int n;
float sum = 0,d,fact =1,j,i;
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++){
fact = 1;
for (j = 1; j <= i; j++){
fact = fact * j;
}
d = (float) i / (float) fact ;
sum = sum + d;
}
printf("sum = %f", sum);
return 0;
}
Its working ..you can check over here :-https://ideone.com/JVXQVX