Showing posts with label NIELIT O-Level July 2014. Show all posts
Showing posts with label NIELIT O-Level July 2014. Show all posts

Tuesday, May 24, 2016

NIELIT M3-R4: Q.No. 6(a) July 2014

M3-R4: July 2014 (O-Level)

Q. 6(a):
Write a program to determine the sum of the following series:
S = 1 – 3 + 5 – 7 + …n
Read the value of n from the user.

Ans:
Program to determine the sum of the above series:

#include <stdio.h>
#include <conio.h>
#include <math.h>

void main(){
    int sum=0, n, i, term;
    printf("\nEnter number of terms (n):");
    scanf("%d", &n);
    for(i=0; i<n; i++){
        term = (2*i+1) * pow(-1,i);
        sum = sum + term;
    }
    printf("\nThe sum of the series is %d", sum);
    getch();
}

NIELIT M3-R4: Q.No. 5(c) July 2014

M3-R4: July 2014 (O-Level)

Q. 5(c):
Write a function to display the multiplication table of the number.

Ans:
Function to display the multiplication table of a number passed as an argument:

void multiTable(int num){
    int i;
    printf("\nMultiplication Table of %d:\n", num);
    for(i=1; i<=10; i++){
        printf("\n%d x %d = %d", num, i, num*i);
    }
    printf("\n");
    getch();
}

The above function can be called from main() as:

multiTable(12);

NIELIT M3-R4: Q.No. 5(a) July 2014

M3-R4: July 2014 (O-Level)

Q. 5(a):
Write a function which accepts an array of size n containing integer values and returns average of all values. Call the function from main program.

Ans:
First lets write the function:

float average(int arr[], int size){
    int i;
    int sum = 0;
    float avg;
    
    for(i=0; i<size; i++){
        sum = sum + a[i];
    }

    avg = (float) sum / size;

    return avg;
}

Calling the function average() from main():

void main(){
    int arr[100], n, i;
    float avg;
    printf("\nEnter number of elements: ");
    scanf("%d", &n);
    for(i=0; i<n; i++){
        printf("\nElement %d: ",i+1);
        scanf("%d", &arr[i]);
    }
    avg = average(arr, n);
    printf("\nThe average is: %f", avg);
    getch();
}