computer c prog 2 (2)
computer c prog 2 (2)
Introduction to C
C is a high-level, general-purpose programming language known for its efficiency, flexibility,
and portability. Developed for system programming, it provides low-level memory access while
maintaining a structured programming approach. It is widely used in operating systems,
embedded systems, game development, and application software. C's simplicity and power have
influenced many modern languages like C++, Java, and Python, making it one of the most
fundamental and widely used programming languages in the world. Known for its structured
approach and simplicity, C is widely used in operating system development (Linux, Windows
kernel), database systems (MySQL, Oracle), game engines, and even modern AI applications.
Its influence is seen in many popular languages, including C++, Java, Python, and Go, making it
a foundational language for programmers and computer science students worldwide.
History of C
C was developed by Dennis Ritchie at Bell Labs in 1972 as an improvement over the B
language, which was derived from BCPL. It was originally created to develop the Unix
operating system, which was later rewritten in C, demonstrating its power and efficiency. In
1978, Brian Kernighan and Dennis Ritchie published The C Programming Language, also
known as K&R C. The language was standardized as ANSI C (C89) in 1989, followed by
updates like C99, C11, and C18, introducing new features while maintaining its core simplicity.
Even after 50+ years, C remains a dominant language in software development, system
programming, and embedded applications.The C Programming Language, commonly known as
K&R C, which became the foundation for future versions. The first official standard, ANSI C
(C89/C90), was introduced in 1989, followed by C99, C11, and C18, each bringing
improvements while maintaining its core simplicity and efficiency. Over five decades, C has
remained one of the most widely used, reliable, and versatile programming languages, playing a
crucial role in modern computing, from embedded systems to supercomputers.
Features of c
C is a powerful, efficient, and flexible programming language with several key features that
make it widely used in software development. One of its most important features is portability,
meaning C programs can run on different hardware and operating systems with minimal
modifications. Being a compiled language, C ensures fast execution, making it suitable for
performance-critical applications. It also provides low-level memory access through pointers,
making it ideal for system programming, operating systems, and embedded systems. C follows a
structured programming approach, allowing developers to write modular code using functions,
improving readability and maintainability. Additionally, C comes with a rich library of built-in
functions and supports external libraries for advanced functionalities. Its simplicity, extensibility,
and direct hardware interaction make it one of the most widely used and fundamental languages
1
in operating system development, game programming, database management, and even artificial
intelligence applications.
1. Write a c program to find the sum of two numbers using a function.
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main() {
int num1, num2, sum;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
sum = add(num1, num2);
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
return 0;
}
Output
2
2. Write a c program to find the square of two numbers.
#include <stdio.h>
int square(int n)
{
return n * n;
}
int main() {
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("The square of %d is: %d\n", num1, square(num1));
printf("The square of %d is: %d\n", num2, square(num2));
return 0;
}
Output
3
3. Write a c program to find divisible of two number using function
#include <stdio.h>
int isDivisible(int a, int b) {
return (a % b == 0);
}
int main() {
int num1, num2;
Output
4
4. WAP to find the greatest among two numbers using function.
#include <stdio.h>
int findGreatest(int a, int b) {
return (a > b) ? a : b;
}
int main() {
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("The greatest number between %d and %d is: %d\n", num1, num2, findGreatest(num1,
num2));
return 0;
}
Output
5
5. WAP to find the area of a circle using function.
#include <stdio.h>
#define PI 3.1416
float calculateArea(float radius) {
return PI * radius * radius;
}
int main() {
float radius;
Output
6
6. WAP to print 4*4 matrix.
#include <stdio.h>
int main() {
int matrix[4][4];
int i, j;
printf("Enter 16 elements for the 4x4 matrix:\n");
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("\nThe 4x4 matrix is:\n");
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output
7
7. WAP to create a 3*3 Matrix.
#include <stdio.h>
int main() {
int matrix[3][3];
int i, j;
printf("Enter 9 elements for the 3x3 matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("\nThe 3x3 matrix is:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output
8
8. Write a program to multiply 2*3 matrix.
#include <stdio.h>
int main() {
int A[2][3] = {{1, 2, 3}, {4, 5, 6}};
int B[2][3] = {{7, 8, 9}, {10, 11, 12}};
int result[2][3];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = A[i][j] * B[i][j];
}
}
printf("Element-wise Multiplication Result:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d\t", result[i][j]);
}
printf("\n");
}
return 0;
}
Output
9
9. WAP to add 4*4 matrix.
#include <stdio.h>
int main() {
int A[4][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
int B[4][4] = {{16, 15, 14, 13}, {12, 11, 10, 9}, {8, 7, 6, 5}, {4, 3, 2, 1}};
int result[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
printf("Matrix Addition Result:\n");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("%d\t", result[i][j]);
}
printf("\n");
}
return 0;
}
Output
10
10. WAP to transpose 3*3 matrix.
#include <stdio.h>
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
transposed[j][i] = matrix[i][j];
}
}
}
int main() {
// Input 3x3 matrix
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int transposed[3][3];
transposeMatrix(matrix, transposed);
printf("Original Matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
printf("\nTransposed Matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", transposed[i][j]);
}
printf("\n");
}
return 0;}
11
Output
12
12. WAP to print a fibonacci series of 5 odd numbers using an array.
#include <stdio.h>
int main() {
int n = 5, fib[10], oddFib[5], j = 0;
fib[0] = 0;
fib[1] = 1;
for (int i = 2; j < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
if (fib[i] % 2 != 0) {
oddFib[j] = fib[i];
j++;
}
}
printf("First 5 odd Fibonacci numbers:\n");
for (int i = 0; i < n; i++) {
printf("%d ", oddFib[i]);
}
return 0;
}
Output
13
13. WAP to print fibonacci series of vowel using array.
#include <stdio.h>
int main() {
int n = 5, fib[5];
char vowels[] = {'A', 'E', 'I', 'O', 'U'};
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
printf("Fibonacci series of vowels:\n");
return 0;
}
Output
14
14.WAP to print a fibonacci series of 5 negative integers using an array.
#include <stdio.h>
int main() {
int n = 5, fib[5];
fib[0] = -1;
fib[1] = -1;
return 0;
}
Output
15
15. WAP to print a fibonacci series of n factorial numbers using an array.
#include <stdio.h>
long long factorial(int n) {
long long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;}
return fact;}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
long long fact = factorial(n);
long long fib[fact];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < fact; i++) {
fib[i] = fib[i - 1] + fib[i - 2];}
printf("Fibonacci series of %d factorial numbers:\n", n);
for (int i = 0; i < fact; i++) {
printf("%lld ", fib[i]); }
return 0;}
Output
16
16. Write a php program to find sum of two numbers.
<?php
class SumCalculator {
private $num1, $num2;
17
17. Write a php program to find whether the number is odd or even give
output.
<?php
class NumberCheck {
private $number;
18
<?php
class SimpleInterest {
private $principal, $rate, $time;
public function __construct($principal, $rate, $time) {
$this->principal = $principal;
$this->rate = $rate;
$this->time = $time;
}
public function calculateInterest() {
return ($this->principal * $this->rate * $this->time) / 100;
}
public function displayResult() {
echo "The Simple Interest is: " . $this->calculateInterest();
}
}
$interestObj = new SimpleInterest(1000, 5, 2);
$interestObj->displayResult();
?>
Output
19
<!DOCTYPE html>
<html>
<body>
<input id="num" type="number">
<button onclick="check()">Check</button>
<p id="result"></p>
<script>
function check() {
var num = document.getElementById("num").value;
document.getElementById("result").innerText = num % 2 === 0 ? "The number is
Even." : "The number is Odd.";
}
</script>
</body>
</html>
Output
If your input is 5 then,
20. Write a java script program to find greatest among two using do while
loop.
<!DOCTYPE html>
<html>
20
<body>
<input id="num1" type="number" placeholder="Enter first number">
<input id="num2" type="number" placeholder="Enter second number">
<button onclick="findGreatest()">Find Greatest</button>
<p id="result"></p>
<script>
function findGreatest() {
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
var result;
do {
if (num1 > num2) {
result = "The greatest number is " + num1;
} else if (num2 > num1) {
result = "The greatest number is " + num2;
} else {
result = "Both numbers are equal." }
break;
} while (false);
document.getElementById("result").innerText = result;}
</script>
</body>
</html>
Output
If you input 5 and 8 then,
21
21. Write a java script program to find difference of two number.
<!DOCTYPE html>
<html>
<body> <input id="num1" type="number">
<input id="num2" type="number">
<button onclick="calc()">Difference</button>
<p id="result"></p><script>
function calc() {
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
document.getElementById("result").innerText = "The difference is " + (num1 -num2)
}</script>
</body></html>
Output
If you input10 and 4 then,
22. Write a java script program to find the area of the triangle.
<!DOCTYPE html>
<html>
<body>
22
<input id="base" type="number">
<input id="height" type="number">
<button onclick="area()">Area</button>
<p id="result"></p>
<script>
function area() {
var base = document.getElementById("base").value;
var height = document.getElementById("height").value;
document.getElementById("result").innerText = "The area is " + (base * height) / 2;
}
</script>
</body>
</html>
Output
If you input 5 and 10,
23.Write a java script program to find whether the word is palindrome or not
using a while loop.
<!DOCTYPE html>
<html>
<body>
<input id="word" type="text" placeholder="Enter a word">
<button onclick="checkPalindrome()">Check Palindrome</button>
<p id="result"></p>
<script>
function checkPalindrome() {
var word = document.getElementById("word").value;
var start = 0;
23
var end = word.length - 1;
var isPalindrome = true;
while (start < end) {
if (word[start] !== word[end]) {
isPalindrome = false;
break; }
start++;
end--;
}
if (isPalindrome) {
document.getElementById("result").innerText = "The word is a palindrome.";
} else {
document.getElementById("result").innerText = "The word is not a palindrome."; }
}</script>
</body>
</html>
Output
If you input the word racecar then,
24
<h2 style="color: #333;">Multiplication of Two Numbers</h2>
<label>Enter first number:</label>
<input type="number" id="num1" style="margin: 10px; padding: 5px;">
<br>
<label>Enter second number:</label>
<input type="number" id="num2" style="margin: 10px; padding: 5px;">
<br>
<button onclick="multiply()" style="margin-top: 10px; padding: 8px 15px; background-color:
#28a745; color: white; border: none; cursor: pointer;">Multiply</button>
<h3 id="result" style="color: blue; margin-top: 20px;"></h3>
<script>
function multiply() {
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
var result = num1 * num2;
document.getElementById("result").innerHTML = "Result: " + result;
}
</script>
</body>
</html>
Output
25
25. WAP using inline css to find greatest number among two.
<!DOCTYPE html>
<html>
<head>
<title>Find Greatest Number</title>
</head>
<body style="font-family: Arial, sans-serif; text-align: center; padding: 20px;">
<h2 style="color: #333;">Find the Greatest Number</h2>
<label>Enter first number:</label>
<input type="number" id="num1" style="margin: 10px; padding: 5px;">
<br>
<label>Enter second number:</label>
<input type="number" id="num2" style="margin: 10px; padding: 5px;">
<br>
<button onclick="findGreatest()" style="margin-top: 10px; padding: 8px 15px;
background-color: #007bff; color: white; border: none; cursor: pointer;">Find Greatest</button>
<h3 id="result" style="color: blue; margin-top: 20px;"></h3>
<script>
function findGreatest() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
26
if (isNaN(num1) || isNaN(num2)) {
document.getElementById("result").innerHTML = "Please enter valid numbers.";
} else {
var greatest = (num1 > num2) ? num1 : num2;
document.getElementById("result").innerHTML = "Greatest Number: " + greatest;
}
}
</script>
</body>
</html>
Output
26. Write a html css program to change celsius into fahrenheit using
internal CSS.
<!DOCTYPE html>
<html>
<head>
<title>Celsius to Fahrenheit</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 20px;
background-color: #f4f4f4;
}
27
h2 {
color: #ff5733;
margin-bottom: 15px;
}
input {
padding: 8px;
width: 150px;
border: 1px solid #ccc;
border-radius: 5px;
text-align: center;
}
button {
margin-top: 10px;
padding: 8px 15px;
background-color: #ff5733;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #e04e2a;
}
h3 {
color: #333;
margin-top: 15px;
}
</style>
</head>
<body>
<h2>Convert Celsius to Fahrenheit</h2>
<input type="number" id="celsius" placeholder="Enter °C">
<br>
28
<button onclick="convertToFahrenheit()">Convert</button>
<h3 id="result"></h3>
<script>
function convertToFahrenheit() {
var celsius = parseFloat(document.getElementById("celsius").value);
if (isNaN(celsius)) {
document.getElementById("result").innerHTML = "Please enter a valid number.";
} else {
var fahrenheit = (celsius * 9/5) + 32;
document.getElementById("result").innerHTML = `${celsius}°C =
${fahrenheit.toFixed(2)}°F`;
}
}
</script>
</body>
</html>
Output
29
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 20px;
background-color: #f4f4f4;
}
h2 {
color: #ff5733;
margin-bottom: 15px;
}
input {
padding: 8px;
width: 150px;
border: 1px solid #ccc;
border-radius: 5px;
text-align: center;
}
button {
margin-top: 10px;
padding: 8px 15px;
background-color: #ff5733;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}button:hover
background-color: #e04e2a;
}
h3 {
color: #333;
margin-top: 15px;
}
</style>
30
</head>
<body>
<h2>Calculate Perimeter of a Square</h2>
<input type="number" id="side" placeholder="Enter side length">
<br>
<button onclick="calculatePerimeter()">Calculate</button>
<h3 id="result"></h3>
<script>
function calculatePerimeter() {
var side = parseFloat(document.getElementById("side").value);
if (isNaN(side) || side <= 0) {
document.getElementById("result").innerHTML = "Please enter a valid positive
number.";
} else {
var perimeter = 4 * side;
document.getElementById("result").innerHTML = `Perimeter: ${perimeter}`;
}
}
</script>
</body>
</html>
Output
31
28. Write css program to change farhenhite to celsius using external CSS.
<!DOCTYPE html>
<html><head>
<title>Fahrenheit to Celsius Converter</title>
<link rel="stylesheet" type="text/css" href="styles.css"></head>
<body>
<h2>Convert Fahrenheit to Celsius</h2>
<input type="number" id="fahrenheit" placeholder="Enter °F"><br>
<button onclick="convertToCelsius()">Convert</button>
<h3 id="result"></h3> <script>
function convertToCelsius() {
var fahrenheit = parseFloat(document.getElementById("fahrenheit").value);
if (isNaN(fahrenheit)) {
document.getElementById("result").innerHTML = "Please enter a valid number.";
} else { var celsius = (fahrenheit - 32) * 5/9;
document.getElementById("result").innerHTML = `${fahrenheit}°F =
${celsius.toFixed(2)}°C`; }
} </script>
</body>
</html>
Output
32
29. Write a html css program to find whether the given number is armstrong
or not using external css
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Armstrong Number Checker</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Armstrong Number Checker</h1>
<form id="armstrongForm">
<label for="number">Enter a number:</label>
<input type="number" id="number" required>
<button type="submit">Check</button>
</form>
<div id="result"></div>
</div>
<script>
document.getElementById('armstrongForm').addEventListener('submit', function(event) {
event.preventDefault();
33
let num = document.getElementById('number').value;
let originalNum = num;
let sum = 0;
let numberOfDigits = num.length;
Output
class Vehicle:
def __init__(self, make, model, year):
34
self.make = make
self.model = model
self.year = year
def display_vehicle(self):
print(f"Vehicle Information: {self.year} {self.make} {self.model}")
vehicle1.display_vehicle()
vehicle2.display_vehicle()
vehicle3.display_vehicle()
Output
35
self.age = age
def display_dog(self):
print(f"Dog's Name: {self.name}, Breed: {self.breed}, Age: {self.age} years old")
dog1.display_dog()
dog2.display_dog()
dog3.display_dog()
Output
36
input_string = "Hello World, this is an example."
vowel_display = Vowel(input_string)
vowel_display.display_vowels()
Output
def display_greatest(self):
if self.num1 > self.num2:
print(f"The greatest number is: {self.num1}")
elif self.num2 > self.num1:
print(f"The greatest number is: {self.num2}")
else:
print("Both numbers are equal.")
37
34. Write an oop to display name of 5 richest people in the world.
class RichestPeople:
def __init__(self, people):
self.people = people
def display_richest(self):
print("The 5 richest people in the world are:")
for person in self.people:
print(person)
rich_people_list = ["Elon Musk", "Jeff Bezos", "Bernard Arnault", "Bill Gates", "Larry
Ellison"]
richest_people = RichestPeople(rich_people_list)
richest_people.display_richest()
Output
38
35. Write an oop to display the sum of two numbers.
class SumOfNumbers:
def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2
def display_sum(self):
total = self.num1 + self.num2
print(f"The sum of {self.num1} and {self.num2} is: {total}")
def display_multiplication(self):
result = 1
for num in self.numbers:
result *= num
print(f"The multiplication of the numbers is: {result}")
39
numbers_list = [2, 3, 4, 5]
multiplication = Multiplication(numbers_list)
multiplication.display_multiplication()
Output
def display_matrix(self):
print("The 3x3 Matrix is:")
for row in self.matrix:
print(row)
matrix_data = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix_object = Matrix(matrix_data)
matrix_object.display_matrix()
Output
40
38. Write an oop to display 5 subjects.
class Subjects:
def __init__(self, subjects):
self.subjects = subjects
def display_subjects(self):
print("The 5 subjects are:")
for subject in self.subjects:
print(subject)
41
class Cone:
def __init__(self, radius, height):
self.radius = radius
self.height = height
def display_volume(self):
volume = (1/3) * math.pi * self.radius**2 * self.height
print(f"The volume of the cone with radius {self.radius} and height {self.height} is:
{volume:.2f}")
42