Wednesday, June 1, 2016

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

M3-R4: January 2015 (O-Level)

Q. 5(b):
Write a program to print the value of numbers in words when number entered is in the range of 0 to 999.

Ans:
Number to Words printing program: 

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

void main(){
    int num,unit_digit,ten_digit,hundred_digit;
    char numbers[10][10] = {"Zero","One","Two","Three","Four",

        "Five","Six","Seven","Eight","Nine"};
    char tens[10][10] = {"","Ten","Twenty","Thirty","Forty",

        "Fifty","Sixty","Seventy","Eighty","Ninety"};
    char teens[10][10] = {"Ten","Eleven","Twelve","Thirteen",

        "Fourteen","Fifteen","Sixteen","Seventeen",
        "Eighteen","Nineteen"};

    printf("\nEnter a number between 0 and 999: ");
    scanf("%d",&num);
    if(num<0 || num>999){
        printf("\nThe number is not between 0 and 999.");
        getch();
        exit(0);
    }
    unit_digit = num%10;
    num=num/10;
    ten_digit = num%10;
    num=num/10;
    hundred_digit = num;
    printf("\n");
    if(hundred_digit > 0){
        printf("%s Hundred ",numbers[hundred_digit]);
    }
    if(ten_digit == 1){
        printf("%s ",teens[ten_digit]);
    }
    else if(ten_digit > 1){
        printf("%s ",tens[ten_digit]);
    }
    if(unit_digit == 0){
        if(hundred_digit == 0 && ten_digit == 0){
            printf("%s",numbers[unit_digit]);
        }
    }
    else{
        printf("%s",numbers[unit_digit]);
    }
    getch();
}

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

M3-R4: January 2015 (O-Level)

Q. 5(a):
A lucas sequence is given below:
1, 3, 4, 7, 11, 18, 29
The third number is sum of previous two numbers. Write a program to print first 10 numbers of lucas sequence.

Ans:
To print first 10 numbers of the given lucas sequence: 

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


void main(){
    int first=1, second=3, third, i;
    printf("\nNumber 1: %d", first);
    printf("\nNumber 2: %d", second);
    for(i=3; i<=10; i++){
        third = first + second;
        printf("\nNumber %d: %d", i, third);
        first = second;
        second = third;
    }
    getch();
}

Wednesday, May 25, 2016

NIELIT M3-R4: Q.No. 9(b) January 2013

M3-R4: January 2013 (O-Level)

Q. 9(b):
Write a ‘C’ program to find out a factorial of a given number?

Ans:

Method 1: Finding factorial using loop: 

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


void main(){
    int num, i;
    long fact = 1;
    printf("\nEnter an integer: ");
    scanf("%d", &num);
    for(i=num; i>0; i--){
        fact = fact * i;
    }
    printf("\n%d! = %ld", num, fact); 
    getch();
}


Method 2: Finding factorial using recursive function: 

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


long fact(int);

void main(){
    int num;
    long f;
    printf("\nEnter an integer: ");
    scanf("%d", &num);
    f = fact(num);
    printf("\n%d! = %ld", num, f); 
    getch();
}


long fact(int n){
    if(n <= 0)
        return 1;
    return n * fact(n-1);
}

NIELIT M3-R4: Q.No. 7(b) January 2013

M3-R4: January 2013 (O-Level)

Q. 7(b):
Write a program to create a link list. There should be 10 nodes in the list, each node contains an integer between 1 – 10. The list should be printed at the end.

Ans:
We are putting random numbers between 1 and 10 here: 

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

struct node{
    int inf;
    struct node *next;
};

struct node * add(struct node *list, int info){
    struct node *temp;
    temp = malloc(sizeof(struct node *));
    temp->inf = info;
    temp->next = NULL;
    if(list == NULL){
        list = temp;
    }
    else{
        temp->next = list;
        list = temp;
    }
    return list;
}

void display(struct node *list){
    printf("\n");
    while(list != NULL){
        printf("%d -> ",list->inf);
        list = list->next;
    }
    printf("NULL");
}

void main(){
    struct node *linkedlist = NULL;
    int i=0,info;
    randomize();
    for(i=0;i<10;i++){
        info = rand()%10 + 1;
        linkedlist = add(linkedlist,info);
    }
    display(linkedlist);
    getch();
}

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();
}