0% found this document useful (0 votes)
24 views20 pages

Application Programs in C Langauge

Application programs in c

Uploaded by

Sharon Nyamagwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views20 pages

Application Programs in C Langauge

Application programs in c

Uploaded by

Sharon Nyamagwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

APPLICATION PROGRAMS IN C LANGAUGE

PART 1: Application problems in C that test the use of control structures, along with their
solutions:

Problem 1: Check if a Number is Prime

Write a program that takes an integer input and checks whether the number is prime or not.

Solution:

c
Copy code
#include <stdio.h>

int main() {
int num, i, isPrime = 1;

printf("Enter a number: ");


scanf("%d", &num);

if (num <= 1) {
isPrime = 0;
} else {
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}

if (isPrime)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);

return 0;
}

Problem 2: Find the Largest of Three Numbers

Write a C program that accepts three numbers from the user and determines the largest number
using if-else statements.

Solution:

1
Copy code
#include <stdio.h>

int main() {
int num1, num2, num3;

printf("Enter three numbers: ");


scanf("%d %d %d", &num1, &num2, &num3);

if (num1 >= num2 && num1 >= num3)


printf("The largest number is %d\n", num1);
else if (num2 >= num1 && num2 >= num3)
printf("The largest number is %d\n", num2);
else
printf("The largest number is %d\n", num3);

return 0;
}

Problem 3: Calculate the Factorial of a Number

Write a program that calculates the factorial of a number using a for loop.

Solution:

c
Copy code
#include <stdio.h>

int main() {
int num, i;
unsigned long long factorial = 1;

printf("Enter a number: ");


scanf("%d", &num);

if (num < 0)
printf("Factorial is not defined for negative numbers.\n");
else {
for (i = 1; i <= num; i++) {
factorial *= i;
}
printf("Factorial of %d = %llu\n", num, factorial);
}

return 0;
}

2
Problem 4: Fibonacci Sequence

Write a program to display the first n Fibonacci numbers using a while loop.

Solution:

c
Copy code
#include <stdio.h>

int main() {
int n, first = 0, second = 1, next, i = 0;

printf("Enter the number of Fibonacci terms: ");


scanf("%d", &n);

printf("Fibonacci sequence: ");


while (i < n) {
if (i <= 1)
next = i;
else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
i++;
}

printf("\n");
return 0;
}

Problem 5: Grade Calculation

Write a program to calculate the grade of a student based on their marks using a switch
statement. The grading system is:

 Marks >= 90: A


 Marks >= 80: B
 Marks >= 70: C
 Marks >= 60: D
 Marks < 60: F

Solution:

c
Copy code
#include <stdio.h>

int main() {

3
int marks;
char grade;

printf("Enter the student's marks: ");


scanf("%d", &marks);

switch (marks / 10) {


case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
default:
grade = 'F';
}

printf("The student's grade is %c\n", grade);

return 0;
}

Each problem tests a different aspect of control structures: if-else, for, while, and switch.

PART2: Application problems in C with their solutions, focusing on different


programming concepts and control structures.

Problem 1: Sum of Digits of a Number

Write a program to find the sum of the digits of a number.

Solution:

c
Copy code
#include <stdio.h>

int main() {
int num, sum = 0, digit;

printf("Enter a number: ");


scanf("%d", &num);

4
while (num != 0) {
digit = num % 10;
sum += digit;
num /= 10;
}

printf("Sum of digits: %d\n", sum);


return 0;
}

Problem 2: Reverse a Number

Write a program to reverse a given number.

Solution:

c
Copy code
#include <stdio.h>

int main() {
int num, reversed = 0, remainder;

printf("Enter a number: ");


scanf("%d", &num);

while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

printf("Reversed number: %d\n", reversed);


return 0;
}

Problem 3: Palindrome Check

Write a program to check if a number is a palindrome or not.

Solution:

c
Copy code
#include <stdio.h>

