Professional Documents
Culture Documents
Practical 04 Answers
Practical 04 Answers
Practical 04 Answers
Harshan
Practical Number 04
Areas covered Selection and iteration control structures
Part A
1. Input 10 numbers and to output number of positive, number of negative, number of
zeros.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, count_positive = 0, count_negative = 0, count_zero = 0;
#include <stdio.h>
#include <stdlib.h>
int main()
{
int mark, max = 0, min = 100, total = 0;
return 0;
}
3. Input price of 10 items and display the average value of an Item , number of items
which the price is greater than 200.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float price, total_price = 0, count_gt_200 = 0;
return 0;
}
4. Input the Employee no and the Basic Salary of the Employees in an organisation
ending with the dummy value -999 for Employee no and count the number
Employees whose Basic Salary >=5000.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int emp_no;
float basic_salary, count_gt_5000 = 0;
while (1) {
printf("Enter the employee number (-999 to end): ");
scanf("%d", &emp_no);
if (emp_no == -999) {
break;
}
printf("Enter the basic salary: ");
scanf("%f", &basic_salary);
if (basic_salary >= 5000) {
count_gt_5000++;
}
}
printf("Number of employees with a basic salary greater than or equal to Rs. 5,000/-: %d\n",
count_gt_5000);
return 0;
}
5. Input employee number, and hours worked by employees, and to display the following:
Employee number, Over Time Payment, and the percentage of employees whose Over Time
Payment exceeding the Rs. 4000/-.
The user should input –999 as employee number to end the program, and the normal Over
Time Rate is Rs.150 per hour and Rs. 200 per hour for hours in excess of 40.
#include <stdio.h>
#include <stdlib.h>
int main() {
int emp_no, hours_worked, count_gt_4000 = 0;
while (1) {
printf("Enter the employee number (-999 to end): ");
scanf("%d", &emp_no);
if (emp_no == -999) {
break;
}
printf("Enter the number of hours worked: ");
scanf("%d", &hours_worked);
float overtime_payment = 0;
if (hours_worked > 40) {
overtime_payment = (hours_worked - 40) * 200 + 40 * 150; }
else {
overtime_payment = hours_worked * 150; }
printf("Employee number: %d\n", emp_no);
printf("Overtime payment: Rs. %.2f\n", overtime_payment);
if (overtime_payment > 4000) {
count_gt_4000++; }
}
float percent_gt_4000 = (float) count_gt_4000 / 10 * 100;
printf("Percentage of employees with overtime payment exceeding Rs. 4,000/-: %.2f%%\n",
percent_gt_4000);
return 0;
}
Part B
Switch Statements
Q1) Use If-Else and write a program that reads an integer and determines and prints if the
number is even or odd. (I.e. divisible by 2)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
switch (num % 2) {
case 0:
printf("The integer is even.\n");
break;
case 1:
printf("The integer is odd.\n");
break;
}
return 0;
}
Re-write the above program using a switch statement instead of an If-Else statement!
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("The integer is even.\n");
} else {
printf("The integer is odd.\n");
}
return 0;
}
Q2) Write a simple menu driven calculator to perform (+ - / *) operations. (The program
must display a menu to select the desired operator.)
#include <stdio.h>
#include <stdlib.h>
int main()
{
float operand1, operand2, result;
int operator;
printf("Calculator Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
switch (operator) {
case 1:
result = operand1 + operand2;
break;
case 2:
result = operand1 - operand2;
break;
case 3:
result = operand1 * operand2;
break;
case 4:
result = operand1 / operand2;
break;
}
printf("Result: %.2f\n", result);
return 0;
}
Q3) Create a text-based, menu-driven program that allows the user to choose whether to
calculate the circumference of a circle, the area of a circle or the volume of a sphere. The
program should then input a radius from the user, perform the appropriate calculation
and display the result.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float radius, result;
printf("Menu:\n");
printf("1. Calculate the circumference of a circle\n");
printf("2. Calculate the area of a circle\n");
printf("3. Calculate the volume of a sphere\n");
printf("Enter a choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
result = 2 * 3.14159 * radius;
break;
case 2:
result = 3.14159 * radius * radius;
break;
case 3:
result = 4.0/3.0 * 3.14159 * radius * radius * radius;
break;
}
printf("Result: %.2f\n", result);
return 0;
}
Q5) Write a C program to read a character from the user and determine whether the given
letter is vowel or not. (Use a switch statement which also includes ‘default’ state).
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
printf("Enter a character: ");
scanf(" %c", &c);
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("%c is a vowel.\n", c);
break;
default:
printf("%c is not a vowel.\n", c);
}
return 0;
}
Q6) Write a C program to enter month number and print total number of days in month
using switch case. First assume that the given month belongs to a non-leap year.
int main()
{ int month;
printf("Enter month number: ");
scanf("%d", &month);
switch (month) {
case 1:
printf("Total number of days: 31\n");
break;
case 2:
printf("Total number of days: 28\n");
break;
case 3:
printf("Total number of days: 31\n");
break;
case 4:
printf("Total number of days: 30\n");
break;
case 5:
printf("Total number of days: 31\n");
break;
case 6:
printf("Total number of days: 30\n");
break;
case 7:
printf("Total number of days: 31\n");
break;
case 8:
printf("Total number of days: 31\n");
break;
case 9:
printf("Total number of days: 30\n");
break;
case 10:
printf("Total number of days: 31\n");
break;
case 11:
printf("Total number of days: 30\n");
break;
case 12:
printf("Total number of days: 31\n");
break;
default:
printf("Invalid month number.\n");
}
return 0; }
Loops (While, Do..While, For)
Q1) Write a C program to print numbers from 0 to 100. (You are required to write 3
separate answers each using While, Do..While, For, looping structures).
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marks[10];
int total = 0;
double average;
return 0;
}
Q3) Write a C program to calculate factorial of a user given number.
Hint:
Select an appropriate looping structure.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int factorial = 1;
if (n == 0) {
printf("Factorial: 1\n");
return 0;
}
return 0;
}
Q4) Write a C program to calculate the sum of all digits of a user given number.
If user input 123 your program should output 6. (calculated as 1+2+3)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return 0;
}
Q5) Write a C program to reverse the digits of a number using do-while statement.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
int reversed = 0;
do {
reversed = reversed * 10 + n % 10;
n /= 10;
} while (n > 0);
return 0;
}
Q6) Write a C program to calculate nth power of a given integer. The user input base and
exponent. (Do NOT use inbuilt functions, instead use a loop)
#include <stdio.h>
#include <stdlib.h>
int main() {
int base;
int exponent;
int result = 1;
return 0;
}
Q7) Write a C program to print first 10 numbers of “Fibonacci Sequence”.
(The sequence follows the rule that each number is equal to the sum of the preceding two
numbers. The Fibonacci sequence begins with the following 14 integers: 0, 1, 1, 2, 3, 5, 8, 13,
21, 34, 55, 89, 144, 233 ... Each number, starting with the third, adheres to the prescribed
formula.)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n1 = 0;
int n2 = 1;
int n3;
printf("%d\n", n1);
printf("%d\n", n2);
(An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is
equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3
= 371. Write a program to find all Armstrong number in the range of 0 and 999.)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int num_digits,;
int sum = 0;
int n_copy
int digit
n_copy = n;
while (n_copy > 0) {
digit = n_copy % 10;
sum = sum + pow(digit, num_digits);
n_copy = n_copy / 10;
}
if (sum == n) {
printf("%d is an Armstrong number.\n", n);
} else {
printf("%d is not an Armstrong number.\n", n);
}
return 0;
}
Q9) Write a C program to print all the ASCII values for letters A to Z.
#include <stdio.h>
#include <stdlib.h>
int main() {
for (char c = 'A'; c <= 'Z'; c++) {
printf("%c: %d\n", c, c);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
for (int i = 1; i <= 5; i++) {
for (int j = 0; j < i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Q11) Write a program to check whether a given number is prime or not.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
if (n < 2) {
printf("%d is not a prime number.\n", n);
return 0;
}
return 0;
}
Q12) Write a C program to print all factors of a given integer.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
return 0;
}
Q12) Write a C program to add all user inputs until user input ‘-1’. And then display the
sum.
#include <stdio.h>
int main()
{
int n;
int sum = 0;
return 0;
}
Q13) Write a C program to read user inputs for an integer array (size = 10) and print the
array.
int main() {
int arr[10];
printf("Enter 10 numbers:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
printf("Array: ");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Q14) Re-Write the above code to count all the even numbers in above integer array and
display the count.
int main() {
int arr[10];
int count = 0;
printf("Enter 10 numbers:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < 10; i++) {
if (arr[i] % 2 == 0) {
count++;
}
}
printf("Number of even numbers: %d\n", count);
return 0;
}