1. Question:Sum of 1 + 2 + …. + n series in c programming language 

    Answer
    #include<stdio.h>
    int main(){
    
        int n,i;
        int sum=0;
    
        printf("Enter the n i.e. max values of series: ");
        scanf("%d",&n);
    
        sum = (n * (n + 1)) / 2;
    
        printf("Sum of the series: ");
    
        for(i =1;i <= n;i++){
             if (i!=n)
                 printf("%d + ",i);
             else
                 printf("%d = %d ",i,sum);
             }
       
        return 0;
    }
    Sample output: Enter the n i.e. max values of series: 5 Sum of the series: 1 + 2 + 3 + 4 + 5 = 15

    1. Report
  2. Question:Sum of 1^2 + 2^2 + …. + n^2 series in c programming language 

    Answer
    #include<stdio.h>
    
    int main(){
    
        int n,i;
        int sum=0;
    
        printf("Enter the n i.e. max values of series: ");
        scanf("%d",&n);
    
        sum = (n * (n + 1) * (2 * n + 1 )) / 6;
    
        printf("Sum of the series : ");
    
        for(i =1;i<=n;i++){
             if (i != n)
                 printf("%d^2 + ",i);
             else
                 printf("%d^2 = %d ",i,sum);
        }
    
        return 0;
    }
    Sample output: Enter the n i.e. max values of series: 5 Sum of the series: 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55

    1. Report
  3. Question:Write a c program or code to find out the sum of series 1^3 + 2^3 + …. + n^3 that is sum of cube of n natural numbers. 

    Answer
    #include<stdio.h>
    #include<math.h>
    
    int main(){
    
        int n,i;
        int sum=0;
    
        printf("Enter the n i.e. max values of series: ");
        scanf("%d",&n);
    
        sum = pow(((n * (n + 1) ) / 2),2);
    
        printf("Sum of the series : ");
    
        for(i =1;i<=n;i++){
             if (i != n)
                 printf("%d^3 + ",i);
             else
                 printf("%d^3 = %d ",i,sum);
        }
    
        return 0;
    }
    Sample output: Enter the n i.e. max values of series: 3 Sum of the series: 1^3 + 2^3 + 3^3 = 36

    1. Report
  4. Question:Write a c program to find out the sum of in A.P. series 

    Answer
    #include<stdio.h>
    #include<math.h>
    
    int main(){
    
        int a,d,n,i,tn;
        int sum=0;
    
        printf("Enter the first number of the A.P. series: ");
        scanf("%d",&a);
    
        printf("Enter the total numbers in the A.P. series: ");
        scanf("%d",&n);
    
        printf("Enter the common difference of A.P. series: ");
        scanf("%d",&d);
    
        sum = ( n * ( 2 * a + ( n -1 ) * d ) )/ 2;
        tn = a + (n-1) * d;
        printf("Sum of the series A.P.: ");
    
        for(i=a;i<=tn; i= i + d ){
             if (i != tn)
                 printf("%d + ",i);
             else
                 printf("%d = %d ",i,sum);
        }
    
        return 0;
    }
    Sample output: Enter the first number of the A.P. series: 1 Enter the total numbers in the A.P. series: 5 Enter the common difference of A.P. series: 3 Sum of the series: 1 + 4 + 7 + 10 + 13 = 35

    1. Report
  5. Question:Sum of GP series in c programming language 

    Answer
    #include<stdio.h>
    #include<math.h>
    
    int main(){
    
        float a,r,i,tn;
        int n;
        float sum=0;
    
        printf("Enter the first number of the G.P. series: ");
        scanf("%f",&a);
    
        printf("Enter the total numbers in the G.P. series: ");
        scanf("%d",&n);
    
        printf("Enter the common ratio of G.P. series: ");
        scanf("%f",&r);
    
        sum = (a*(1 - pow(r,n+1)))/(1-r);
           tn = a * (1 -pow(r,n-1));
    
        printf("tn term of G.P.: %f",tn);
        printf("\nSum of the G.P.: %f",sum);
    
        return 0;
    }
    Sample output: Enter the first number of the G.P. series: 1 Enter the total numbers in the G.P. series: 5 Enter the common ratio of G.P. series: 2 tn term of G.P. : 16.000000 Sum of the G.P. : 63.000000

    1. Report
  6. Question:Sum of infinite GP series in c programming language 

    Answer
    #include<stdio.h>
    
    int main(){
    
        float a,r;
        float sum=0;
    
        printf("Enter the first number of the G.P. series: ");
        scanf("%f",&a);
    
        printf("Enter the common ratio of G.P. series: ");
        scanf("%f",&r);
    
        if(1 > r)
             sum = a/(1-r);
        else
             sum = a/(r-1);
    
        printf("\nSum of the infinite G.P. series: %f",sum);
    
        return 0;
    }
    Sample output: Enter the first number of the G.P. series: 1 Enter the common ratio of G.P. series: .5 Sum of the infinite G.P. series: 2.000000 Enter the first number of the G.P. series: 5 Enter the common ratio of G.P. series: 2 Sum of the infinite G.P. series: 5.000000

    1. Report
  7. Question:C program to find the largest element in an array 

    Answer
    #include<stdio.h>
    int main(){
      int a[50],size,i,big;
      printf("\nEnter the size of the array: ");
      scanf("%d",&size);
      printf("\nEnter %d elements in to the array: ”, size);
      for(i=0;i<size;i++)
          scanf("%d",&a[i]);
      big=a[0];
      for(i=1;i<size;i++){
          if(big<a[i])
               big=a[i];
      }
      printf("\nBiggest: %d",big);
      return 0;
    }

    1. Report
  8. Question:C program to find the second largest element in an array 

    Answer
    #include<stdio.h>
    int main(){
      int a[50],size,i,j=0,big,secondbig;
      printf("Enter the size of the array: ");
      scanf("%d",&size);
      printf("Enter %d elements in to the array: ", size);
      for(i=0;i<size;i++)
          scanf("%d",&a[i]);
     
      big=a[0];
      for(i=1;i<size;i++){
          if(big<a[i]){
               big=a[i];
               j = i;
          }
      }
    
      secondbig=a[size-j-1];
      for(i=1;i<size;i++){
          if(secondbig <a[i] && j != i)
              secondbig =a[i];
      }
      
      printf("Second biggest: %d", secondbig);
      return 0;
    }
    Sample output: Enter the size of the array: 5 Enter 5 elements in to the array: 5 3 2 1 0 Second biggest: 3

    1. Report
  9. Question:C program to find the second smallest element in an array 

    Answer
    #include<stdio.h>
    int main(){
      int a[50],size,i,j=0,small,secondsmall;
      printf("Enter the size of the array: ");
      scanf("%d",&size);
      printf("Enter %d elements in to the array: ", size);
      for(i=0;i<size;i++)
             scanf("%d",&a[i]);
    
      small=a[0];
      for(i=1;i<size;i++){
             if(small>a[i]){
                   small=a[i];
                   j = i;
          }
      }
    
      secondsmall=a[size-j-1];
      for(i=1;i<size;i++){
             if(secondsmall > a[i] && j != i)
                  secondsmall =a[i];
      }
    
      printf("Second smallest: %d", secondsmall);
      return 0;
    }
    Sample Output: Enter the size of the array: 5 Enter 5 elements in to the array: 5 7 3 2 6 Second smallest: 3

    1. Report
  10. Question:REMOVE DUPLICATE ELEMENTS IN AN ARRAY USING C PROGRAM 

    Answer
    #include<stdio.h>
    int main(){
      int arr[50];
      int *p;
      int i,j,k,size,n;
      printf("\nEnter size of the array: ");
      scanf("%d",&n);
      printf("\nEnter %d elements into the array: ",n);
      for(i=0;i<n;i++)
        scanf("%d",&arr[i]);
      size=n;
      p=arr;
      for(i=0;i<size;i++){
        for(j=0;j<size;j++){
             if(i==j){
                 continue;
             }
             else if(*(p+i)==*(p+j)){
                 k=j;
                 size--;
                 while(k < size){
                     *(p+k)=*(p+k+1);
                     k++;
                  }
                  j=0;
              }
          }
      }
      printf("\nThe array after removing duplicates is: ");
      for(i=0;i < size;i++){
        printf(" %d",arr[i]);
      }
      return 0;
    }

    1. Report
Copyright © 2024. Powered by Intellect Software Ltd