EXERCISE-4

1. Write a program in C to display the n terms of even natural number and their sum.
SOURCE CODE:
#include<stdio.h>
int main(){
    int n,i=1,c=1,sum=0;
    printf("Enter number of terms : ");
    scanf("%d",&n);
    printf("Even natural numbers are : \n");
    while(c<=n){
        if(i%2==0){
            printf("%d ",i);
            sum=sum+i;
            c++;
        }
        i++;
    }
    printf("\nSum of even natural numbers is: %d\n",sum);
    return 0;
}
OUTPUT:
Enter number of terms : 5
Even natural numbers are :
2 4 6 8 10
Sum of even natural numbers is: 30

2. Write a program in C to display the n terms of harmonic series and their sum. 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms.
SOURCE CODE:
#include<stdio.h>
int main(){
    int n,i;
    float sum=0;
    printf("Enter range:");
    scanf("%d",&n);
    printf("Series is:\n");
    for(i=1;i<=n;i++){
        printf("1/%d",i);
        if(i<n)
            printf("+");
        sum = sum + (float)1/i;
    }
    printf("\nSum of series is: %f",sum);
    return 0;
}
OUTPUT:
Enter range:5
Series is:
1/1+1/2+1/3+1/4+1/5
Sum of series is: 2.283334
3. Write a C program to check whether a given number is an Armstrong number or not.
SOURCE CODE:
/*Program to check whether the given number is Armstrong number or not.*/
#include<stdio.h>
#include<math.h>
int main(){
    int n,temp,arm=0,c=0;
    printf("Enter number:");
    scanf("%d",&n);
    temp=n;
    while(temp>0){
        temp=temp/10;
        c++;
    }
    temp=n;
    while(temp>0){
        arm=arm+pow((temp%10),c);
        temp=temp/10;
    }
    if(n==arm)
        printf("Given number is Armstrong.");
    else
        printf("Given number is not Armstrong.");
    return 0;
}
OUTPUT-1:
Enter number:371
Given number is Armstrong.

OUTPUT-2:
Enter number:151
Given number is not Armstrong.