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

Tuesday, May 24, 2016

NIELIT M3-R4: Q.No. 7(a) July 2013

M3-R4: July 2013 (O-Level)

Q. 7(a):
Define recursion. Write a complete program to evaluate the given series using recursive function sum( ). Here n is user dependent.
1 + 2 + 3 +…+ n

Ans:

Recursion: A recursive function is a function that calls itself repeatedly.

Program to evaluate sum of the given series using recursive function:

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

int sum(int);
 
void main(){
    int n, s;
    printf("\nEnter n: ");
    scanf("%d", &n);
    s = sum(n);
    printf("\nSum = %d", s);
    getch();
}

int sum(int n){
    if(n == 1)
        return 1;
    
    return (n + sum(n-1));
}
 

NIELIT M3-R4: Q.No. 6(b) July 2013

M3-R4: July 2013 (O-Level)

Q. 6(b):
Write a ‘C’ program to compute the average of every third integer lying between 1 and 200? Include appropriate documentation.

Ans:
Program to compute the average of every third integer between 1 and 200. Here are assuming 1 and 200 inclusive:

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

void main(){
    float average;
    int i, sum=0, n=0;
    for(i=1; i<=200; i+=3){
        sum = sum + i;
        n++;
    }
    average = (float) sum / n;
    printf("\nAverage = %f", average);
    getch();
}

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

M3-R4: July 2013 (O-Level)

Q. 6(a):
Write a program having function print_char( ) which receives a character and n integer as arguments. The function should print n times the entered character.

Ans:
Program to print a character n times using a function print_char():

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

void print_char(char, int);

void main(){
    print_char('%', 50);
    print_char('*', 30);
    getch();
}

void print_char(char ch, int n){
    int i;
    printf("\n");
    for(i=0; i<n; i++)
        printf("%c", ch);
    getch();
}

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

M3-R4: July 2013 (O-Level)

Q. 5(a):
Write a program to calculate number of vowels (a,e,i,o,u) separately in an entered string.

Ans:
Calculating number of vowels separately in a string:

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

void main(){
    char str[80];
    int i, len, count[5];
    printf("\nEnter a string: ");
    gets(str);
    len = strlen(str);
    for(i=0; i<len; i++){
        switch(str[i]){
            case 'a':
            case 'A':
                count[0]++;
                break;
            case 'e':
            case 'E':
                count[1]++;
                break;
            case 'i':
            case 'I':
                count[2]++;
                break;
            case 'o':
            case 'O':
                count[3]++;
                break;
            case 'u':
            case 'U':
                count[4]++;
                break; 
        }
    }
    printf("\nNumber of 'a': %d", count[0]);
    printf("\nNumber of 'e': %d", count[1]); 
    printf("\nNumber of 'i': %d", count[2]); 
    printf("\nNumber of 'o': %d", count[3]); 
    printf("\nNumber of 'u': %d", count[4]);  
    getch();
}