0% found this document useful (0 votes)
22 views13 pages

TSP Feb 5th2025

The document contains various C programming exercises covering topics such as loops, functions, structures, and file handling. Each section includes a program example, its output, and explanations for tasks like calculating areas, checking prime numbers, and managing employee payroll. It serves as a practical guide for learning C programming skills through hands-on coding examples.

Uploaded by

senthilveludvm
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)
22 views13 pages

TSP Feb 5th2025

The document contains various C programming exercises covering topics such as loops, functions, structures, and file handling. Each section includes a program example, its output, and explanations for tasks like calculating areas, checking prime numbers, and managing employee payroll. It serves as a practical guide for learning C programming skills through hands-on coding examples.

Uploaded by

senthilveludvm
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/ 13

23ES1212 -TECHNICAL SKILL PRACTICES I- 4TH FEB 2025

1. WRITE A FOR LOOP TO PRINT FROM 10 TO 1.

PROGRAM
#include <stdio.h>
void main()
{
int i;
for (i = 10; i>= 1; i--)
{
printf("%d\t", i);
}
}

OUTPUT

10 9 8 7 6 5 4 3 2 1

2.
WRITE A C PROGRAM TO FIND AREA AND PERIMETER OF A CIRCLE

PROGRAM
#include <stdio.h>
#define PI 3.14f /* Define the value of pie */
void main()
{
/* Variable Declaration. */
float radius, perimeter, area;

/* Taking input of the radious of the circle from the user */


printf("Enter radius of the Circle:\n");
scanf("%f", & radius);

/* Calculating perimeter of the circle */


perimeter = 2 * PI * radius;
printf("Perimeter of the circle: %0.4f\n", perimeter);

/* Calculating area of the circle */


area = PI * radius * radius;
printf("Area of circle: %0.4f\n", area);
}

OUTPUT
Enter radius of the Circle:
5.3
Perimeter of the circle: 33.2840
Area of circle: 88.2026
3. C PROGRAM TO FIND FACTORIAL OF A NUMBER

PROGRAM
#include <stdio.h>
void main()
{
int num, i,fact = 1;
printf("Enter a number: ");
scanf("%d", &num);

// Calculate factorial
for (i = 1; i<= num; i++)
{
fact =fact* i;
}
printf("Factorial of %d is %d\n", num, fact);
}

OUTPUT
Enter a number: 5
Factorial of 5 is 120

4. C PROGRAM TO CHECK WHETHER A NUMBER IS PRIME OR NOT.

