final c manual
final c manual
No: 1
PROGRAM USING I/O STATEMENTS AND EXPRESSIONS
Date:
AIM:
ALGORITHM:
Step-1: Start the program.
Step-2: Declare variables and initializations.
1
SAMPLE OUTPUT:
Enter the limit: 10
The sum of odd numbers is 25
The sum of even numbers is 30
The absolute difference is 5
RESULT:
2
Ex. No: 2
DESIGN A CALCULATOR TO PERFORM BASIC OPERATIONS
Date:
AIM:
To write C Design a calculator to perform the operations, namely, addition, subtraction,
Multiplication, division and square of a number.
ALGORITHM:
Step-1: Start
Step-2: Read the inputs from user.
Step-3: Initialize i as 0 and list.
Step-4: Perform the operations, namely, addition, subtraction, multiplication, division and square of a
given number.
Step-5: Print the value stored in sum, difference, product, quotient, square.
Step-6: Stop.
PROGRAM:
#include<stdio.h>
#include <conio.h>
int main()
{
int firstNumber, secondNumber, sum, difference, product, quotient;
long square;
printf("Enter First Number: ");
scanf("%d", &firstNumber);
printf("Enter Second Number: ");
scanf("%d", &secondNumber);
sum = firstNumber + secondNumber;
printf("\nSum = %d", sum);
difference = firstNumber - secondNumber;printf("\nDifference = %d", difference);
product = firstNumber * secondNumber;printf("\nMultiplication = %d", product);
quotient = firstNumber / secondNumber;printf("\nDivision = %.3f", quotient);
square = firstNumber *firstNumber;printf("\n Square= %ld", square);
getch();
}
return 0;
3
SAMPLE OUTPUT:
Enter First Number: 25
Enter Second Number: 4
Sum = 29
Difference = 21
Multiplication = 100
Division = 6.250
Square = 625
RESULT:
Thus the C Program for Arithmetic operations has been executed and the output was obtained.
4
Ex. No: 3
Date: GENERATION OF PRIMENUMBERS AND CHECK ARMSTRING OR NOT
AIM:
To write C Program to generate prime numbers and check the given numbers are Armstrong or not
ALGORITHM:
STEP 2: Declare all required variables and initialize the result variable “sum” to 0.
STEP 3: Get an input from the user and store it in “n” and “n_copy” variables.
STEP 4: Using the user input, perform the following operation until it becomes
ZEROr = n %10;
Sum = sum + (r*r*r);
n = n/10;
STEP 5: compare the value of “sum” and n_copy”. If they are equal then go to step no 6
Otherwise go to step no 7.
STEP 6: print the result as “It is an Armstrong Number” and go to step 8.
PROGRAM:
#include <stdio.h>
#include <math.h>
// Function to check if a number is prime
int is Prime(int num)
{
if (num <= 1) return 0; // 0 and 1 are not prime
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) return 0; // Found a divisor, not prime
}
return 1; // It's a prime number
}
// Function to check if a number is an Armstrong number
int is Armstrong(int num) {
int originalNum = num, sum = 0, digits = 0;
5
sum += pow(digit, digits);
originalNum /= 10;
}
return sum == num; // Check if it's an Armstrong number
}
int main() {
int lower, upper;
if (isArmstrong(checkNum))
{
printf("%d is an Armstrong number.\n", checkNum);
} else {
printf("%d is not an Armstrong number.\n", checkNum);
}
return 0;
}
SAMPLE OUTPUT:
RESULT:
Thus the C Program to generate prime numbers and check the given numbers are Armstrong or not.
6
Ex. No: 4
Date: Greatest number using Array(one- dimensional)
AIM:
To write C Program to generate greatest number using one-dimensional Array.
ALGORITHM:
PROGRAM:
#include <stdio.h>
int find_greatest(int arr[], int size)
{
if (size == 0)
{
return -1; // Indicate an empty array (or handle as needed)
}
int max_value = arr[0]; // Start with the first element
for (int i = 1; i < size; i++)
{
if (arr[i] > max_value)
{
max_value = arr[i];
}
}
return max_value;
}
int main()
{
int array[] = {3, 1, 4, 1, 5, 9, 2, 6};
int size = sizeof(array) / sizeof(array[0]);
int greatest_number = find_greatest(array, size);
if (greatest_number != -1)
{
printf("The greatest number is: %d\n", greatest_number);
}
else
{
printf("The array is empty.\n");
7
}
return 0;
}
SAMPLE OUTPUT:
RESULT:
Thus the C Program to generate greatest numbers using one-dimensional array was successfully executed.
8
Ex. No: 5
Matrix Multiplication using array (Two-Dimensional)
Date:
AIM:
To write a C Program to calculate matrix multiplication using two dimensional array.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#define MAX 10
void multiplyMatrices(int first[MAX][MAX], int second[MAX][MAX], int result[MAX][MAX], int row1,
int col1, int row2, int col2)
{
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
result[i][j] = 0;
}
}
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
for (int k = 0; k < col1; k++) {
result[i][j] += first[i][k] * second[k][j];
}
}
}
}
int main() {
int first[MAX][MAX], second[MAX][MAX], result[MAX][MAX];
int row1, col1, row2, col2;
printf("Enter rows and columns for first matrix: ");
scanf("%d %d", &row1, &col1);
printf("Enter elements of first matrix:\n");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
scanf("%d", &first[i][j]);
}
}
9
printf("Enter rows and columns for second matrix: ");
scanf("%d %d", &row2, &col2);
if (col1 != row2) {
printf("Matrix multiplication not possible. Columns of first matrix must equal rows of second matrix.\n");
return 1;
}
// Input second matrix elements
printf("Enter elements of second matrix:\n");
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
scanf("%d", &second[i][j]);
}
}
// Multiply the matrices
multiplyMatrices(first, second, result, row1, col1, row2, col2);
// Display the result
printf("Resultant matrix:\n");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
SAMPLE OUTPUT:
Enter rows and columns for first matrix: 2 3
Enter elements of first matrix:
123
456
Enter rows and columns for second matrix: 3 2
Enter elements of second matrix:
7 8
9 10
11 12
Resultant matrix:
1863 36
4650 99
RESULT:
Thus the C Program for calculating matrix multiplication was successfully executed.
10
Ex. No: 6
AVERAGE HEIGHT OF PERSONS
Date:
AIM:
To write a C Program to populate an array with height of persons and find how many persons are
above the average height.
ALGORITHM:
Step-1: Start
Step-2: Declare variables
Step-3: Read the total number of persons and their height.
Step-4: Calculate avg=sum/n and find number of persons their h>avg.
Step-5: Display the output of the calculations.
Step-6: Stop.
PROGRAM:
/* Get a Height of Different Persons and find how many of them are are above average */
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n,sum=0,count=0,height[100];float avg;
clrscr();
//Read Number of persons
printf("Enter the Number of Persons : ");
scanf("%d",&n);
//Read the height of n persons
printf("\nEnter the Height of each person in centimeter\n");
for(i=0;i<n;i++)
{
scanf("%d",&height[i]);
sum = sum + height[i];
}
avg = (float)sum/n;
//Counting
for(i=0;i<n;i++)
if(height[i]>avg)
{
count++;
}
printf("\nAverage Height of %d persons is : %.2f\n",n,avg);
printf("\nThe number of persons above average : %d ",count);
getch();
}
11
SAMPLE OUTPUT:
Enter the Number of Persons: 5
Enter the Height of each person in centimeter
150
155
162
158
154
Average Height of 5 persons is: 155.8
The number of persons above average: 2
RESULT:
Thus the C Program average height of persons has been executed and the output was obtained.
12
Ex. No: 7
BODY MASS INDEX OF THE INDIVIDUALS
Date:
AIM:
To write a C Program to Populate a two dimensional array with height and weight of persons
and compute the Body Mass Index of the individuals
ALGORITHM:
Step-1: Start
Step-2: Declare variables
Step-3: Read the number of persons and their height and weight.
Step-4: Calculate BMI=W/H2 for each person
Step-5: Display the output of the BMI for each person
Step-6: Stop.
PROGRAM:
#include <stdio.h>
#define MAX_PERSONS 100
void computeBMI(float persons[MAX_PERSONS][2], float bmi[], int count)
{
for (int i = 0; i < count; i++) {
float height = persons[i][0]; // height in meters
float weight = persons[i][1]; // weight in kilograms
if (height > 0) {
bmi[i] = weight / (height * height); // BMI calculation
} else
{
bmi[i] = -1; // Invalid height
}
}
}
int main() {
int count;
float persons[MAX_PERSONS][2]; // 2D array for height and weight
float bmi[MAX_PERSONS]; // Array to store BMI values
// Compute BMI
computeBMI(persons, bmi, count);
// Output results
printf("\nBMI Results:\n");
for (int i = 0; i < count; i++) {
if (bmi[i] >= 0) {
printf("Person %d: Height = %.2f m, Weight = %.2f kg, BMI = %.2f\n", i + 1, persons[i][0],
persons[i][1], bmi[i]);
} else {
printf("Person %d: Invalid height!\n", i + 1);
}
}
return 0;
}
SAMPLE OUTPUT:
Enter the number of persons (max 100): 2
Enter height (in meters) and weight (in kilograms) for person 1: 1.28 88
Enter height (in meters) and weight (in kilograms) for person 2: 2.2 58
BMI Results:
Person 1: Height = 1.28 m, Weight = 88.00 kg, BMI = 53.71
Person 2: Height = 2.20 m, Weight = 58.00 kg, BMI = 11.98
RESULT:
Thus The C program body mass index of the individuals has been executed and the output was obtained
14
Ex. No: 8
REVERSE OF A GIVEN STRING
Date:
AIM:
To write a C Program to perform reverses without changing the position of special characters for
the given string.
ALGORITHM:
Step-1: Start
Step-2: Declare variables.
Step-3: Read a String.
Step-4: Check each character of string for alphabets or a special character by using Alpha().
Step-5: Change the position of a character vice versa if it is alphabet otherwise
remains same.
Step-6: Repeat step 4 until reach to the mid of the position of a string.
Step-7: Display the output of the reverse string without changing the position of
special characters.
Step-8: Stop
PROGRAM:
#include<stdio.h>
#include<string.h>
#include <ctype.h>
void swap(char *a, char *b)
{
char temp = *a;
*a = *b;
*b = temp;
}
void reverse(char *str)
{
char *p1 = str;
char *p2 = str + strlen(str) - 1;
while (p1 < p2) {
while (!isalpha(*p1))
p1++;
while (!isalpha(*p2))
p2--;
if (p1 < p2)
{
swap(p1, p2);
15
p1++;
p2--;
}
}
}
int main(void)
{
char str[6];
strcpy(str,"a,b$c");
reverse(str);
printf("%s\n", str);
return 0;
}
SAMPLE OUTPUT:
c,b$a
RESULT:
Thus the C Program for reverse of a given String has been executed and the output was obtained.
16
Ex. No: 9
STRING OPERATIONS USING BUILT-IN FUNCTIONS
Date:
AIM:
To write a C Program to perform string operations on a given paragraph for the following using
built-in functions:
ALGORITHM:
Step 1: Start the program.
Step 6: Find the first word of each sentence to capitalize by checks to see if a character is punctuation
mark used to denote the end of a sentence. (! . ?)
Step 7: Replace the word in the text by user specific word if match
Step 8: Display the output of the calculations.
Step 9: Repeat the step 4 till choose the option stop.
Step 10: Stop
PROGRAM
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_LENGTH 1000
int countWords(const char *str) {
int count = 0;
int inWord = 0;
while (*str)
{
if (isspace(*str))
{
inWord = 0;
17
}
else if (inWord == 0)
{
inWord = 1;
count++;
}
str++;
}
return count;
}
void capitalizeSentences(char *str)
{
int capitalize = 1;
for (int i = 0; str[i] != '\0'; i++)
{
if (capitalize && isalpha(str[i]))
{
str[i] = toupper(str[i]);
capitalize = 0;
}
if (str[i] == '.' || str[i] == '!' || str[i] == '?')
{
capitalize = 1;
}
}
}
void replaceWord(char *str, const char *oldWord, const char *newWord)
{
char buffer[MAX_LENGTH];
char *pos, *start = str;
int oldLen = strlen(oldWord);
int newLen = strlen(newWord);
int bufferIndex = 0;
while ((pos = strstr(start, oldWord)) != NULL)
{
strncpy(buffer + bufferIndex, start, pos - start);
bufferIndex += pos - start;
strcpy(buffer + bufferIndex, newWord);
bufferIndex += newLen;
start = pos + oldLen;
}
strcpy(buffer + bufferIndex, start);
strcpy(str, buffer);
18
int main()
{
char paragraph[MAX_LENGTH];
char oldWord[50], newWord[50];
printf("Enter a paragraph:\n");
fgets(paragraph, sizeof(paragraph), stdin);
paragraph[strcspn(paragraph, "\n")] = '\0';
int wordCount = countWords(paragraph);
printf("Total number of words: %d\n", wordCount);
capitalizeSentences(paragraph);
printf("Paragraph after capitalizing sentences:\n%s\n", paragraph);
printf("Enter the word to replace: ");
scanf("%s", oldWord);
printf("Enter the new word: ");
scanf("%s", newWord);
replaceWord(paragraph, oldWord, newWord);
printf("Paragraph after replacing words:\n%s\n", paragraph);
return 0;
}
SAMPLE OUTPUT:
Enter a paragraph:
Function to count the number of words in a string.flag to indicate if we are inside a word.we
are outside a word.
Total number of words: 22
Paragraph after capitalizing sentences:
Function to count the number of words in a string. Flag to indicate if we are inside a word.We are
outside a word.
Enter the word to replace: to
Enter the new word: hello
Paragraph after replacing words:
Function hello count the number of words in a string. Flag hello indicate if we are inside a word.We
are outside a word.
RESULT:
Thus a C Program String operation has been executed and the output was obtained
19
Ex. No: 10
SORTING USING PASS BY REFERENCE
Date:
AIM:
#include<stdio.h>
int main()
{
int n,a[100],i;
void sortarray(int*,int);
printf("\nEnter the Number of Elements in an array : ");
scanf("%d",&n);
printf("\nEnter theArray elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sortarray(a,n);
printf("\nAfter Sorting ... \n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
void sortarray(int* arr,int num)
{
int i,j,temp;
for(i=0;i<num;i++)
for(j=i+1;j<num;
j++)
20
if(arr[i] > arr[j])
{
temp=arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
SAMPLE OUTPUT:
Enter the Number of Elements in an array : 5
Enterthe Array elements
33
67
21
45
11
After Sorting....
11
21
33
45
67
RESULT:
Thus the C Program Sorting using pass by reference has been executed and the output was obtained.
21
Ex. No: 11(a)
Date: PROGRAMS WITH USER DEFINED FUNCTIONS AND RECURSIVE
FUNCTIONS.
AIM:
ALGORITHM:
Step 1: Start
Step 2: Read number n
Step 3: Call factorial (n)
Step 4: If n>=1 then
return n*multiply Numbers(n-1);
Step 5: Else
return 1;
Step 6: Print factorial
Step 7: stop
PROGRAM:
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n)
{if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
22
SAMPLE OUTPUT:
RESULT:
Thus the C Program with User Defined Functions and Recursive Functions for the Factorial of a
number using recursion has been executed and the output was obtained.
23
Ex. No:11 (b)
Date: FIBONACCI OF A NUMBER USING RECURSION
AIM:
ALGORITHM:
Step 1: Start
Step 2: Take an integer as an input, and will return the element at that position.
Step 3: we will have an if-else-if ladder.
Step 4: If the input will be 0 i.e., the element at the 0th position, we will return 0.
Step 5: If the input will be 1 i.e., the element at 1st position, we will return 1.
Step 6: Else, we will recursively call the fibonacci().
Step 7: stop.
PROGRAM:
#include <stdio.h>
int fibonacci(int num)
{
if (num == 0) {
return 0;
} else if (num == 1) {
return 1;
} else {
return fibonacci(num - 1) + fibonacci(num - 2);
}
}
int main()
{
int num;
printf("Enter the number of elements to be in the series: ");
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
printf("%d", fibonacci(i));
if (i < num - 1)
{
printf(", ");
}
}
printf("\n");
return 0;
}
24
SAMPLE OUTPUT:
RESULT:
Thus the C Program with User Defined Functions for Fibonacci of a number using recursion has been
executed and the output was obtained
25
Ex. No: 12
Generate salary slip of employees using structures and pointers
Date:
AIM:
To write a C Program to generate salary slip of employees usingstructures and pointers.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create a structure named Employee that contains id, name, basic salary, allowances, and
deduction.
STEP 3: Compute the total salary using the formula:
Total Salary=Basic Salary+Allowances−Deductions
int main() {
struct Employee emp;
printf("Enter Employee ID: ");
scanf("%d", &emp.id);
printf("Enter Employee Name: ");
getchar(); // to consume newline left by previous input
fgets(emp.name, sizeof(emp.name), stdin);
printf("Enter Basic Salary: ");
scanf("%f", &emp.basicSalary);
26
printf("Enter Allowances: ");
scanf("%f", &emp.allowances);
printf("Enter Deductions: ");
scanf("%f", &emp.deductions);
printSalarySlip(&emp);
return 0;
}
SAMPLE OUTPUT:
Enter Employee ID: 61
Enter Employee Name: sandhiya
Enter Basic Salary: 60000
Enter Allowances: 5000
Enter Deductions: 3000
RESULT:
Thus the C Program to generate salary slip of employees usingstructures and pointers was
successfully created and the output was obtained.
27
Ex. No: 13
INTERNAL MARKS OF STUDENTS
Date:
AIM:
To write a C Program to compute internal marks of students for five different subjects using
structures and functions.
ALGORITHM:
Step 1: Start
Step 2: Declare variables
Step 3: Read the number of students.
Step 4: Read the student mark details
Step 5: Calculate internal mark by i=total of three test marks / 3 for each subject per student.
Step 6: Display the output of the calculations for all the students.
Step 7: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct stud
{
char name[20];
long int rollno;
int marks[5,3];
int i[5];
}students[10];
void calcinternal(int);
int main()
{
int a,b,j,n;
clrscr();
printf("How many students : \n");
scanf("%d",&n);
for(a=0;a<n;++a)
{
clrscr();
printf("\n\nEnter the details of %d student : ", a+1);
printf("\n\nEnter student %d Name : ", a);
scanf("%s", students[a].name);
printf("\n\nEnter student %d Roll Number : ", a);
scanf("%ld", &students[a].rollno);
total=0;
for(b=0;b<=4;++b)
{
for(j=0;j<=2;++j)
{
28
printf("\n\nEnter the test %d mark of subject-%d : ",j+1, b+1); scanf("%d", &students[a].marks[b,j]);
}
}
}
calcinternal(n);
for(a=0;a<n;++a)
{
clrscr();
printf("\n\n\t\t\t\tMark Sheet\n");
printf("\nName of Student : %s", students[a].name);
printf("\t\t\t\t Roll No : %ld", students[a].rollno);
printf("\n ");
for(b=0;b<5;b++)
{
printf("\n\n\t Subject %d internal \t\t :\t %d", b+1, students[a].i[b]);
}
printf("\n\n \n");
getch();
}
return(0);
}
void calcinternal(int n)
{
int a,b,j,total;
for(a=1;a<=n;++a)
{
for(b=0;b<5;b++)
{
total=0;
for(j=0;j<=2;++j)
{
total += students[a].marks[b,j];
}
students[a].i[b]=total/3;
}
}
}
29
SAMPLE OUTPUT:
Mark Sheet
Name of Student: H.Xerio Roll No: 536435
Subject 1 internal : 59
Subject 2 internal : 78
Subject 3 internal : 74
Subject 4 internal : 40
ubject 5 internal : 60
RESULT:
Thus the C Program for Internal marks of students has been executed and the output was obtained
30