lab-manual-c-programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

lOMoARcPSD|37497581

LAB Manual C Programming

Bachelor of Computer Applications (I. K. Gujral Punjab Technical University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)
lOMoARcPSD|37497581

LAB MANUAL
PROBLEM SOLVING C PROGRAMMING

Department of Computer Applications

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

HISTORY OF C

The milestones in C's development as a language are listed below: 1. BCPL - a user
friendly OS providing powerful development tools developed from BCPL c. 1967.
Assembler was tedious, long and error prone. 2. A new language “B” was attempted c.
1970 by Ken Thompson at Bell Labs. 3. UNIX was developed c. 1970 using “B” - DEC
PDP-7 Assembly Language. 4. A totally new language “C”, a successor to “B” was
developed c. 1971. 5. By 1973, UNIX OS was almost totally written in “C”.

Hardware Requirement: Desktop Computer / laptop computer. Software Requirement:


Linux Operating System with GCC / TURBO C in WINDOWS OS / TURBO C++ in WINDOWS
OS.

Turbo C/C++

Open Turbo C/C++ from your Desktop or Programs menu. Select “File” from Menu bar
and select option “New” and Save C program with filename „.C‟ extension. To do
compiling – Select -> Compile from menu and click-> compile. If the compilation is
successful – you will see a “success” message. Else you will see the number of errors. To
RUN the program – you may select ->Run from menu and click -> Run Now you will see
the output screen.

STRUCTURE OF „C‟ PROGRAM :

C program may contain one or more sections as shown below:


DOCUMENTATION SECTION
LINK SECTION
DEFINITION SECTION
GLOBAL DECLARATION SECTION
Main()
Function section
{
Declaration part Executable part
}
SUBPROGRAM SECTION

User defined functions

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

S. NO. EXPERIMENT PAGE NO.


1. WRITE A PROGRAM to display your name. Write another program to print message
with inputted name
2. WRITE A PROGRAM to add two numbers.
3. WRITE A PROGRAM to find the square of a given number.
4. WRITE A PROGRAM to calculate the average of three real numbers.
5. Write a program to Find ASCII Value of a Character
6. WRITE A PROGRAM to Find the Size of int, float, double and char
7. WRITE A PROGRAM to Compute Quotient and Remainder
8. WRITE A PROGRAM to accept the values of two variables.
9. WRITE A PROGRAM to find the simple interest, inputs are amount, period in years and
rate of interest
10. Basic salary of an employee is input through the keyboard. The DA is 25% of the basic
salary while the HRA is 15% of the basic salary. Provident Fund is deducted at the rate
of 10% of the gross salary(BS+DA+HRA). WRITE A PROGRAM to calculate the net
salary
11. WRITE A PROGRAM to find area of a circle using PI as constant
12. WRITE A PROGRAM to find volume of a cube using side as input from user
13. WRITE A PROGRAM using various unformatted Input Functions
14 WRITE A PROGRAM to find area of rectangle and print the result using unformatted
output Functions
15. WRITE A PROGRAM to find the larger of two numbers.
16. WRITE A PROGRAM to find greater of three numbers using Nested If
17. WRITE A PROGRAM to find whether the given number is even or odd.
18. WRITE A PROGRAM to Generate Multiplication Table Using for loop
19. WRITE A PROGRAM to Generate Multiplication Table Using while loop
20. WRITE A PROGRAM to Make a Simple Calculator Using switch...case
21. WRITE A PROGRAM to find whether the given number is a prime number.
22. WRITE A PROGRAM using function to find the largest of three numbers.
23. WRITE A PROGRAM using function to print first 20 numbers and its squares.
24. WRITE A PROGRAM to find the factorial of a given number
25. WRITE A PROGRAM to print the sum of two matrices
26. WRITE A PROGRAM to Find the Length of a String
27. WRITE A PROGRAM to Copy String using strcpy()
28. WRITE A PROGRAM to compare a string
29. WRITE A PROGRAM to reverse a string
30. WRITE A PROGRAM to reverse a string
31. WRITE A PROGRAM to multiply two numbers using pointers.
32. WRITE A PROGRAM to display address of variable using pointers
33. WRITE A PROGRAM to show the memory occupied by Structure and Union
34. WRITE A PROGRAM to create Student I-Card using a Structure
35. WRITE A PROGRAM to read data from a file from a file
36. WRITE A PROGRAM to save Employee details in a file using File Handling

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

1. WRITE A PROGRAM to display your name.

#include <stdio.h>

int main()
{
// print name
printf("Name : ssicmit");
return 0;
}

Output
Name : ssicmit
In this example, we use scanf() to accept the name as input from the user and
then print it.

#include <stdio.h>

int main()
{
char name[20];
printf("Enter name: ");

// user input will be taken here


scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Output:

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

2. WRITE A PROGRAM to add two numbers.

#include <stdio.h>

int main()
{
int A, B, sum = 0;

// Ask user to enter the two numbers


printf("Enter two numbers A and B : \n");

// Read two numbers from the user || A = 2, B = 3


scanf("%d%d", &A, &B);

// Calculate the addition of A and B


// using '+' operator
sum = A + B;

// Print the sum


printf("Sum of A and B is: %d", sum);

return 0;
}

Output:
Enter two numbers A and B :
21
22
Sum of A and B is: 43

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

3. WRITE A PROGRAM to find the square of a given number.

#include<stdio.h>
int main()
{
float number, square;
printf("Please Enter any integer Value : ");
scanf("%f", &number);
square = number * number;
printf("square of a given number %.2f is = %.2f", number, square);
return 0;
}

Output:
Please Enter any integer Value : 3
square of a given number 3.00 is = 9.00

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

4. WRITE A PROGRAM to calculate the average of three real numbers.

#include <stdio.h>
int main()
{
int a,b,c;
float avg;
printf("\tEnter Three Numbers\n");
printf("--------------------------------\n");
printf("Enter First Number : ");
scanf("%d", &a);
printf("\nEnter Second Number : ");
scanf("%d",&b);
printf("\nEnter Third Number : ");
scanf("%d",&c);
printf("--------------------------------\n");
/* To find average*/
avg=a+b+c/3.0;
printf("\nAverage of Three Numbers : %f",avg);
return 0;

Output:

Enter Three Numbers


--------------------------------
Enter First Number : 23
Enter Second Number : 43
Enter Third Number : 54
--------------------------------

Average of Three Numbers : 84.000000

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

5. Write a program to Find ASCII Value of a Character.

#include <stdio.h>
int main()
{
char ch; // variable declaration
printf("Enter a character");
scanf("%c",&ch); // user input
printf("\n The ascii value of the ch variable is : %d", ch);
return 0;
}

Output:
Enter a charactera
The ascii value of the ch variable is : 97

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

6. WRITE A PROGRAM to Find the Size of int, float, double and char.

include <stdio.h>
int main()
{
int integerType;
char charType;
float floatType;
double doubleType;
// Calculate and Print
// the size of integer type
printf("Size of int is: %ld", sizeof(integerType));

// Calculate and Print


// the size of charType
printf("Size of char is: %ld", sizeof(charType));

// Calculate and Print


// the size of floatType
printf("Size of float is: %ld", sizeof(floatType));

// Calculate and Print


// the size of doubleType
printf("Size of double is: %ld", sizeof(doubleType));
return 0;
}
Output:
Size of int is: 4
Size of char is: 1
Size of float is: 4
Size of double is: 8

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

7. WRITE A PROGRAM to Compute Quotient and Remainder

include <stdio.h>
// Driver code
int main()
{
int A, B, quotient = 0, remainder = 0;

// Ask user to enter the two numbers


printf("Enter two numbers A and B : \n");

// Read two numbers from the user || A = 17, B = 5


scanf("%d%d", &A, &B);

// Calculate the quotient of A and B using '/' operator


quotient = A / B;

// Calculate the remainder of A and B using '%' operator


remainder = A % B;

// Print the result


printf("Quotient when A/B is: %d\n", quotient);
printf("Remainder when A/B is: %d", remainder);

return 0;
}

Output:
Enter two numbers A and B: Quotient when A / B is: 3
Remainder when A / B is: 2

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

8. WRITE A PROGRAM to accept the values of two variables.

include <stdio.h>
int main() {
int n1;
int n2;
printf("Enter the number 1:\n");
scanf("%d", &n1);

printf("Enter the number 2:\n");


scanf("%d", &n2);

printf("Number 1: %d\n", n1);

printf("Number 2: %d\n", n2);

return 0;

Output
Enter the number1: 3
Enter the number2: 55

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

9. WRITE A PROGRAM to find the simple interest, inputs are amount, period in
years and rate of interest.

// C program to find the simple interst


#include <stdio.h>

// Driver code
int main()
{
// We can change values here for
// different inputs
float P , R , T;
printf("Enter p,r, and t values \n");
scanf("%f %f %f",&P,&R,&T);
// Calculate simple interest
float SI = (P * T * R) / 100;

// Print Simple Interest


printf("Simple Interest = %f\n", SI);

return 0;
}

Output:
Enter p,r, and t values
2300
20
2
Simple Interest = 920.000000

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

10. Basic salary of an employee is input through the keyboard. The DA is


25% of the basic salary while the HRA is 15% of the basic salary.
Provident Fund is deducted at the rate of 10% of the gross
salary(BS+DA+HRA). WRITE A PROGRAM to calculate the net salary.

#include <stdio.h>
int main (void)
{
float basic_salary;
float DA;
float HRA;
float PF;
float gross_salary;
float net_salary;

//Input basic salary


printf("Input Basic Salary: ");
scanf("%f", &basic_salary);

//Calculate DA
DA = basic_salary * 25.0 / 100.0;

//Calculate HRA
HRA = basic_salary * 15.0 / 100.0;

//Calculate gross salary


gross_salary = basic_salary + DA + HRA;

//Calculate PF
PF = gross_salary * 10.0 / 100.0;

//Calculate net salary


net_salary = gross_salary - PF;

//Print result
printf("Basic Salary: %.2f\n", basic_salary);

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

printf("DA: %.2f\n", DA);


printf("HRA: %.2f\n", HRA);
printf("Gross Salary: %.2f\n", gross_salary);
printf("PF: %.2f\n", PF);
printf("Net Salary: %.2f", net_salary);

return 0;
}

Output:
Input Basic Salary: 25000
Basic Salary: 25000.00
DA: 6250.00
HRA: 3750.00
Gross Salary: 35000.00
PF: 3500.00
Net Salary: 31500.00

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

11. WRITE A PROGRAM to find area of a circle using PI as constant.

#include < stdio.h >


#include < conio.h > #define PI 3.141
int main()
{
float radius, area;
printf("Enter radius of circle\n");
scanf("%f", & radius);
area = PI * radius * radius;
printf("Area of circle : %0.4f\n", area);
getch();
return 0;
}

Output:

Enter radius of circle


6
Area of circle : 113.0760

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

12. WRITE A PROGRAM to find volume of a cube using side as input from user.

#include <stdio.h>

int main(){
float side, volume;

printf("Enter length of any side of cube\n");


scanf("%f", &side);

volume = side*side*side;
printf("Volume of Cube : %f\n", volume);

return 0;
}

Output:

Enter length of any side of cube


4
Volume of Cube : 64.000000

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

13. WRITE A PROGRAM using various unformatted Input Functions.

#include<stdio.h>
#include<conio.h>
int main()
{
int ch;
printf("Running getch(), please enter a character : ");
ch = getch();

printf("\n");
printf("The character value you have just entered : %c", ch);
printf("\n");

printf("Running getche(), please enter a character : ");


ch = getche();

printf("\n");
printf("The character value you have just entered : %c", ch);
printf("\n");
printf("Running getchar(), please enter a character : ");

ch = getchar();
printf("The character value you have just entered : %c", ch);
return 0;
}

Output:

Running getch(), enter a character :


The character value you have just entered : a
Running getche(), enter a character : g
The character value you have just entered : g
Running a getchar(), enter a character : w
The character value you have just entered : w

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

14. WRITE A PROGRAM to find area of rectangle and print the result
using unformatted output Functions.

#include<stdio.h>
int main()
{
float length,breadth;
float area;
printf(" Enter the Length of a Rectangle : "); scanf("%f",&length);
printf("\n Enter the Breadth of a Rectangle : "); scanf("%f",&breadth);
area = length * breadth;
printf("\n Area of Rectangle is : %f",area);
return 0;
}

OUTPUT:

Enter the Length of Rectangle : 3.2

Enter the Breadth of Rectangle : 4

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

15. WRITE A PROGRAM to find the larger of two numbers.

#include <stdio.h>
int main()
{
int num1, num2;
// Ask user to enter the two numbers
printf("Please Enter Two different values\n");
// Read two numbers from the user
scanf("%d %d", &num1, &num2);
if(num1 > num2)
{
printf("%d is Largest\n", num1);
}
else if (num2 > num1)
{
printf("%d is Largest\n", num2);
}
else
{
printf("Both are Equal\n");
}
return 0;
}

Output:

Please Enter Two different values


23
43
43 is Largest

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

16. WRITE A PROGRAM to find greater of three numbers using Nested If.

#include <stdio.h>
int main()
{
int A, B, C;

printf("Enter three numbers: ");


scanf("%d %d %d", &A, &B, &C);

if (A >= B) {
if (A >= C)
printf("%d is the largest number.", A);
else
printf("%d is the largest number.", C);
}
else {
if (B >= C)
printf("%d is the largest number.", B);
else
printf("%d is the largest number.", C);
}

return 0;
}

Output:

Enter the numbers A, B and C: 2 8 1


8 is the largest number.

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

17. WRITE A PROGRAM to find whether the given number is even or odd.

#include <stdio.h>

int main()
{
int a;
printf("Enter a: ");
scanf("%d", &a);

//logic
if (a % 2 == 0) {
printf("The given number is EVEN");
}
else {
printf("The given number is ODD");
}
return 0;
}

Output:
Enter a: 2

The given number is EVEN

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

17. WRITE A PROGRAM to Generate Multiplication Table Using for loop.

#include <stdio.h>
int main()
{
int num, i; // declare a variable
printf (" Enter a number to generate the table in C: ");
scanf (" %d", &num); // take a positive number from the user

printf ("\n Table of %d", num);


// use for loop to iterate the number from 1 to 10
for ( i = 1; i <= 10; i++)
{
printf ("\n %d * %d = %d", num, i, (num*i));
}
return 0;
}

Output:

Enter a number to generate the table in C: 9


Table of 9
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

18. WRITE A PROGRAM to Generate Multiplication Table Using while


loop.

#include <stdio.h>
int main()
{
int num, i = 1; // declare a variable
printf (" Enter a number to generate the table in C: ");
scanf (" %d", &num); // take a positive number from the user

printf ("\n Table of %d \n ", num);

// use while loop to evaluate the condition


while (i <= 10)
{
// print the table
printf (" %d x %d = %d \n", num, i, (num * i));
i++; // incremented by 1
}
Output:

Enter a number to generate the table in C: 4


Table of 4
4x1=4
4x2=8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

20. WRITE A PROGRAM to Make a Simple Calculator Using switch...case.

#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
double a, b;
while (1)
{
printf("Enter an operator (+, -, *, /), "
"if want to exit press x: ");
scanf(" %c", &ch);
// to exit
if (ch == 'x')
exit(0);
printf("Enter two first and second operand: ");
scanf("%lf %lf",&a,&b);

switch (ch) {
case '+':
printf("%.1lf + %.1lf = %.1lf\n",
a, b, a + b);
break;

// For Subtraction
case '-':
printf("%.1lf - %.1lf = %.1lf\n",
a, b, a - b);
break;

// For Multiplication
case '*':
printf("%.1lf * %.1lf = %.1lf\n",
a, b, a * b);

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

break;

// For Division
case '/':
printf("%.1lf / %.1lf = %.1lf\n",
a, b, a / b);
break;

// If operator doesn't match any case constant


default:
printf("Error! please write a valid operator\n");
}

printf("\n");
}
}

Output:

Enter an operator (+, -, *, /), if want to exit press x: +


Enter two first and second operand: 2
3
2.0 + 3.0 = 5.0
Enter an operator (+, -, *, /), if want to exit press x: x

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

21. WRITE A PROGRAM to find whether the given number is a prime number.

// C Program to check for prime number using Naive Approach


#include <stdio.h>

// Function to check prime number


void checkPrime(int N)
{
// initially, flag is set to true or 1
int flag = 1;

// loop to iterate through 2 to N/2


for (int i = 2; i <= N / 2; i++) {

// if N is perfectly divisible by i
// flag is set to 0 i.e false
if (N % i == 0) {
flag = 0;
break;
}
}

if (flag) {
printf("The number %d is a Prime Number\n", N);
}
else {
printf("The number %d is not a Prime Number\n", N);
}

return;
}

// driver code
int main()
{

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

int N ;
printf("enter no.");
scanf("%d",&N);

checkPrime(N);

return 0;
}

Output:

enter no.7
The number 7 is a Prime Number

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

22. WRITE A PROGRAM using function to find the largest of three numbers.

#include<stdio.h>
int biggest(int, int, int); // function prototype
int main()
{
int a, b, c;
printf("Enter 3 integer numbers\n");
scanf("%d%d%d", &a, &b, &c);
//function call biggest(a, b, c)
printf("Biggest of %d, %d and %d is %d\n", a, b, c, biggest(a, b, c));
return 0;
}
// function definition
int biggest(int x, int y, int z)
{
if(x > y && x > z)
{
return x;
}
else
{
if(y > z)
return y;
else
return z;
}
}

Output:

Enter 3 integer numbers


4
2
8

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

Biggest of 4, 2 and 8 is 8
23. WRITE A PROGRAM using function to print first 20 numbers and its
squares.
#include<stdio.h>
int main()
{
int x;
/* Print column names */
printf("Number\tSquare\tCube\n");
printf("=========================\n");
for(x=0; x<=20; x++)
printf("%d\t%d\t%d\n", x, x*x, x*x*x);
return 0;
}
Output:
Number Square Cube
=========================
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
11 121 1331
12 144 1728
13 169 2197
14 196 2744
15 225 3375
16 256 4096
17 289 4913
18 324 5832

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

19 361 6859
20 400 8000
24. WRITE A PROGRAM to find the factorial of a given number.

#include<stdio.h>

int main(){

int x,fact=1,n;

printf("Enter a number to find factorial: ");

scanf("%d",&n);

for(x=1;x<=n;x++)

fact=fact*x;

printf("Factorial of %d is: %d",n,fact);

return 0;

Output:

Enter a number to find factorial: 4


Factorial of 4 is: 24

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

25. WRITE A PROGRAM to print the sum of two matrices.

#include <stdio.h>
int main()
{
int a[2][2],b[2][2],s[2][2],i,j;
printf("Enter Elements of First 2*2 Matrix: \n\n");
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
{
scanf("%d",&a[i][j]);
}
printf("\nEnter Elements of Second 2*2 Matrix: \n\n");
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
{
scanf("%d",&b[i][j]);
}
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
{
s[i][j]=a[i][j]+b[i][j];
}
printf("\nSum of Matrices :\n\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d ",s[i][j]);
}
printf("\n");
}
return 0;
}

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

Output:
Enter Elements of First 2*2 Matrix:
3
4
5
6
Enter Elements of Second 2*2 Matrix:
3
5
2
4
Sum of Matrices :
69
7 10

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

26. WRITE A PROGRAM to Find the Length of a String.

#include <stdio.h>
#include <string.h>

int main()
{
char str[100];

printf("Enter a string: ");


scanf("%s", str);

int len = strlen(str);

printf("The length of the string is: %d\n", len);


return 0;
}

Output:

Enter a string: ssicmit


The length of the string is: 7

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

27.WRITE A PROGRAM to Copy String using strcpy().

#include<stdio.h>
#include<string.h>

main()
{
char source[] = "C Program";
char destination[50];
strcpy(destination, source);
printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);
return 0;
}

Output:

Source string: C Program


Destination string: C Program

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

28. WRITE A PROGRAM to compare a string.

#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;

// comparing strings str1 and str2


result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);

