TSP Feb 5th2025
TSP Feb 5th2025
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;
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
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.
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
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);
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
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
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
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
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);
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
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.
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
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
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