#include <math.h>
#include <stdio.h>
int main(){
int num, xval;
int sum=0,j=1,p;
printf("Enter number of terms: ");
scanf("%d",num);
printf("Enter value of x: ");
scanf("%d",xval);
for(int i=1;i<=num;i++){
p=pow(xval,j);
j=j+2;
if(i%2==0)
sum=sum-p;
}
else{
sum=sum+p;
}
}
printf("Sum of the series is: %d",sum);
return 0;
}
The question was to find the sum of n terms of the sin series: sin(x) = x - x3 + x5 – x7.
Related
How can I printf the series of the following series of fraction Sn=1/n+1/(n-1)+1/(n-2)+...+1 in c programming?
#include <stdio.h>
#include <stdlib.h>
int main()
{
//declaring integer variables
int i,num;
//declaring float variable
float sum = 0;
printf("Enter any number: ");
scanf("%d", &num);
//creating for loop
for(i = 1; i <= num; i++) //
{
sum = sum + 1/i + 1/i-1;
if(i==num)
{
printf("1/%d=", i);
}else
{
printf(" 1/%d +", i);
}
}
printf("The sum of the series are: %f", sum);
return 0;
}```
#include <stdio.h>
#include <stdlib.h>
/* Sn=1/n+1/(n-1)+1/(n-2)+...+1 */
int
main(int argc, char **argv)
{
unsigned n = argc > 1 ? strtoul(argv[1], NULL, 0) : 10;
float sum = 1.0;
if( n ){
printf("S(%d) = ", n);
for( ; n > 1; n-- ){
sum += 1.0/n;
printf("1/%d + ", n);
}
printf("1 =~ %f\n", sum);
}
}
I have a program where a user is asked to how many xy coordinates they want to enter. Then I would calculate the distance of the coordinates.
For example if the enter they want to put 3 coordinates I would calculate sqrt(pow(x0-x1,2) pow(x0-x2,2) pow(x1-x2) + pow(y0-y1,2) pow(y0-y2,2) pow(y1-y2)).
Calculating the distance is where I get stuck. Tried a nested loop.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
int numOfCoords;
struct CoordApp{
double x;
double y;
char xLabel[20];
char yLabel[20];
};
int main()
{
int i, j, k;
char num[5];
double minX, minY, distance, l, m;
do
{
printf("How many coordinates do you want to enter? \nPlease enter more than 2 coordinates:");
scanf("%d", &numOfCoords);
}while(numOfCoords <= 2); //Request more than 2 coordinates
struct CoordApp coords[numOfCoords]; // Assign a new struct
for(i=0; i<numOfCoords; i++) // Get X and Y coordinates
{
do
{
printf("Please enter x coordinate %d:", i);
scanf("%lf", &coords[i].x);
printf("Please enter y coordinate %d:", i);
scanf("%lf", &coords[i].y);
}while(coords[i].x < 0 || coords[i].y < 0); // Check for positive coords
strcpy(coords[i].xLabel, "XCoord");
strcat(coords[i].xLabel, itoa(i,num,5));
strcpy(coords[i].yLabel, "YCoord");
strcat(coords[i].yLabel, itoa(i,num,5));
}
for(j=0; j<numOfCoords; j++)
{
for(k=0; k<sizeof(coords)/sizeof(coords[j]); k++)
{
distance=sqrt(pow(coords[k].x-coords[j].x,2 ) + pow(coords[k].y - coords[j].y, 2)) ;
}
printf("[%lf][%lf]", coords[j].x,coords[j].y);
}
return 0;
}
Thank you for helping me with this
I'm trying to calculate the sin with the Taylor series but for some reason it doesn't work and it's returning big numbers (bigger than 1). I saw some examples on google but I don't see where I did a mistake. here's the code:
#include <stdio.h>
#include <math.h>
int factorial(int n) {
int i, f=1;
for (i=1; i<=n; i++) {
f= f*i;
}
return (f);
}
int main() {
int x, i, j=0;
float s=0;
printf("write a number to calculate sin(x)\n");
scanf("%d",&x);
for(i=1;i<=x ;i=i+2) {
if(j%2==0){
s -= pow(x, i)/factorial(i);
}
else{
s += pow(x, i)/factorial(i);
}
j++;
}
printf("sin(%d)=%f \n", x, s);
}
here's an example output: sin(6)=-34.799999
but in a calculator: sin(6)=-0.27941549819
I can't seem to get any sum right. This is a test code to see if my compiler can add 2 numbers correctly, but all it gives me is 2147344384 even if I fix the value for z. (eg. z = 6 + 4 will give me 2147344384)
#include <stdio.h>
int main()
{
int x;
int y;
printf("enter x: ");
scanf("%d", &x);
printf("\nenter y: ");
scanf("%d", &y);
int z = x + y;
printf("\n%d", z);
return 0;
}
You are doing addition before getting inputs. so move this sum syntax int z = x + y; to be after get x and y input.
#include <stdio.h>
int main()
{
int x;
int y;
printf("enter x: ");
scanf("%d", &x);
printf("\nenter y: ");
scanf("%d", &y);
int z = x + y;
printf("\n%d", z);
return 0;
}
Factorial number is a number that is multiplied by it's previous numbers. For example it's 5. 1*2*3*4*5 is it's factorial number.
I already made a program which prints factorial of any number, but i don't know how to make it to print N first factorial number in c.
For example i type 10. It must show first 10 numbers along with their factorials (Making a table)
Here is what i was made to print factorial of any number.Is there any possibility to do with while/ if else statements/ and for loop?
#include <stdio.h>
int main()
{
int i, n, fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fakt = fakt*i;
printf("Factorial of %d js %d\n", n, fakt);
getch();
}
You probably want this:
Program:
#include <stdio.h>
int main()
{
int i, n, fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i=1;i<= n;i++) //use braces to write more than one statement inside the loop
{
fakt=fakt*i;
printf("Factorial of %d is %d\n", i, fakt);
}
getch();
}
Output:
Enter a number:
5
Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
#include <conio.h>
#include <stdio.h>
void main()
{
int f=1,i,v;
clrscr();
printf("Enter the number :");
scanf("%d",&v);
for(i=1;i<=v;i++)
{
f=f*i;
printf("num =%d and fac=%d\n",i,f);
}
getch();
}
this code will work
That code already uses the for loop. The while loop equivalent is:
i = 1;
while (i <= n) {
fakt = fakt*i;
i++;
}
#include <stdio.h>
int factorial(int n)
{
int i,fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fakt = fakt*i;
return fakt;
}
int main()
{
int n;
printf("Enter a number:\n");
scanf("%d", &n);
int i = 0;
for(i=1;i<=n;i++)
{
printf("Factorial for %d is %d\n",i,factorial(i));
}
return 0;
}
I think this will do the job just fine.
You can do a nested loop.
Run the parent loop from 1 to n,
and the nested loop will be your already working for loop.
You may want this:
#include <stdio.h>
int main()
{
int n, i, num, factorial;
printf("Enter the number of terms: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
num = i;
factorial = 1;
while(num)
factorial *= num--;
printf("%d \t %d\n", i, factorial);
}
return 0;
}
Output:
Enter the number of terms: 10
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
Use this fastest version of factorial using recursion with if ..else statement
#include<stdio.h>
int fact(int n);
void main()
{
int n;
printf("\nEnter an integer:");
scanf("%d",&n);
fact(n);
}
int fact(int n)
{
int a;
if(n==0)
{
printf("The Factorial of 0 is 1\n");
return 1;
}
else
{
a=n*fact(n-1);
printf("The Factorial of %d is %d\n",n,a);
return a;
}
}
#include<stdio.h>
int main(int n){
int fact;
clrscr();
printf("Enter a number and type exit:\n");
scanf("%d",&n);
if(n!=0){
fact=n*main(n-1);
printf("Factorial of %d is %d\n",n,fact);
getch();
return fact;
}
else{
printf("Factorial of 0 is 1.\n");
getch();
return 1;
}
}