PWC Final Journal
PWC Final Journal
Practical No 1:
Practical A:
Aim: Write a program to find the addition, subtraction, multiplication and division of two
numbers.
Code:
int sum(int x, int y) {
return x + y;
}
void main() {
int a, b, ans, choice = 0;
do {
printf(" Basic Calculator \n");
printf(" \n");
printf("Choose an operation:\n");
printf("1.ADD\n2.SUBTRACT\n3.MULTIPLICATION\n4.DIVISION\n5.EXIT\n");
printf("Enter your choice as (1-5)\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter two integers:\n");
scanf("%d%d", &a, &b);
ans = sum(a, b);
printf("The answer is %d\n", ans);
break;
case 2:
printf("Enter two integers:\n");
scanf("%d%d", &a, &b);
ans = diff(a, b);
printf("The answer is %d\n", ans);
break;
case 3:
printf("Enter two integers:\n");
scanf("%d%d", &a, &b);
ans = product(a, b);
printf("The answer is %d\n", ans);
break;
case 4:
printf("Enter Dividend:\n");
scanf("%d", &a);
printf("Enter Divisor:\n");
scanf("%d", &b);
if (b != 0) {
ans = quotient(a, b);
printf("The answer is %d\n", ans);
} else {
printf("Division by zero is not allowed.\n");
}
break;
case 5:
printf("Thank You\n");
break;
default:
printf("Enter a valid choice\n");
}
} while (choice != 5);
}
Output
Practical B:
Aim: Write a program to find area of rectangle, square and circle.
Code:
float areaofsquare(float x) {
return x * x;
}
float areaofcircle(float x) {
return 3.14159 * x * x;
}
void main() {
int choice = 0;
float l, b, r, area;
do {
printf(" Area Calculator \n");
printf(" \n");
printf("Choose an operation:\n");
printf("1. Area of Square\n2. Area of Circle\n");
printf("3. Area of Rectangle\n4. EXIT\n");
printf("Enter your choice (1-4):\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter length of side of square:\n");
scanf("%f", &l);
area = areaofsquare(l);
printf("The area of the square is %f\n", area);
break;
case 2:
printf("Enter radius of circle:\n");
scanf("%f", &r);
area = areaofcircle(r);
printf("The area of the circle is %f\n", area);
break;
case 3:
printf("Enter length and breadth of rectangle:\n");
scanf("%f%f", &l, &b);
area = areaofrectangle(l, b);
printf("The area of the rectangle is %f\n", area);
break;
case 4:
printf("Thank You\n");
break;
default:
printf("Enter a valid choice\n");
}
} while (choice != 4);
}
Output:
Practical C:
Aim: Write a program to find volume of cube, sphere and cylinder.
Code:
float volumeofcube(float x) {
return x * x * x;
}
float volumeofsphere(float x) {
return (4.0 / 3.0) * 3.14159 * x * x * x;
}
void main() {
int choice = 0;
float l, h, r, vol;
do {
printf(" Volume Calculator \n");
printf(" \n");
printf("Choose an operation:\n");
printf("1. Volume of Cube\n2. Volume of Sphere\n");
printf("3. Volume of Cylinder\n4. EXIT\n");
printf("Enter your choice (1-4):\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter length of side of cube:\n");
scanf("%f", &l);
vol = volumeofcube(l);
printf("The volume of the cube is %f\n", vol);
break;
case 2:
printf("Enter radius of sphere:\n");
scanf("%f", &r);
vol = volumeofsphere(r);
printf("The volume of the sphere is %f\n", vol);
break;
case 3:
printf("Enter height of the cylinder:\n");
scanf("%f", &h);
printf("Enter radius of cylinder:\n");
scanf("%f", &r);
vol = volumeofcylinder(r, h);
printf("The volume of the cylinder is %f\n", vol);
break;
case 4:
printf("Thank You\n");
break;
default:
printf("Enter a valid choice\n");
}
} while (choice != 4);
}
Output:
PROGRAMMING WITH C:
Practical No 2:
A) Write c program for student grade calculator. Take Name, roll no and marks of three
subjects (Math, science and English) from user. Display total marks, percentage and Grade.
(Percentage >=90 A Grade, >=80 B Grade, >=70 C grade, >=60 D grade Else F grade).
Code:
#include <stdio.h>
void main()
int roll;
float percentage, m1, m2, m3, total;
scanf("%d", &roll);
scanf("%f", &m2);
scanf("%f", &m3);
total = m1 + m2 + m3;
grade = 'A';
grade = 'B';
}
else if (percentage >= 70.00)
grade = 'C';
grade = 'D';
else
grade = 'F';
Output:
B) Write C program to perform string manipulation operation like find length, concatenation,
comparison, copy and reverse. Take required string from user.
Code:
void main()
int choice=0,n;
char str1[30],str2[30];
do{
printf("\nChoose an operation:\n");
printf("1.Length 2.Concatenation 3.Comparison 4.Copy 5.Reverse 6.Exit\n");
scanf("%d",&choice);
switch (choice)
case 1:
printf("Enter String:\n");
scanf("%s",&str1);
n=strlen(str1);
printf("%d\n",n);
break;
case 2:
printf("Enter first string :\n");
scanf("%s",&str1);
printf("Enter second string :\n");
scanf("%s",&str2);
printf(strcat(str1,str2));
break;
case 3:
scanf("%s",&str1);
printf("Enter second string :\n");
scanf("%s",&str2);
n=strcmp(str1,str2);
if (n==0)
else
case 4:
printf("Enter String:\n");
scanf("%s",&str1);
strcpy(str2,str1);
break;
case 5:
printf("Enter String:\n");
scanf("%s",&str1);
printf(strrev(str1),"\n");
break;
case 6:
printf("Thank You\n");
break;
default:
};
while (choice!=6);
Output:
C) Write a C program to check if a given string is palindrome or not.
Code:
void main()
char str1[50],rstr1[50];
int n;
n=1;
scanf("%s",&str1);
strcpy(rstr1,str1);
strrev(rstr1);
n=strcmp(str1,rstr1);
if (n==0)
else
}
}
Output:
Code:
void main()
{
char str1[50];
int count[100],i,j,len;
scanf("%s",str1);
len=strlen(str1);
for (i=0;i<=(len-1);i++)
{
if (str1[i]==' ')
continue;
count[i]=0;
for (j=0;j<=i;j++)
if(str1[j]==str1[i])
count[j]+=1;
break;
printf("Frequency :\n");
for(i=0;i<=(len-1);i++)
{
if (count[i]==0 || str1[i]==' ')
continue;
}
else
printf("%c : %d\n",str1[i],count[i]);
}
Output:
PROGRAMMING WITH C:
Practical No 3:
Practical A:
Aim: Write a program to check whether the number is Even or Odd.
Code:
#include <stdio.h>
void main() {
int num;
if (num % 2 == 0) {
} else {
Output:
Practical B:
Aim: Write a program to check whether the number is Positive, Negative or Zero.
Code:
#include <stdio.h>
void main() {
int num;
if (num > 0) {
} else {
Output:
Practical C:
Aim: Write a program to find the sum of squares of digits of a number.
Code:
#include <stdio.h>
void main() {
scanf("%d", &num);
while (num != 0) {
}
printf("Sum of squares of digits: %d\n", sum);
Output:
Practical D:
Aim: Write a program to reverse the digits of an integer.
Code:
#include <stdio.h>
void main() {
while (num != 0) {
}
printf("Reversed number: %d\n", reversed);
Output:
PROGRAMMING WITH C:
Practical No 4:
Practical A:
Aim: Write a C program to calculate the Body Mass Index (BMI) of a person based on their
weight and height, and determine their BMI category according to the following criteria:
BMI < 18.5 Underweight; 18.5 ≤ BMI < 24.9 Normal weight; 25 ≤ BMI < 29.9 Overweight
BMI ≥ 30 Obese using function.
Code:
#include <stdio.h>
void main() {
scanf("%f", &height);
scanf("%f", &weight);
bmi = calculateBMI(weight, height);
determineBMICategory(bmi);
}
Output:
PROGRAMMING WITH C:
Practical No 5:
Practical A:
Aim: Write a program to find the factorial of a number using recursive function.
Code:
#include <stdio.h>
long factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
void main() {
int num;
scanf("%d", &num);
if (num < 0) {
} else {
Output:
Practical B:
Aim: Write a program to find the sum of natural numbers using recursive function.
Code:
#include <stdio.h>
int sumOfNaturalNumbers(int n) {
if (n == 0)
return 0;
else
void main() {
int num;
if (num < 0) {
Output:
PROGRAMMING WITH C:
Practical No 6:
Practical A:
Aim: Find Largest Value Store in Array.
Code:
#include <stdio.h>
int main()
{
int n, i, max;
int arr[n];
scanf("%d", &arr[i]);
}
max = arr[0];
max = arr[i];
}
}
return 0;
}
Output:
Practical B:
Aim: Write a program using pointer to compute the sum of all elements stored in array.
Code:
#include <stdio.h>
int main()
int n, i, sum = 0;
int *ptr;
ptr = arr;
scanf("%d", &arr[i]);
sum += *ptr;
ptr++;
}
printf("Sum is: %d", sum);
return 0;
}
Output:
Practical C:
Code:
#include <stdio.h>
{
int i, j, temp;
temp = arr[j];
arr[j + 1] = temp;
}
}
}
}
{
int i, j, temp;
temp = arr[j];
arr[j + 1] = temp;
}
}
void printArray(int arr[], int n)
printf("\n\n");
}
int main() {
int n, i;
printf("Name: Karthik Vinod, Roll No. FCS2425041\n");
scanf("%d", &n);
int arr[n];
for(i = 0; i<n; i++)
{
scanf("%d", &arr[i]);
}
printArray(arr, n);
sortAscending(arr, n);
printf("Sorted in Ascending Order: ");
printArray(arr, n);
sortDescending(arr, n);
printArray(arr, n);
return 0;
}
Output:
Practical D:
Aim: Display result of 5 students using array.
Code:
#include <stdio.h>
void main()
{
int n;
scanf("%s", name[i]);
scanf("%d", &roll[i]);
scanf("%d", &phy[i]);
scanf("%d", &chem[i]);
scanf("%d", &bio[i]);
printf("Enter CS: ");
scanf("%d", &cs[i]);
calculate = 0;
percentage[i] = (float)calculate/500*100;
if (percentage[i] >= 90)
grade[i] = 'A';
}
grade[i] = 'B';
grade[i] = 'C';
grade[i] = 'D';
else {
grade[i] = 'F';
}
}
for (i = 0; i<n; i++)
printf("Physcis: %d\t Chemistry: %d\t Biology: %d\t CS: %d\t Maths: %d\n", phy[i],
chem[i], bio[i], cs[i], maths[i]);
}
}
Output:
PROGRAMMING WITH C:
Practical No 7:
Practical A:
Aim: Swap two number using pointer.
Code:
#include <stdio.h>
#include <stdlib.h>
int temp=*a;
*a=*b;
*b=temp;
int main()
scanf("%d",&num1);
scanf("%d",&num2);
swap(&num1, &num2);
Output:
Practical B:
Code:
// Copy string and find length using pointer
#include <stdio.h>
#include <stdlib.h>
while(*src!='\0')
*dest=*src;
src++;
dest++;
*dest='\0';
int length=0;
while(*str!='\0')
{
length++;
str++;
}
return length;
int main()
char secondString[20];
copyString(&firstString,&secondString);
return 0;
}
Output:
Practical C:
Aim: Perform addition and subtraction of two pointer variable.
Code:
//Perform addition and subtraction of two pointer variable
#include <stdio.h>
#include <stdlib.h>
return(*x+*y);
}
return(*x-*y);
int main()
int num1,num2;
scanf("%d",&num1);
scanf("%d",&num2);
printf("Sum: %d\n",intAddition(&num1,&num2));
printf("Difference: %d\n",intSubtraction(&num1,&num2));
return 0;
Output:
PROGRAMMING WITH C:
Practical No 8:
Practical A:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
struct Student {
char name[50];
int age;
float grade;
};
int main()
printf("%s\n", s1.name);
printf("%d\n", s1.age);
printf("%.2f\n", s1.grade);
return 0;
}
Output:
Practical B:
Aim: Program on structure.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Bank
int acc_no;
char name[30];
float balance;
};
x.balance += num;
return x;
{
if (x.balance < num)
printf("Insufficient Funds\n");
}
else
x.balance -= num;
return x;
int main()
int choice = 0;
float amt;
scanf("%s", acc1.name);
while (choice != 4)
{
printf("\nWhat would you like to do?");
scanf("%d", &choice);
switch (choice)
case 1:
check_balance(acc1);
break;
case 2:
printf("Enter amount you want to deposit: ");
scanf("%f", &amt);
case 3:
printf("Enter amount you want to withdraw: ");
scanf("%f", &amt);
default:
printf("Invalid option selected. Please try again.\n");
}
return 0;
Output:
PROGRAMMING WITH C:
Practical No 9:
Practical A:
Aim: Write a C program on union.
Code:
#include <stdio.h>
#include <stdlib.h>
union student {
int rollno;
float percentage;
char name[30];
};
void main()
s1.rollno = 225;
printf("Roll no: %d\n", s1.rollno);
s1.percentage = 80.0;
strcpy(s1.name, "Karthik");
printf("Name: %s\n", s1.name);
Output:
Practical B:
Code:
#include <stdio.h>
#include <string.h>
union studentData {
int id;
float percentage;
char name[30];
};
struct student {
};
int main()
{
s1.info.id = 101;
printf("Student ID: %d\n", s1.info.id);
s1.info.percentage = 85.5;
strcpy(s1.info.name, "Karthik");
printf("Name: %s\n", s1.info.name);
return 0;
}
Output:
Practical C:
Aim: Write a C program on union using pointer.
Code:
#include <stdio.h>
#include <string.h>
union student {
int id;
float percentage;
char name[30];
};
int main()
ptr = &s1;
ptr->id = 101;
printf("Student ID: %d\n", ptr->id);
ptr->percentage = 85.5;
printf("Percentage: %.2f\n", ptr->percentage);
strcpy(ptr->name, "Karthik");
printf("Name: %s\n", ptr->name);
return 0;
Output:
Practical D:
Aim: Write a C program on union using user define function.
Code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
union Student
{
int rollno;
float percentage;
char name[30];
};
void printStudent(union Student s1)
int main()
s1.rollno= 101 ;
s1.percentage= 85.50 ;
strcpy(s1.name,"Karthik");
printStudent(s1);
return 0;
Output:
PROGRAMMING WITH C:
Practical No 10:
Practical 10A:
Aim: Create a file read and close it.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
char line[100];
fp=fopen("example.txt","r");
if (fp==NULL)
{
fclose(fp);
return 0;
}
else
while(fgets(line,100,fp))
printf("%s",line);
}
printf("\nClosing File");
fclose(fp);
return 0;
Output:
Practical 10B:
Aim: Write a user friendly program to read and write the file.
Code:
#include <stdio.h>
#include <stdlib.h>
void readFile()
FILE* fp;
char line[100];
fp=fopen("ReadWrite.txt","r");
if (fp==NULL)
{
printf("Error in opening file .\n");
fclose(fp);
return;
else
while(fgets(line,100,fp))
printf("%s",line);
printf("\nClosing File");
fclose(fp);
}
void writeFile()
FILE* fp;
fp=fopen("ReadWrite.txt","w");
if (fp==NULL)
fclose(fp);
return;
else
printf("\nClosing File");
fclose(fp);
int main()
int choice,e=1;
while(e==1)
switch(choice)
{
case 1:
readFile();
break;
case 2:
writeFile();
break;
case 3:
printf("Program Closing. .....\n");
e=0;
break;
default:
printf("Invalid choice");
}
}
return 0;
Output:
Practical 10C:
Aim: To implement a C program that reads an integer from a file (input.txt), calculates its
factorial using recursion, and writes the result to another file (output.txt).
Code:
#include <stdio.h>
#include <stdlib.h>
int factorial(int n)
{
if (n == 0 || n == 1)
return 1;
}
int main()
{
FILE *fp;
if (fp == NULL)
return 1;
}
fclose(fp);
fact = factorial(num);
fp = fopen("output.txt", "w");
if (fp == NULL)
return 1;
}
fclose(fp);
return 0;
}
Output: