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
1.2^2+2.3^2+3.4^2+4.5^2+ need to calculate the sum up-to nth number with C programming.
But I can't find any way to solve the program.
When the user in put 4 as the value of n, the sum will be the total of 1.2^2+2.3^2+3.4^2+4.5^2.
Can anyone help me get the algorithm?
A simple for loop would do it:
int compute(int n) {
int i, sum=0;
for(i=1; i<=n; i++) {
int val = i*(i+1)*(i+1);
sum += val;
}
return sum;
}
for(int i=0; i<n; i++). This is a for loop.
Inside the loop, store the loop iterator i in a double variable.
Add 1.2 to it.
Multiply it by itself.
Do something with the result: print it, and/or add it to a sum variable etc.
Do not use the xor operator ^ for this.
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 3 years ago.
Improve this question
This code prints the array elements, but I can't understand how does k[x-1] gives the array elements.
#include<stdio.h>
int main()
{
int x[]={2,4,6,8,10},k=1;
while (k<=5)
{
printf ("%3d",k[x-1]);
k++;
}
return 0;
}
Array indexes start at 0 in C. An array like int x[]={2,4,6,8,10} will have a value x[0]=2 and so forth. Typically, when iterating through an array, a convention like this is used:
for (int i = 0; i < length; i++)
printf("%3d",x[i]);
Since the code you provided begins the indexing at 1, you have to subtract one to fetch the proper element.
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 6 years ago.
Improve this question
So I need to calculate the determinant matrix on 2x2 matrix, simple one..
If you can, please write the code, because i don't understand like in the other thread, it's like i'm reading an English Literature + Code..
#include<stdio.h>
int main(){
int x[2][2]={{1,2},{3,4}};
int i,j,d;
for(i=0;i<=1;i++){
for(j=0;j<=1;j++){
printf(" %d ", x[i][j]);
}
printf(" \n");
}
return 0;
}
Thank You!
You don't need a loop to calculate the determinant of a 2x2 matrix, simply use:
int result = (x[0][0] * x[1][1]) - (x[0][1] * x[1][0]);
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
#include<stdio.h>
int i=0, j=0;
void main(){
int a[3][5]={1,2,{3,4,6,8},{5,8,9},10,{11,12,13,14},{21,22},23,9,8,7,6,5,4,3};//Array Initialisation
for(i=0; i<3; i++){
for(j=0; j<5; j++){
printf("\na[%d][%d]:%d\n", i, j, a[i][j]);//Array Printing
}
}
}
/*The above code initialises the array with some logic that I'm unable to understand. How are the set elements treated? Please explain */
You don't get the point of bi-dimensional array in C:
int A[2][3]
is the declaration of a bi-dimensional array of 6 integers elements with two rows and three columns. This is always true, the number in the first square brackets stands for rows, instead the number in the second square brackets stands for columns.
To initialize a bi-dimensional array you need to know these things:
int a[3][5] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
As you can see there are three curly brackets (the rows), inside these three curly brackets there are 5 numbers (the columns).
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
• Declare an array type of int and size of 5
• Ask user to enter an integer value for each element of the array
• Then display every element of the array user entered
please help me where to start i don't know much about array.
One advice, if you don't learn for yourself, then nobody can teach you. So, please try researching a bit on the internet, or reading a good book before you post a question.
Assuming this is your first mistake, and you wont repeat this without working a little on your own, i'll provide the code and the explanation..
#include<stdio.h>
int main(void)
{
int arr[5], i;
printf("please enter the numbers one by one");
for(i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
printf("The numbers you entered are\n");
for(i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
}
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 9 years ago.
Improve this question
What does the main problem in my algorithm,I need to find the smallest positive number which divided from 1 to 20 with out divider...
#include <stdio.h>
#include <stdbool.h>
int main(int argc,char* argv[])
{
int num,j=2;
int saveNum=20;
bool flag = false;
while(!flag)
{
num = saveNum;
while(num%j==0 && j<=20)
{
num /= j;
j++;
}
if(j>20)
flag = true;
else
{
saveNum++;
j=1;
}
}
printf("Done");
printf("%d",saveNum);
}
Are you missing a printf to see what your intermediate results are? That might help you get an idea as to what is going on internally.
But I don't really understand what you're trying to solve. Do you want the result: 2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20?
Because I think that's what you are currently computing. However, you'll overflow before you get there and iterating to that point will take you a while.
If instead you're trying to find the smallest number that is divisible by every number less than or equal to 20, then you may want to revisit your update of num /= j.