int main() {
int num, original, reversed = 0, remainder;

printf("Enter a number: ");


scanf("%d", &num);

5
original = num;

while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

if (original == reversed)
printf("%d is a palindrome.\n", original);
else
printf("%d is not a palindrome.\n", original);

return 0;
}

Problem 4: Armstrong Number

Write a program to check whether a number is an Armstrong number.

Solution:

c
Copy code
#include <stdio.h>
#include <math.h>

int main() {
int num, original, remainder, result = 0, n = 0;

printf("Enter a number: ");


scanf("%d", &num);
original = num;

// Count number of digits


for (int temp = num; temp != 0; ++n)
temp /= 10;

while (num != 0) {
remainder = num % 10;
result += pow(remainder, n);
num /= 10;
}

if (result == original)
printf("%d is an Armstrong number.\n", original);
else
printf("%d is not an Armstrong number.\n", original);

return 0;
}

6
Problem 5: Find the GCD of Two Numbers

Write a program to find the greatest common divisor (GCD) of two numbers using the Euclidean
algorithm.

Solution:

c
Copy code
#include <stdio.h>

int main() {
int a, b;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

while (a != b) {
if (a > b)
a -= b;
else
b -= a;
}

printf("GCD: %d\n", a);


return 0;
}

Problem 6: Generate a Multiplication Table

Write a program to generate a multiplication table for a given number up to 10.

Solution:

c
Copy code
#include <stdio.h>

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

for (int i = 1; i <= 10; i++) {


printf("%d x %d = %d\n", num, i, num * i);
}

return 0;
}

7
Problem 7: Calculate the Sum of N Natural Numbers

Write a program to calculate the sum of the first N natural numbers.

Solution:

c
Copy code
#include <stdio.h>

int main() {
int n, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &n);

for (int i = 1; i <= n; i++) {


sum += i;
}

printf("Sum of the first %d natural numbers is: %d\n", n, sum);


return 0;
}

Problem 8: Find the Factorial of a Number Using Recursion

Write a recursive function to calculate the factorial of a number.

Solution:

c
Copy code
#include <stdio.h>

unsigned long long factorial(int n) {


if (n == 0)
return 1;
else
return n * factorial(n - 1);
}

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

printf("Factorial of %d is %llu\n", num, factorial(num));


return 0;
}

8
Problem 9: Count the Number of Vowels in a String

Write a program to count the number of vowels in a given string.

Solution:

c
Copy code
#include <stdio.h>
#include <string.h>

int main() {
char str[100];
int count = 0;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);

for (int i = 0; i < strlen(str); i++) {


if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o'
|| str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O'
|| str[i] == 'U') {
count++;
}
}

printf("Number of vowels: %d\n", count);


return 0;
}

Problem 10: Find the Largest and Smallest Elements in an Array

Write a program to find the largest and smallest elements in an array.

Solution:

c
Copy code
#include <stdio.h>

int main() {
int n;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

int arr[n];

printf("Enter the elements: ");


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);

9
}

int largest = arr[0], smallest = arr[0];

for (int i = 1; i < n; i++) {


if (arr[i] > largest) {
largest = arr[i];
}
if (arr[i] < smallest) {
smallest = arr[i];
}
}

printf("Largest element: %d\n", largest);


printf("Smallest element: %d\n", smallest);

return 0;
}

These problems cover a wide range of basic concepts such as loops, conditional statements,
recursion, arrays, and string manipulation.

PART 3: FURTHER PROBLEMS IN C PROGRAMMING LANGUAGE

These problems focus on using key C programming concepts in handling input and output, using
arrays, conditional statements/structures, loops, and mathematical functions in C, while applying
them in practical situations/problems in common real-world scenarios involving grading, shopping
lists, gross salary calculation, basic USSD-based service interactions, and investment planning.

Problem 1: Grade System