// comparing strings str1 and str3


result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);

return 0;
}

Output:

strcmp(str1, str2) = 32
strcmp(str1, str3) = 0

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

29. WRITE A PROGRAM to reverse a string.

#include <stdio.h>
#include <string.h>
int main()
{
char str[40]; // declare the size of character string
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);

// use strrev() function to reverse a string


printf (" \n After the reverse of a string: %s ", strrev(str));
return 0;
}

Output:

Enter a string to be reversed: AMBULANCE


After the reverse of a string: ECNALUBMA

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

30. WRITE A PROGRAM to reverse a string.

#include <stdio.h>
#include <string.h>
int main()
{
char str[40]; // declare the size of character string
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);

// use strrev() function to reverse a string


printf (" \n After the reverse of a string: %s ", strrev(str));
return 0;
}

Output:

Enter a string to be reversed: AMBULANCE


After the reverse of a string: ECNALUBMA

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

31. WRITE A PROGRAM to multiply two numbers using pointers.

#include<stdio.h>

void main()
{
int a, b, *p, *q, mul;

// Reads two user inputs integer values for variable a and b.


printf("\nEnter integer a:");
scanf("%d", &a);
printf("\nEnter integer b:");
scanf("%d", &b);

// assign address of variable a and b to integer pointers p and q.


p = &a;
q = &b;

// performs multiplication using pointers p and q referring the address of a and b


values.
mul = *p * *q;

printf("\nMultiplication of the numbers: %d", mul);


}

Output:

Enter integer a:3


Enter integer b:5
Multiplication of the numbers: 15

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

32. WRITE A PROGRAM to display address of variable using pointers.

#include <stdio.h>

int main() {
int a;
int *pt;

printf("Pointer Example Program : Print Pointer Address\n");


a = 10;
pt = &a;

printf("\n[a ]:Value of A = %d", a);


printf("\n[*pt]:Value of A = %d", *pt);
printf("\n[&a ]:Address of A = %p", &a);
printf("\n[pt ]:Address of A = %p", pt);
printf("\n[&pt]:Address of pt = %p", &pt);
printf("\n[pt ]:Value of pt = %p", pt);

return 0;
}

Output:

Pointer Example Program : Print Pointer Address

[a ]:Value of A = 10
[*pt]:Value of A = 10
[&a ]:Address of A = 0x7ffe685a034c
[pt ]:Address of A = 0x7ffe685a034c
[&pt]:Address of pt = 0x7ffe685a0340
[pt ]:Value of pt = 0x7ffe685a034c

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

33. WRITE A PROGRAM to show the memory occupied by Structure and Union.

#include<stdio.h>
struct s_tag
{
int a;
long int b;
} x;
union u_tag
{
int a;
long int b;
} y;
int main()
{
printf("Memory allocation for structure = %d", sizeof(x));
printf("\nMemory allocation for union = %d", sizeof(y));
return 0;
}

