You are on page 1of 3

PROGRAMMING CAT 2

JONES ANYONA
SCT-253-023/2022
UNIT CODE:2123
(a)Outline four rules one should adhere to when naming identifiers
1. An identifier name is case sensitive such that home and home is recognized as two separate
identifiers.
2. An identifier cannot be same as a C keyword.
3. The first character of an identifier must be either an alphabet or underscore.
4. Any special characters other than alphabets, digits, and underscore (such as: blank space, /
are not allowed).

(b) State four types of bit wise operators


1. & Bitwise AND
2. ^ Bitwise exclusive OR
3. >> Shift right
4. << Shift left

(c) Explain the function of the following string functions


i. Strcpy - Concatenates str2 at the end of str1
ii. Strncpy - Copies given number of characters of one string to another
iii. Strcat – Concatenates str2 at the end of str1

(d) Explain three advantages of using functions in programing


1. The program can be modularized into smaller parts.
2. The function provides modularity.
3. The function provides reusable code.
4. In large programs, debugging and editing tasks is easy with the use of functions.
(e) Using a for a loop, write a c program that provides the following output
#include <stdio.h> int main() { int
marks[4], i, n, sum = 0, ;
printf("Enter number of elements: ");
scanf("%d", &n); for(i=0; i<n; ++i)
{ printf("Enter number%d:
",i+1); scanf("%d", &marks[i]);
// adding integers entered by the user to the sum variable sum
+= marks[i];
} printf("Average = %d",
average); return 0;
}

(f) Using a while loop, write a program that computes the factorial of number 9

#include <stdio.h>

int main() { int


num = 9; int
fact = 1;
int i = 1;

while (i <= num) {


fact *= i; i++; }

printf("The factorial of %d is %d", num, fact);

return 0;
}

You might also like