Write a C program to calculate the grade of a student based on their marks. The program should
prompt the user to input marks for five subjects. Calculate the average of the marks and assign a
grade according to the following criteria:

 Marks >= 90: A


 Marks >= 80 and < 90: B
 Marks >= 70 and < 80: C
 Marks >= 60 and < 70: D
 Marks < 60: F

Solution:

c
Copy code
#include <stdio.h>

int main() {
float marks[5], total = 0, average;
int i;

10
// Input marks for five subjects
printf("Enter marks for five subjects: \n");
for (i = 0; i < 5; i++) {
printf("Subject %d: ", i + 1);
scanf("%f", &marks[i]);
total += marks[i];
}

// Calculate average
average = total / 5;

// Determine grade based on average marks


printf("Average Marks: %.2f\n", average);
if (average >= 90)
printf("Grade: A\n");
else if (average >= 80)
printf("Grade: B\n");
else if (average >= 70)
printf("Grade: C\n");
else if (average >= 60)
printf("Grade: D\n");
else
printf("Grade: F\n");

return 0;
}

Problem 2: Supermarket Shopping List

Write a C program that allows a user to create a shopping list for a supermarket. The program
should:

1. Ask the user how many items they want to purchase.


2. For each item, input the name, quantity, and price per item.
3. Display the total cost for the entire shopping list.

Solution:

Solution:

#include <stdio.h>

int main() {
int num_items, i;
float total_cost = 0;

printf("Enter the number of items to purchase: ");


scanf("%d", &num_items);

char item_name[50];
int quantity;

11
float price, cost;

for (i = 0; i < num_items; i++) {


// Input item details
printf("Enter the name of item %d: ", i + 1);
scanf("%s", item_name);

printf("Enter the quantity of %s: ", item_name);


scanf("%d", &quantity);

printf("Enter the price per unit of %s: ", item_name);


scanf("%f", &price);

// Calculate total cost for the current item


cost = quantity * price;
total_cost += cost;

printf("Cost for %s: %.2f\n", item_name, cost);


}

// Display total cost


printf("\nTotal cost of all items: %.2f\n", total_cost);

return 0;
}

Problem 3: Gross Salary Calculation

Write a C program to calculate the gross salary of an employee. The user inputs the basic salary,
and the program calculates the gross salary based on the following:

 House Rent Allowance (HRA) = 20% of basic salary


 Dearness Allowance (DA) = 50% of basic salary
 Gross Salary = Basic Salary + HRA + DA

Solution:

c
Copy code
#include <stdio.h>

int main() {
float basic_salary, hra, da, gross_salary;

// Input basic salary


printf("Enter the basic salary: ");
scanf("%f", &basic_salary);

// Calculate HRA and DA


hra = 0.20 * basic_salary;
da = 0.50 * basic_salary;

// Calculate gross salary

12
gross_salary = basic_salary + hra + da;

// Output the results


printf("House Rent Allowance (HRA): %.2f\n", hra);
printf("Dearness Allowance (DA): %.2f\n", da);
printf("Gross Salary: %.2f\n", gross_salary);

return 0;
}

Problem 4: USSD Code-Based Service

Write a C program to simulate a basic USSD service menu for mobile balance inquiry and data
bundle purchase. The program should display a menu where the user can:

1. Check their balance.


2. Purchase a data bundle.

For balance inquiry, the user should be able to see their current balance. For data bundle
purchase, the user selects from 1GB, 2GB, or 5GB packages. Display the corresponding cost for
each.

Solution:

c
Copy code
#include <stdio.h>