Output:

Memory allocation for structure = 16


Memory allocation for union = 8

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

34. WRITE A PROGRAM to create Student I-Card using a Structure

#include <stdio.h>
struct student {
char name[60];
int rollNum;
char address[75];
float marks;
} s[3];

int main() {
int i;
printf("Enter student information:\n");
// getting information
for (i = 0; i < 3; ++i) {
s[i].rollNum = i + 1;
printf("\nFor roll number%d: \n", s[i].rollNum);
printf("Enter Name: ");
scanf("%s", s[i].name);
printf("Enter Address: ");
scanf("%s", s[i].address);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("\nPrint student Information:\n");
// printing information
for (i = 0; i < 3; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].name);
printf("Address: ");
puts(s[i].address);
printf("Marks: %.1f", s[i].marks);
printf("\n");

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

}
return 0;
}
Output:
Enter student information:

For roll number1:


Enter Name: Chaitanya
Enter Address: Noida
Enter marks: 95

For roll number2:


Enter Name: Ajeet
Enter Address: Delhi
Enter marks: 97

For roll number3:


Enter Name: Harry
Enter Address: Agra
Enter marks: 96.5

Print student Information:

Roll number: 1
First name: Chaitanya
Address: Noida
Marks: 95.0

Roll number: 2
First name: Ajeet
Address: Delhi
Marks: 97.0

