Application Programs in C Langauge
Application Programs in C Langauge
PART 1: Application problems in C that test the use of control structures, along with their
solutions:
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;
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;
}
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;
return 0;
}
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;
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("\n");
return 0;
}
Write a program to calculate the grade of a student based on their marks using a switch
statement. The grading system is:
Solution:
c
Copy code
#include <stdio.h>
int main() {
3
int marks;
char grade;
return 0;
}
Each problem tests a different aspect of control structures: if-else, for, while, and switch.
Solution:
c
Copy code
#include <stdio.h>
int main() {
int num, sum = 0, digit;
4
while (num != 0) {
digit = num % 10;
sum += digit;
num /= 10;
}
Solution:
c
Copy code
#include <stdio.h>
int main() {
int num, reversed = 0, remainder;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
Solution:
c
Copy code
#include <stdio.h>
int main() {
int num, original, reversed = 0, remainder;
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;
}
Solution:
c
Copy code
#include <stdio.h>
#include <math.h>
int main() {
int num, original, remainder, result = 0, n = 0;
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;
while (a != b) {
if (a > b)
a -= b;
else
b -= a;
}
Solution:
c
Copy code
#include <stdio.h>
int main() {
int num;
return 0;
}
7
Problem 7: Calculate the Sum of N Natural Numbers
Solution:
c
Copy code
#include <stdio.h>
int main() {
int n, sum = 0;
Solution:
c
Copy code
#include <stdio.h>
int main() {
int num;
8
Problem 9: Count the Number of Vowels in a String
Solution:
c
Copy code
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int count = 0;
Solution:
c
Copy code
#include <stdio.h>
int main() {
int n;
int arr[n];
9
}
return 0;
}
These problems cover a wide range of basic concepts such as loops, conditional statements,
recursion, arrays, and string manipulation.
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.
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:
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;
return 0;
}
Write a C program that allows a user to create a shopping list for a supermarket. The program
should:
Solution:
Solution:
#include <stdio.h>
int main() {
int num_items, i;
float total_cost = 0;
char item_name[50];
int quantity;
11
float price, cost;
return 0;
}
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:
Solution:
c
Copy code
#include <stdio.h>
int main() {
float basic_salary, hra, da, gross_salary;
12
gross_salary = basic_salary + hra + da;
return 0;
}
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:
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
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);
default:
printf("Invalid option. Please try again.\n");
}
return 0;
}
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:
Solution:
c
Copy code
#include <stdio.h>
#include <math.h>
int main() {
14
float principal, rate, years, future_value;
return 0;
}
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:
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:
Solution (C++):
cpp
Copy code
#include <iostream>
using namespace std;
15
int main() {
float marks[5], total = 0, average;
int i;
// Calculate average
average = total / 5;
return 0;
}
Write a C++ program that allows a user to create a shopping list for a supermarket. The program
should:
Solution (C++):
cpp
Copy code
#include <iostream>
#include <string>
using namespace std;
int main() {
int num_items;
float total_cost = 0;
16
cin >> num_items;
string item_name;
int quantity;
float price, cost;
cout << "Enter the quantity of " << item_name << ": ";
cin >> quantity;
cout << "Enter the price per unit of " << item_name << ": ";
cin >> price;
cout << "Cost for " << item_name << ": " << cost << endl;
}
return 0;
}
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:
Solution (C++):
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
float basic_salary, hra, da, gross_salary;
17
// Calculate HRA and DA
hra = 0.20 * basic_salary;
da = 0.50 * basic_salary;
return 0;
}
Write a C++ program to simulate a basic USSD service menu for mobile balance inquiry and
data bundle purchase. The program should:
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
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;
default:
cout << "Invalid option. Please try again." << endl;
}
return 0;
}
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:
Solution (C++):
cpp
Copy code
#include <iostream>
19
#include <cmath>
using namespace std;
int main() {
float principal, rate, years, future_value;
cout << "Enter the annual interest rate (in %): ";
cin >> rate;
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