lab-manual-c-programming
lab-manual-c-programming
lab-manual-c-programming
LAB MANUAL
PROBLEM SOLVING C PROGRAMMING
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”.
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.
#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: ");
#include <stdio.h>
int main()
{
int A, B, sum = 0;
return 0;
}
Output:
Enter two numbers A and B :
21
22
Sum of A and B is: 43
#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
#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:
#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
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));
include <stdio.h>
// Driver code
int main()
{
int A, B, quotient = 0, remainder = 0;
return 0;
}
Output:
Enter two numbers A and B: Quotient when A / B is: 3
Remainder when A / B is: 2
include <stdio.h>
int main() {
int n1;
int n2;
printf("Enter the number 1:\n");
scanf("%d", &n1);
return 0;
Output
Enter the number1: 3
Enter the number2: 55
9. WRITE A PROGRAM to find the simple interest, inputs are amount, period in
years and rate of interest.
// 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;
return 0;
}
Output:
Enter p,r, and t values
2300
20
2
Simple Interest = 920.000000
#include <stdio.h>
int main (void)
{
float basic_salary;
float DA;
float HRA;
float PF;
float gross_salary;
float net_salary;
//Calculate DA
DA = basic_salary * 25.0 / 100.0;
//Calculate HRA
HRA = basic_salary * 15.0 / 100.0;
//Calculate PF
PF = gross_salary * 10.0 / 100.0;
//Print result
printf("Basic Salary: %.2f\n", basic_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
Output:
12. WRITE A PROGRAM to find volume of a cube using side as input from user.
#include <stdio.h>
int main(){
float side, volume;
volume = side*side*side;
printf("Volume of Cube : %f\n", volume);
return 0;
}
Output:
#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("\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:
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:
#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:
16. WRITE A PROGRAM to find greater of three numbers using Nested If.
#include <stdio.h>
int main()
{
int 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:
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
#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
Output:
#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
#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);
break;
// For Division
case '/':
printf("%.1lf / %.1lf = %.1lf\n",
a, b, a / b);
break;
printf("\n");
}
}
Output:
21. WRITE A PROGRAM to find whether the given number is a prime number.
// 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()
{
int N ;
printf("enter no.");
scanf("%d",&N);
checkPrime(N);
return 0;
}
Output:
enter no.7
The number 7 is a Prime Number
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:
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
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;
scanf("%d",&n);
for(x=1;x<=n;x++)
fact=fact*x;
return 0;
Output:
#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;
}
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
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
Output:
#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:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
return 0;
}
Output:
strcmp(str1, str2) = 32
strcmp(str1, str3) = 0
#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);
Output:
#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);
Output:
#include<stdio.h>
void main()
{
int a, b, *p, *q, mul;
Output:
#include <stdio.h>
int main() {
int a;
int *pt;
return 0;
}
Output:
[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
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:
#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");
}
return 0;
}
Output:
Enter 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
Marks: 96.5
#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 :
Name,Item,Price
Bhanu,1,23.4
Priya,2,45.6
text on the file:
Name Item Price
Bhanu 1 23.4
Priya 2 45.6
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);
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