Roll number: 3
First name: Harry
Address: Agra

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

Marks: 96.5

35. WRITE A PROGRAM to read data from a file from a file.

#include <stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main(){
char ch;
FILE *fp;
fp=fopen("std1.txt","w");
printf("enter the text.press cntrl Z:
");
while((ch = getchar())!=EOF){
putc(ch,fp);
}
fclose(fp);
fp=fopen("std1.txt","r");
printf("text on the file:
");
while ((ch=getc(fp))!=EOF){
if(ch == ',')
printf("\t\t");
else
printf("%c",ch);
}
fclose(fp);
return 0;
}
Output :

enter the text.press cntrl Z:

Name,Item,Price

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

Bhanu,1,23.4
Priya,2,45.6
text on the file:
Name Item Price
Bhanu 1 23.4
Priya 2 45.6

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

36. WRITE A PROGRAM to save Employee details in a file using File Handling.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
int i, n, empno;
float bpay, allow, ded;
char name[10];
clrscr();
fptr = fopen("EMPLOYEE.DAT", "w");
printf("Enter the number of employees : ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("\nEnter the employee number : ");
scanf("%d", &empno);
printf("\nEnter the name : ");
scanf("%s", name);
printf("\nEnter the basic pay, allowances & deductions : ");
scanf("%f %f %f", &bpay, &allow, &ded);
fprintf(fptr, "%d %s %f %f %f \n", empno,name,bpay,allow,ded);
}
fclose(fptr);

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

fptr = fopen("EMPLOYEE.DAT", "r");


printf("\nEmp. No.Name\t\t Bpay\t\t Allow\t\t Ded\t\t Npay\n\n");
for(i = 0; i < n; i++)
{
fscanf(fptr,"%d%s%f%f%f\n", &empno,name,&bpay,&allow,&ded);
printf("%d \t %s \t %.2f \t %.2f \t %.2f \t %.2f \n", empno, name, bpay,
allow, ded, bpay + allow - ded);
}
fclose(fptr);
getch();
}

Output:
Enter the number of employees : 2
Enter the employee number : 101
Enter the name : ram
Enter the basic pay, allowances & deductions : 20000
2000
1500
Enter the employee number : 102
Enter the name : sham
Enter the basic pay, allowances & deductions : 20000
1000
1500
Emp. No.Name Bpay Allow Ded Npay

101 ram 20000.00 2000.00 1500.00 20500.00

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)


lOMoARcPSD|37497581

102 sham 20000.00 1000.00 1500.00 19500.00

Downloaded by Jaswinder Kaur (jaswinder.bca@ggi.ac.in)

You might also like