PL 1 Sheets
PL 1 Sheets
2) Write a C program to declare two integer and one float variables, then initialize them to 10, 15,
and 12.6. It then prints these values on the screen.
3) Write a C program to prompt the user (i.e. ask the user) to input 3 integer values and print these
values in the reversed order.
4) Given the following pseudo code, write a program that executes it.
a. read x
b. read y
c. compute p = x y
d. compute s = x + y
e. total = s2 + p ( s – x ) ( p + y )
f. print total
5) Write a C program that finds the area of a triangle. Note: a triangle’s area = 0.5 * base * height
6) Write a C program that prompts the user to input three integer values and find the greatest value
of those three values.
7) Write a program that determines a student’s grade. The program will read three scores and
determine the grade based on the following rules:
● if the average score >= 90% => grade = A
● if the average score >= 70% and < 90% => grade = B
● if the average score >= 50% and < 70% => grade = C
● if the average score < 50% => grade = F
9) Write a C program that compares two numbers a and b. The output of this comparison is whether
the two numbers are equal, that a is greater, or that b is greater.
10) Write a C program that finds the type of a triangle when its three angles are given.
● If all angles are equal, it is an equilateral triangle.
● If any two angles are equal, it is an isosceles triangle.
2 /1
11) Write a C program that prompts the user to choose an operation to do on 2 input integers. The
operations are addition, subtraction, multiplication, and division. Note: the program should not allow
the division by zero.
14) Write a C program that finds the sum of the first n natural numbers.
15) Write a C code that finds the sum of the digits of a number.
2 /2
2) Repeat the problems from 5 to 16 in Sheet#0 using functions, and you should call them from the
main. The functions should return a value to the main and the main is responsible for printing the
output.
4) Write a C program to find the sum of the first n natural numbers using recursion.
Note: Positive integers are known as natural numbers.
5) Write a C program to check whether a number is a prime number or not, by using recursion.
2 /1
2 /2
3) Write a C program that reads 10 integer numbers from the user, and then the program should
calculate the sum of the odd numbers, and the sum of the even numbers.
4) Write a C program that asks the user to enter 10 integers in an array. The program will then
display (based on the entered numbers) one of the following messages:
● "the numbers in the array are increasing",
● "the numbers in the array are decreasing",
● "the numbers in the array are not changing", or
● "the numbers in the array are increasing and then decreasing."
5) Write a C program that reads a matrix (3🞨4), and asks the user to choose a number, and then
displays the position of the selected number if found, otherwise it displays “number not found.”
6) Write a C program to read a matrix from the user, and then display the row with the maximum
total/sum (that is, the row whose sum of elements is maximum).
7) By using a two-dimensional array, write a C program to display the matrix shown below:
0 1 1 1 1
-1 0 1 1 1
-1 -1 0 1 1
-1 -1 -1 0 1
-1 -1 -1 -1 0
8) By using a two-dimensional array, write a C program to display a Pascal triangle of any size. In a
Pascal triangle, the first & second rows are set to 1. Each element of the triangle (starting from the
third row downwards) is the sum of the element directly above it and the element to the left of the
element directly above it. See the following example of a Pascal triangle (with a size = 5):
1
2/1
10) Write a C function that reads the number of students in a class, and five grades for each student.
The function should then compute the average grade for each student.
11) Write a C function that checks if a matrix is sparse or not. Given that a sparse matrix is a matrix
in which most of the elements are zero (that is, the number of zero-valued elements are more than
50% of the total number of elements).
12) Write a C function that reads a matrix and checks whether the given matrix is a symmetric
matrix or not. Given that: If a square matrix A is equal to its transpose AT, then it is a symmetric
matrix. For example: if the elements of the matrix are:
1 2 3
2 4 5
3 5 8
Then the matrix is symmetric
13) Write a C program that reads the radius of a circle, and then calls a function that returns the
circumference and the area of that circle. The program should include a global constant variable.
14) Write a C program that reads a 1-D array of any size, then calls a function that returns the
following:
● The maximum value in the array.
● The minimum value in the array.
● The average value of the array.
2/2
2) Write a C program that reads 10 characters from the user, and then searches for the position of
the character z.
3) Write a C program that reads string S1 and certain letter from the user, then call your own
function that return the number of occurrences of the given character in the given string.
4) Write a C function that take two strings (array of characters) and return one if the 1st is part of
the 2nd and zero otherwise
6) Write a function ``replace'' which takes a string as a parameter and replaces all spaces in that
string by minus signs and delivers the number of spaces it replaced.
b) int main()
{
enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
printf("%d, %d, %d, %d, %d, %d\n", MON, TUE, WED, THU,
FRI, SAT);
return 0;
}
2 /1
a) For the previous declaration, write a function which input one employee data.
b) For the previous declaration, write a function which take an array of employee’s data and an
id_no, the function should search for that employee in the employees array and return his salary, or
return -1 if the employee id not found.
c) Use all the previous in a program that enter data for 5 employees, then the program takes one
id_no for an employee, search for it, if found return his salary, otherwise write "NOT FOUND".
main() {
struct s {
double x;
int y;
} a_struct;
printf("The size of a_struct: %d-byte\n",
sizeof(a_struct));
}
2 /2
2) Write a C program that reads 10 characters from the user, and then searches for the position of the
character z.
4) Write a C program that reads 10 integer numbers from the user, and then the program should
calculate the sum of the odd numbers, and the sum of the even numbers.
1/1
1/1
2) Write a program similar to the previous one, but instead of an integer, use a string initialized with
your own name.
• For a trip, prompts the user for the number of driven miles, and the fuel consumption.
• Calls a function with three parameters: (1) the number of miles, (2) the fuel consumption,
and (3) the fuel consumption per mile - which is calculated within the function.
• Prints the fuel consumption per mile – which is calculated within the function.
4) Write a program that reads 5 integers to an array. The integers should then be printed. Use pointer
arithmetic.
5) Modify the preceding program so that it would calculate the sum of the integers. Use a
pointer variable for the sum.
6) Write a function which takes as arguments: (1) a pointer to an integer array, and (2) the number of
items of the array. The function then finds and return the greatest item. In the main function, the user
should enter 8 numbers to be stored in the array. The function is then called, and the returned
greatest item is printed (in the main function).
9) Write a recursive function that checks if a string is entirely consisting of digits, or not.
10) Write a program that asks the user to enter his/her name and age. Then asks the user whether
(s)he is a U.S. citizen. If the answer is Yes, ask the user to enter the name of the state where (s)he
comes from. Otherwise, ask the user to enter the name of the country (s)he comes from. (You're
required to use a struct and a union in your program).
4 /1
A B
void swap_nums( int *x, int *y ) { #include <stdio.h>
int tmp; int add_two( int x, int y )
tmp = *x; {
*x = *y; static int counter = 1;
*y = tmp; printf("This is the function call of %d,\n",
} counter++);
return ( x + y );
void swap_pointers( char *x, char *y ) { }
char *tmp;
tmp = x; /* the main function */
x = y; main()
y = tmp; {
} int i, j;
for ( i = 0, j = 5; i < 5; i++, j-- )
int main() { printf( "the addition of %d and %d is
int a,b; char *s1,*s2; %d.\n\n", i, j, add_two( i, j ) );
a = 3; b=4; return 0;
swap_nums( &a, &b ); }
printf( "a is %d\n", a);
printf( "b is %d\n", b);
s1 = "I should print second";
s2 = "I should print first";
swap_pointers( s1, s2 );
printf( "s1 is %s\n", s1 );
printf( "s2 is %s\n", s2 );
return 0;
}
struct var_type {
int type_in_union;
union {
float un_float;
char un_char;
int un_int;
} vt_un;
} var_type;
4 /3
main() {
var_type.type_in_union = FLOAT_TYPE;
var_type.vt_un.un_float = 3.5;
print_vt();
var_type.type_in_union = CHAR_TYPE;
var_type.vt_un.un_char = 'a';
print_vt();
}
4 /4
1/1