int main() {
int option;
float balance = 500.0;// Example balance

// Display the USSD menu


printf("USSD Service Menu\n");
printf("1. Check Balance\n");
printf("2. Buy Data Bundle\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &option);

switch (option) {
case 1:
// Check balance
printf("Your current balance is: $%.2f\n", balance);
break;

case 2:
// Purchase data bundle
printf("Select Data Bundle:\n");
printf("1. 1GB - $5\n");
printf("2. 2GB - $10\n");
printf("3. 5GB - $20\n");

13
int bundle_choice;
scanf("%d", &bundle_choice);

if (bundle_choice == 1 && balance >= 5) {


balance -= 5;
printf("You purchased 1GB. Remaining balance: $%.2f\n",
balance);
} else if (bundle_choice == 2 && balance >= 10) {
balance -= 10;
printf("You purchased 2GB. Remaining balance: $%.2f\n",
balance);
} else if (bundle_choice == 3 && balance >= 20) {
balance -= 20;
printf("You purchased 5GB. Remaining balance: $%.2f\n",
balance);
} else {
printf("Insufficient balance or invalid option.\n");
}
break;

default:
printf("Invalid option. Please try again.\n");
}

return 0;
}

Problem 5: Investment Calculation

Write a C program to calculate the future value of an investment based on the principal amount,
rate of interest, and number of years. Use the following formula:

Where:

 P is the principal amount


 R is the annual interest rate (in percentage)
 T is the number of years

Solution:

c
Copy code
#include <stdio.h>
#include <math.h>

int main() {

14
float principal, rate, years, future_value;

// Input principal amount, rate of interest, and number of years


printf("Enter the principal amount: ");
scanf("%f", &principal);

printf("Enter the annual interest rate (in %%): ");


scanf("%f", &rate);

printf("Enter the number of years: ");


scanf("%f", &years);

// Calculate future value


future_value = principal * pow((1 + rate / 100), years);

// Output the result


printf("The future value of the investment is: %.2f\n", future_value);

return 0;
}

PART 4: C++ VERSIONS OF FURTHER PROBLEMS IMPLEMENTED ABOVE IN C


PROGRAMMING LANGUAGE

Here are the C++ versions for Problem 1 (Grade System), Problem 2 (Supermarket
Shopping List), Problem 3: Gross Salary Calculation, Problem 4: USSD Code-
Based Service and Problem 5: Investment Calculation:

Problem 1: Grade System (C++ Version)

Write a C++ program to calculate the grade of a student based on their marks. The user inputs
marks for five subjects. Calculate the average of the marks and assign a grade according to the
following criteria:

 Marks >= 90: A


 Marks >= 80 and < 90: B
 Marks >= 70 and < 80: C
 Marks >= 60 and < 70: D
 Marks < 60: F

Solution (C++):

cpp
Copy code
#include <iostream>
using namespace std;

15
int main() {
float marks[5], total = 0, average;
int i;

// Input marks for five subjects


cout << "Enter marks for five subjects: " << endl;
for (i = 0; i < 5; i++) {
cout << "Subject " << i + 1 << ": ";
cin >> marks[i];
total += marks[i];
}

// Calculate average
average = total / 5;

// Determine grade based on average marks


cout << "Average Marks: " << average << endl;
if (average >= 90)
cout << "Grade: A" << endl;
else if (average >= 80)
cout << "Grade: B" << endl;
else if (average >= 70)
cout << "Grade: C" << endl;
else if (average >= 60)
cout << "Grade: D" << endl;
else
cout << "Grade: F" << endl;

return 0;
}

Problem 2: Supermarket Shopping List (C++ Version)

Write a C++ program that allows a user to create a shopping list for a supermarket. The program
should:

1. Ask the user how many items they want to purchase.


2. For each item, input the name, quantity, and price per item.
3. Display the total cost for the entire shopping list.

Solution (C++):

cpp
Copy code
#include <iostream>
#include <string>
using namespace std;

int main() {
int num_items;
float total_cost = 0;

cout << "Enter the number of items to purchase: ";

16
cin >> num_items;

string item_name;
int quantity;
float price, cost;

for (int i = 0; i < num_items; i++) {


// Input item details
cout << "Enter the name of item " << i + 1 << ": ";
cin >> item_name;

cout << "Enter the quantity of " << item_name << ": ";
cin >> quantity;

cout << "Enter the price per unit of " << item_name << ": ";
cin >> price;

// Calculate total cost for the current item


cost = quantity * price;
total_cost += cost;

cout << "Cost for " << item_name << ": " << cost << endl;
}

// Display total cost


cout << "\nTotal cost of all items: " << total_cost << endl;

return 0;
}

Problem 3: Gross Salary Calculation (C++ Version)

Write a C++ program to calculate the gross salary of an employee. The user inputs the basic
salary, and the program calculates the gross salary based on the following:

 House Rent Allowance (HRA) = 20% of basic salary


 Dearness Allowance (DA) = 50% of basic salary
 Gross Salary = Basic Salary + HRA + DA

Solution (C++):

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
float basic_salary, hra, da, gross_salary;

// Input basic salary


cout << "Enter the basic salary: ";
cin >> basic_salary;

17
// Calculate HRA and DA
hra = 0.20 * basic_salary;
da = 0.50 * basic_salary;

// Calculate gross salary


gross_salary = basic_salary + hra + da;

// Output the results


cout << "House Rent Allowance (HRA): " << hra << endl;
cout << "Dearness Allowance (DA): " << da << endl;
cout << "Gross Salary: " << gross_salary << endl;

return 0;
}

