Showing posts with label Series. Show all posts
Showing posts with label Series. Show all posts

Friday, March 6, 2020

Sum of series 1 + 1/2 + 1/3 + 1/4 + ....N

C Program to print sum of series 1 + 1/2 + 1/3 + 1/4 + ....N for the given value of N.

For example, if N=5, output will be sum of 1 + 1/2 + 1/3 + 1/4 + 1/5, i.e. 2.28 

#include<stdio.h>
int main()
{
  int i, N;
  float sum=0;
  printf("Enter N:");
  scanf("%d", &N);
  if(N<1){
  printf("Enter value of N > 0.");
  exit(1);
  }
  for(i=1; i<=N; i++)
  {
   if(i==1)
   {
  sum=1;
  printf("Series = 1");
   }
   else
   {
     sum = sum + (float)1/i;
     printf(" + 1/%d", i);
   }
  }
  printf("\nSum of the series = %0.2f", sum);
  return 0; 
}

Output of program

Enter N:5
Series = 1 + 1/2 + 1/3 + 1/4 + 1/5
Sum of the series = 2.28



Read more C Programming Series Programs here...

< Back to Home Page >

Saturday, July 2, 2016

C Program to print A to Z using for loop.

#include<stdio.h>
int main()
{
 int i;

 for(i=65; i<=90; i++)
 {
  printf("%c ",i);
 }
 return 0;
}

Output of the program:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z


C Program to print series 10 20 30 40 50 ... N

C Program to print series 10 20 30 40 50 ... N for the given value of N.


For example, if N=5, output should be
10 20 30 40 50



#include<stdio.h>
void main()
{
 int i, N;
 printf("Enter N:");
 scanf("%d", &N);

 for(i=1; i<=N; i++)
 {
     printf("%d ",i*10);
 }
}


Output of program

Enter N:5
10 20 30 40 50

C program to print Even number series 2 4 6 8 ... N.

C Program to print Even number series 2 4 6 8 ... N for the given value of N.

For example, if N=10, output should be
2 4 6 8 10

Program Code

#include<stdio.h>
int main()
{
  int i, N;
  printf("Enter N:");
  scanf("%d", &N);

  for(i=2; i<=N; i=i+2)
  {
    printf("%d ",i);
  }
  return 0;
}

Program Code

Enter N:10
2 4 6 8 10




C Program to print Odd number series 1 3 5 7 ... N for the given value of N.



C Program to print Odd number series 1 3 5 7 ... N for the given value of N.

For example, if N=10, output should be
1 3 5 7 9


#include<stdio.h>
int main()
{
 int i, N;
 printf("Enter N:");
 scanf("%d", &N);

 for(i=1; i<=N; i=i+2)
 {
   printf("%d ",i);
 }
 return 0;
}

Output of program

Enter N:10
1 3 5 7 9

C Program to print series 5, 4, 3, 2, 1 for the given value of N.


C Program to print series 5, 4, 3, 2, 1 for the given value of N.

For example, if N=5, output should be
5, 4, 3, 2, 1


#include<stdio.h>
int main()
{
 int i, N;
 printf("Enter N:");
 scanf("%d", &N);

 for(i=N; i>0; i--)
 {
   printf("%d",i);
   //Logic avoid last , 
   if(i!=1)
     printf(", ");
 }
 return 0;
}

Output of program

Enter N:5
5, 4, 3, 2, 1


C Program to print series 1, 2, 3, 4, 5.....N for the given value of N.

C Program to print series 1, 2, 3, 4, 5.....N for the given value of N.
For example, if N=5, output should be
1, 2, 3, 4, 5


#include<stdio.h>
int main()
{
 int i, N;
 printf("Enter N:");
 scanf("%d", &N);

 for(i=1; i<=N; i++)
 {
  printf("%d",i);
 
  //Logic avoid last , 
  if(i!=N)
   printf(", ");
 }
 return 0; }

Output of program
Enter N:5
1, 2, 3, 4, 5