PROGRAM
#include <stdio.h>
void main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; ++i)
{
if (n % i == 0)
{
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
OUTPUT
Enter a positive integer: 50
50 is not a prime number
Enter a positive integer: 1
11 is a prime number.

5 PROGRAM TO CONVERT THE TEMPERATURE GIVEN IN FAHRENHEIT TO


CELSIUS AND VICE VERSA USING THE FOLLOWING FORMULA:

celsius = (fahrenheit - 32) * 5 / 9;


fahrenheit = (celsius * 9 / 5) + 32;

PROGRAM
#include <stdio.h>
void main()
{
int choice;
float temperature, converted;

printf("Temperature Conversion:\n");
printf("1. Celsius to Fahrenheit\n");
printf("2. Fahrenheit to Celsius\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &choice);
if (choice == 1)
{
printf("Enter temperature in Celsius: ");
scanf("%f", &temperature);
converted = (temperature * 9.0 / 5.0) + 32;
printf("Temperature in Fahrenheit: %.2f\n", converted);
}

else if (choice == 2)
{
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &temperature);
converted = (temperature - 32) * 5.0 / 9.0;
printf("Temperature in Celsius: %.2f\n", converted);
}

else
printf("Invalid choice.\n");
}

OUTPUT
Temperature Conversion:
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Enter your choice (1 or 2): 1
Enter temperature in Celsius: 37
Temperature in Fahrenheit: 98.60
C PROGRAM TO COPY ALL ELEMENTS OF AN ARRAY INTO ANOTHER ARRAY.

6. PROGRAM
#include <stdio.h>
void main()
{
int n,i,s[10],d[10];
// Input the size of the array
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements of the source array:\n");
for (i = 0; i< n; i++)
{
scanf("%d",&s[i]);
}
for (i = 0; i< n; i++)
{
d[i] = s[i];
}
printf("Elements of the destination array:\n");
for (i = 0; i< n; i++)
{
printf("%d ", d[i]);
}
}

OUTPUT

Enter the number of elements: 5


Enter the elements of the source array:
66
55
44
33
22
Elements of the destination array:
66 55 44 33 22

7. PROGRAM TO SORT GIVEN N NUMBERS USING FUNCTIONS.

PROGRAM
#include <stdio.h>
// Function to perform Selection Sort
void selectionSort(int arr[], int n)
{
int i, j, minIndex, temp;
// Traverse through all array elements
for (i = 0; i< n - 1; i++)
{
// Find the minimum element in unsorted part
minIndex = i;
for (j = i + 1; j < n; j++)
{
if (arr[j] <arr[minIndex])
minIndex = j;

}
// Swap the found minimum element with the first element
if (minIndex != i)
{
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
} //close outer for loop
} //close selectionsort()

void main()
{
int n,i,arr[20];
printf("Enter the number of elements: ");
scanf("%d", &n);

// Input the elements of the array


printf("Enter the elements: ");
for (i = 0; i< n; i++)
{
scanf("%d", &arr[i]);
}

// Perform Selection Sort


selectionSort(arr, n);
// Print the sorted array
printf("Sorted array: ");
for (i = 0; i< n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
OUTPUT
Enter the number of elements: 5
Enter the elements: 99
77
88
44
1
Sorted array: 1 44 77 88 99
8. WRITE A C PROGRAM USING POINTERS TO COMPUTE THE SUM AND MEAN OF
ALL ELEMENTS STORED IN AN ARRAY OF N REAL NUMBERS.

PROGRAM
#include <stdio.h>
#include <math.h>
void main()
{
int n,i;
float arr[20], sum = 0.0, mean, sd = 0.0, *ptr;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements: \n");
for ( i = 0; i< n; i++)
{
scanf("%f", &arr[i]);
}
// Pointer to traverse the array
ptr = arr;
// Calculate the sum
for (i = 0; i< n; i++)
{
sum += *(ptr + i);
}
mean = sum / n;
// Print results
printf("Sum: %.2f\n", sum);
printf("Mean: %.2f\n", mean);

OUTPUT
Enter the number of elements: 5
Enter the elements:
89.7
67.8
55.6
45.7
34.7
Sum: 293.50
Mean: 58.70

9. C PROGRAM USING STRUCTURES TO PREPARE STUDENTS MARK STATEMENT.

PROGRAM
#include <stdio.h>
struct Student
{
int rollNo;
char name[50];
int mark1;
int mark2;
int mark3;
int totalMarks;
float average;
};

int main()
{
struct Student student;
// Input student details
printf("Enter Roll Number: ");
scanf("%d", &student.rollNo);
printf("Enter Name: ");
scanf(" %s", student.name);
// Input marks for 3 subjects
printf("Enter marks for Subject 1: ");
scanf("%d", &student.mark1);
printf("Enter marks for Subject 2: ");
scanf("%d", &student.mark2);
printf("Enter marks for Subject 3: ");
scanf("%d", &student.mark3);
// Calculate total and average
student.totalMarks = student.mark1 + student.mark2 + student.mark3;
student.average = student.totalMarks / 3.0;
// Display mark statement
printf("\n--- Mark Statement ---\n");
printf("Roll Number: %d\n", student.rollNo);
printf("Name: %s\n", student.name);
printf("Marks:\n");
printf(" Subject 1: %d\n", student.mark1);
printf(" Subject 2: %d\n", student.mark2);
printf(" Subject 3: %d\n", student.mark3);
printf("Total Marks: %d\n", student.totalMarks);
printf("Average Marks: %.2f\n", student.average);
}
OUTPUT
Enter Roll Number: 101
Enter Name: SIVASHANKAR
Enter marks for Subject 1: 99
Enter marks for Subject 2: 78
Enter marks for Subject 3: 89

--- Mark Statement ---


Roll Number: 101
Name: SIVASHANKAR
Marks:
Subject 1: 99
Subject 2: 78
Subject 3: 89
Total Marks: 266
Average Marks: 88.67
10. WRITE AN EXAMPLE PROGRAM FOR UNION IN C

PROGRAM
#include <stdio.h>
// Define a union to hold different types of student data
union Student {
int rollNo;
float marks;
char grade;
};
int main() {
union Student student;
// Input roll number
printf("Enter Roll Number: ");
scanf("%d", &student.rollNo);
printf("Roll Number: %d\n", student.rollNo);

// Input marks
printf("Enter Marks: ");
scanf("%f", &student.marks);
printf("Marks: %.2f\n", student.marks);
// Input grade
printf("Enter Grade: ");
scanf(" %c", &student.grade);
printf("Grade: %c\n", student.grade);
return 0;
}

OUTPUT
Enter Roll Number: 101
Roll Number: 101
Enter Marks: 89.7
Marks: 89.70
Enter Grade: A
Grade: A

11. C PROGRAM TO FIND AVERAGE OF NUMBERS STORED IN SEQUENTIAL


MANNER.

Step 1 : open a turboc editor, store the following numbers and save the file as numbers.txt

numbers.txt
10.5
20.8
30.2
15.0
25.7
Step 2: open turboc editor, type the following program and save it as seqfile.c

seqfile.c
#include <stdio.h>
void main()
{
FILE *file;
float num, sum = 0.0;
int count = 0;
// Open the file in read mode
file = fopen("numbers.txt", "r");
if (file == NULL) {
printf("Could not open file.\n");
return 1;
}
// Read numbers from the file and calculate sum and count
while (fscanf(file, "%f", &num) != EOF)
{
sum += num;
count++;
}
// Check if any numbers were read
if (count == 0) {
printf("No numbers to calculate average.\n");
}
else {
float average = sum / count;
printf("Average of numbers: %.2f\n", average);
}
fclose(file);
}

Step 3: now run the file seqfile.c you will get following output
Average of numbers:20.4

12. C PROGRAM TO DISPLAY THE CONTENTS OF THE FILE IN REVERSE ORDER

Step 1 : open a new file in turboc editor, type the following string and save the file as example.txt

example.txt
Welcome to Panimalar engineering college

Step 2: open a new file in turboc editor, type the following contents and save the file as random.c

random.c
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *file;
char ch;
int i,size;
// Open the file in read mode
file = fopen("example.txt", "r");
if (file == NULL)
{
printf("Could not open the file.\n");
return 1;
}
// Move to the end of the file
fseek(file, 0, SEEK_END);

// Get the size of the file


size = ftell(file);

// Read and display the contents in reverse order


for (i = size - 1; i>= 0; i--)
{
fseek(file, i, SEEK_SET); //read from end of the file
ch = fgetc(file);
putchar(ch);
}
fclose(file);
}

Step 3: now run random.c and you can see output

egelloc gnireenigne ralaminaP ot emocleW

13. C PROGRAM TO COPY THE CONTENTS FROM ONE FILE TO ANOTHER FILE

Step 1 : open a new file in turboc editor, type the following contents and save the file as source.txt

Welcome to Panimalar engineering college

Step 2 : open a new file in turboc editor, type the following contents and save the file as copy.c

copy.c
#include <stdio.h>
int main()
{
FILE *sourceFile, *destinationFile;
char ch;
// Open the source file in read mode
sourceFile = fopen("source.txt", "r");
if (sourceFile == NULL) {
printf("Source file could not be opened.\n");
return 1;
}
// Open the destination file in write mode
destinationFile = fopen("destination.txt", "w");
if (destinationFile == NULL) {
printf("Destination file could not be opened.\n");
fclose(sourceFile); // Close the source file before exiting
return 1;
}
// Copy contents from source to destination
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, destinationFile);
}
printf("File copied successfully.\n");
fclose(sourceFile);
fclose(destinationFile);
return 0;
}

Step 3 : now run file copy.c. You can see the output
File copied successfully.

Step 4 :Now open File menu. Type destination.txt and now you can find the following contents.

Welcome to Panimalar engineering college

14. C PROGRAM USING STRUCTURES TO PREPARE THE EMPLOYEE PAY ROLL OF A


COMPANY.

Formula npay = basicpay +allowances - deduction ;

PROGRAM
#include<stdio.h>
#include<conio.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, allow, ded, npay ;
} e[10] ;
void main()
{
int i, n ;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
scanf("%d", &e[i].empno) ;
printf("\nEnter the name : ") ;
scanf("%s", e[i].name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &e[i].bpay, &e[i].allow, &e[i].ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", e[i].empno,
e[i].name, e[i].bpay, e[i].allow, e[i].ded, e[i].npay) ;
}
}

OUTPUT
Enter the number of employees : 3
Enter the employee number : 101
Enter the name : SIVA
Enter the basic pay, allowances & deductions : 100000 10000 15000
Enter the employee number : 102

Enter the name : SHANKAR


Enter the basic pay, allowances & deductions : 150000 15000 18000
Enter the employee number : 103

Enter the name : SHARMILA


Enter the basic pay, allowances & deductions : 200000 20000 11000

Emp. No. Name Bpay Allow Ded Npay

101 SIVA 100000 10000 5000 105000


102 THARUN 150000 15000 8000 157000
103 SHARMILA 200000 20000 11000 1209000

WRITE A PSEUDO CODE AND A C PROGRAM TO CHECK WHETHER A GIVEN


15. NUMBER IS ARMSTRONG NUMBER OR NOT USING COMMAND LINE ARGUMENT.

Step 1: open turboc editor, create a new file and save it as commandline.c
//commandline.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main(int argc, char *argv[])
{
int num = atoi(argv[1]), originalNum = num, sum = 0, digit, n = 0;
if (argc != 2)
{
printf("Enter a number as a command line argument\n");
}
// Find the number of digits in the number
while (num != 0)
{
num /= 10;
n++;
}
num = originalNum;
// Calculate the sum of digits raised to the power of the number of digits
while (num != 0)
{
digit = num % 10;
sum += pow(digit, n);
num /= 10;
}
if (sum == originalNum)
printf("%d is an Armstrong number.\n", originalNum);
else
printf("%d is not an Armstrong number.\n", originalNum);
}

Step 2 : Now run the source file. Ensure that the commandline.c doesnot have any errors.

Step 3 : open the DOS shell in File menu of the turboc editor. You can find path C:\tc\bin
directory. Type c:\tc\bin\ commandline 153

Output - 153 is an Armstrong number

Step 3: open the DOS shell in File menu of the turboc editor, and type
type c:\tc\bin\ commandline 11
output - 11 is not an Armstrong number

You might also like