Problem 4: USSD Code-Based Service (C++ Version)

Write a C++ program to simulate a basic USSD service menu for mobile balance inquiry and
data bundle purchase. The program should:

1. Check the balance.


2. Purchase a data bundle.

For balance inquiry, the user should be able to see their current balance. For data bundle
purchase, the user selects from 1GB, 2GB, or 5GB packages. Display the corresponding cost for
each.

Solution (C++):

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int option;
float balance = 500.0; // Example balance

// Display the USSD menu


cout << "USSD Service Menu" << endl;
cout << "1. Check Balance" << endl;
cout << "2. Buy Data Bundle" << endl;
cout << "Enter your choice (1 or 2): ";
cin >> option;

switch (option) {
case 1:
// Check balance
cout << "Your current balance is: $" << balance << endl;
break;

case 2:

18
// Purchase data bundle
cout << "Select Data Bundle:" << endl;
cout << "1. 1GB - $5" << endl;
cout << "2. 2GB - $10" << endl;
cout << "3. 5GB - $20" << endl;

int bundle_choice;
cin >> bundle_choice;

if (bundle_choice == 1 && balance >= 5) {


balance -= 5;
cout << "You purchased 1GB. Remaining balance: $" << balance
<< endl;
} else if (bundle_choice == 2 && balance >= 10) {
balance -= 10;
cout << "You purchased 2GB. Remaining balance: $" << balance
<< endl;
} else if (bundle_choice == 3 && balance >= 20) {
balance -= 20;
cout << "You purchased 5GB. Remaining balance: $" << balance
<< endl;
} else {
cout << "Insufficient balance or invalid option." << endl;
}
break;

default:
cout << "Invalid option. Please try again." << endl;
}

return 0;
}

Problem 5: Investment Calculation (C++ Version)

Write a C++ program to calculate the future value of an investment based on the principal
amount, rate of interest, and number of years. Use the following formula:

Where:

 P is the principal amount


 R is the annual interest rate (in percentage)
 T is the number of years

Solution (C++):

cpp
Copy code
#include <iostream>

19
#include <cmath>
using namespace std;

int main() {
float principal, rate, years, future_value;

// Input principal amount, rate of interest, and number of years


cout << "Enter the principal amount: ";
cin >> principal;

cout << "Enter the annual interest rate (in %): ";
cin >> rate;

cout << "Enter the number of years: ";


cin >> years;

// Calculate future value


future_value = principal * pow((1 + rate / 100), years);

// Output the result


cout << "The future value of the investment is: " << future_value <<
endl;

return 0;
}

These C++ programs are the equivalent of the previous C programs, but using C++ input/output
streams (cin, cout) and some minor adjustments for syntax.

20

You might also like