National University: of Computer & Emerging Sciences Peshawar Campus

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

National University

of Computer & Emerging Sciences Peshawar Campus


Student Name: _____________________ Roll No: __________________
Program: BS (SE) Examination: Sessional-II
Semester: Fall-2022 Total Marks: 30 Weightage: 15
Time Allowed: 01 hour Date: 10th November, 2022
Course: Programming Fundamentals (CS1002) Instructor Name: Ms. Sara Rehmat

NOTE: Attempt all questions. Please try your best to solve the questions in the order they are given.

Question 1 (CLO 3): Marks: 10

Write a program to print all Armstrong's numbers from 1 to 10000. A number is an Armstrong’s number if it
is equal to the sum of its own digits with each digit raised to the power of the number of digits. For example
371 and 1634 are Armstrong’s numbers as 371 = 33 + 73 + 13 and 1634 = 14 + 64 + 34 + 44.

You have to define the following functions in your program:

a. A function num_digits with an integer parameter n that returns the number of digits in n.
b. A function power with two positive integer parameters x and y that returns the value of x
raised to power y.
c. A function armstrong with a positive integer parameter n that returns 1 if n is an Armstrong’s
number, else 0.
d. A function print_armstrong that prints all Armstrong's numbers from 1 to 10000.

Question 2 (CLO 1): Write the output of the following program: Marks: 10

#include <stdio.h>

void towers(int,char,char,char);

int main()
{
towers(3,'A','C','B');
return 0;
}

void towers (int n, char source, char dest, char auxiliary)


{

static int step = 0;

printf("Towers (%d, %c, %c, %c) \n", n, source, dest, auxiliary);

if(n == 1)
printf("\t\t\t\tStep %3d: Move from %c to %c\n", ++step, source, dest);
else
{
towers(n - 1, source, auxiliary, dest);
printf("\t\t\t\tStep %3d: Move from %c to %c\n", ++step, source, dest);
towers(n - 1, auxiliary, dest, source);
}
return ;

Question 3 (CLO 1) : Marks: 10

Consider the following program that has to print the number of vowels and the number of consonants in the
given array, but the program may contain syntactical or logical errors.

Rewrite the given program after removing syntactical and logical errors if any. Write the type of error
(syntactical or logical) in the form of a comment in front of each line if that line contains any error.

#include <stdio.h>

#define a 'a';

int main()
{

int arr[] = ['z','b','x','e','f','i'];

int vowel_count = 1;
int consonant_count = 1;

for (int i=0; i<7; i++)


{

switch(arr)
{
case a:
case 'e':
case 'i':
case 'o':
case 'u':
vowel_count++;
break;

default:
break;
consonant_count++;
}

printf("Vowels are %d and consonants are %d",consonant_count, vowel_count);


return 0;

------------------------------------------------------Best of luck--------------------------------------------------------

You might also like