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

Tuesday, May 24, 2016

NIELIT M3-R4: Q.No. 5(b) January 2014

 M3-R4: January 2014 (O-Level)

Q. 5(b):
Write a ‘C’ program to find the power of number using function.

Ans:
Finding power of a number using function.

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


long power(int, int);

void main(){
    int x, n;
    long result;
    printf("\nEnter a number: ");
    scanf("%d", &x);
    printf("\nEnter power: ");
    scanf("%d", &n); 
    result = power(x,n);
    printf("\n%d to the power %d is %ld",x,n,result);
    getch();
}

long power(int x, int n){
    int i;
    long prod=1;
    for(i=0; i<n; i++){
       prod = prod * x;
    }
    return prod;
}

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

 M3-R4: January 2014 (O-Level)

Q. 5(a):
Write a ‘C’ program to count number of words in a string.

Ans:
Program to count number of words in a string:

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

void main(){
    char str[80];
    int i, len, count=0;
    printf("\nEnter a string: ");
    gets(str);
    len = strlen(str);
    for(i=0; i<len; i++){
        if(str[i] == ' ')
            count++;
    }
    count++;
    printf("\nNumber of words in the string is %d", count);
    getch();
}