C Lab Manual 1
C Lab Manual 1
Subject Code:
Prepared By
Dr. M.N. Nachappa,
Professor,
School of CS and IT,
Jain (Deemed-to-be)
University
1
PROGRAMMING IN C LABORATORY OBJECTIVE
OUTCOMES
Upon completion of the course, the students acquire the knowledge to build the
unions, files
2
Program List
Session No. Programs Page No.
1 1. C program to display Your Name, Address and City in
different lines.
2. C program to find the area and circumference of a
circle.
3. C program to convert centigrade into
Fahrenheit. Formula: F= (1.8 * C) +32.
4. C program to swap variable values of two variables
a. Using a temporary variable
b. Without Using a temporary variable
5. C program to calculate the total salary of an
employee given Allowance1 is 33% of Basic Pay,
Allowance2 is 73% of Basic Pay and Deduction is
52% of Basic Pay.
6. C program to calculate simple interest.
2 7. C program to find the largest of three numbers (if).
8. C program to check whether a given year is a leap year
(if-else).
9. C program to find the largest, smallest and
second largest of three numbers.
10. C program to find the second largest and second
smallest of four numbers (else-if).
11. C program to output the next date for a given
date (else-if).
12. C program to find the roots of a quadratic
equation (else-if)
13. C program to check whether a given date is valid or
not (switch).
3 14. C program to output the digits of a number (while).
15. C program to find the sum of all numbers from 1 to
“N” (while).
16. C program to reverse a number (while).
17. C program to calculate compound interest (do-while).
18. C program to convert from (do-while)
a. Decimal to binary
b. Binary to decimal
4 19. C program to find the factorial of a number (for).
20. C program to check whether a number is prime or
not (for).
21. C program to generate first “N” Fibonacci numbers
(for).
3
22. C program to calculate x^y (for).
23. C program to find the sum of the series (for)
a. SIN(X)
b. COS(X)
5 24. C program to generate prime numbers from 1 to “N”.
25. C program to generate the patterns.
a)1 b) 1
1 2 2 2
1 2 3 3 3 3
1 2 3 4 4 4 4 4
4
number.
37. C program using recursion to find the sum of natural
numbers.
38. C program using recursion to count the digits of a
number.
8 39. C program to find the largest and smallest of “N”
numbers using 1-D arrays.
40. C program to sort a list of numbers using Bubble sort.
41. C program to sort a list of numbers using Selection
sort.
42. C program to sort a list of numbers using Insertion
sort.
43. C program to find search for a given number using
Linear search.
44. C program to find search for a given number using
Binary search.
9 45. C program to find the sum of two matrices.
46. C program to find the product of two matrices.
47. C program to transpose a given matrix.
48. C program to check whether a given matrix is
an identity matrix.
49. C program to check whether a given matrix is a scalar
matrix.
10 50. C program to find the frequency of a character in
a string.
51. C program to reverse a string.
52. C program to copy the contents of one string
to another.
53. C program to check whether a given string is
a palindrome or not (without library
functions).
54. C program to remove all blank spaces and punctuation
symbols from a string.
11 55. C program to create and use a pointer.
56. C program to swap the values of two variables using
pointers.
57. C program to find the area and circumference of a
circle using pointers and functions.
58. C program to sort a list of numbers using pointers.
59. C program to concatenate two strings using
pointers and functions.
12 60. C program to create and use a structure for a
student data.
61. C program to add two time periods using structures.
5
6
62. C program to create and use a union.
63. C program to create a file to hold the data of
employees input and output data from it.
64. C program to write a sentence in a file and convert all
lower case alphabets to uppercase and vice versa.
7
1. To display Your Name, Address and City in different lines.
Program Algorithm and Flowchart
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( ); // clears the output window
Output:
2. 8 of a circle.
To find the area and circumference
Program Algorithm and Flowchart
#include<stdio.h>
#include<conio.h>
void main()
{
int rad;
float PI = 3.14, area, ci;
clrscr();
printf("\nEnter radius of circle: ");
scanf("%d", &rad);
getch();
}
Output:
9
Program Algorithm and Flowchart
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius, fahrenheit;
clrscr();
printf("\nEnter temp in Celsius : ");
scanf("%f", &celsius);
getch();
}
Output:
1
0
Program Algorithm and Flowchart
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c,L ;
clrscr();
printf("\nEnter value of a, b & c : ");
scanf("%d%d%d", &a, &b, &c);
getch();
}
Output:
11
8. To check whether a given year is a leap year (if-else).
Program Algorithm and Flowchart
#include<stdio.h>
#include<conio.h>
void main()
{
int y,r ;
clrscr();
printf("\nEnter any year : ");
scanf("%d”, &y);
r = y % 4;
if (r==0)
printf(“ \n %d is a leap year “, y);
else
printf(“ \n %d is not a leap year “, y);
getch();
}
Output:
12
9. To find the largest, smallest and second largest of three numbers.
Program Algorithm and Flowchart
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
int L,S,SL;
clrscr();
printf("\nEnter value of a, b & c : ");
scanf("%d%d%d", &a, &b, &c);
L =a;
if (b > L) L = b;
if (c > L) L = c;
S =a;
if (b < S) S = b;
if (c < S) S = c;
SL = (a+b+c) – (L+S);
getch();
}
Output:
13
12. To find the roots of a quadratic equation (else-if)
Program Algorithm and Flowchart
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
float a, b, c;
float desc, root1, root2;
clrscr();
printf("\nEnter the constants: "); scanf("%f
%f%f", &a,&b,&c);
desc = b * b - 4 * a * c;
if ( desc > 0)
{
printf(“\n Roots are Real”)
getch ();
}
14
Output:
15
#include<conio.h>
void main()
{
int i, num, sum;
clrscr();
printf("\n Enter the limit ");
scanf ("%d", &num);
sum =0;
i = 1;
while (i <= num)
{
sum = sum + i;
++i;
}
getch();
}
Output:
16
#include<conio.h>
void main()
{
int num, num1,rem, rev;
clrscr();
printf("\nEnter any number: ");
scanf("%d", &num);
num1 = num;
rev = 0;
while (num !=0)
{
rem = num % 10;
rev = rev * 10 + rem;
num = num / 10;
}
getch();
}
Output:
17
18.a. To convert from Decimal to binary (do-while)
Program Algorithm and Flowchart
#include <stdio.h>
#include <math.h>
#include<conio.h>
void main()
{
int num,dec,bin,r,k;
clrscr();
printf("Enter a Decimal number: ");
scanf("%f", &dec);
num = dec;
bin = 0;
k = 1;
do
{
r = num % 2;
num = num /2;
bin = bin + r * k;
k = k * 10;
} while (num!=0);
getch();
Output:
18
19. To find the factorial of a number.
Program Algorithm and Flowchart
#include <stdio.h>
19
#include <conio.h>
void main()
{
int i, n, fact = 1;
clrscr();
printf("\nEnter a number");
scanf("%d", &n);
getch();
}
Output:
20
#include <conio.h>
void main()
{
int n, i, flag = 1;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);
getch();
}
Output:
21
23.a. To find the sum of the series to calculate SIN(x).
Program Algorithm and Flowchart
#include<stdio.h>
#include<conio.h>
22
void main()
{
int i;
float x, sum, t;
clrscr();
printf(" Enter the value for x : ");
scanf("%f",&x);
x=x*3.14/180;
t=x;
sum=x;
for(i=1;i<=n;i++)
{
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
}
getch();
}
Output:
23
24
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, rows;
clrscr();
printf("Enter number of rows: ");
scanf("%d",&rows);
Output:
25
#include <conio.h>
void main()
{
int i, j, rows;
clrscr();
printf("Enter number of rows: ");
scanf("%d",&rows);
Output:
26
void main()
{
int i, j, ,k=1,rows;
clrscr();
printf("Enter number of rows: ");
scanf("%d",&rows);
Output:
29. To find the GCD and LCM of two numbers using functions.
Program Algorithm and Flowchart
#include <stdio.h>
#include <conio.h>
void main()
{
27
int num1, num2, gcd, lcm;
int GCD(int,int);
clrscr();
printf("Enter two numbers\n");
scanf("%d%d", &num1, &num2);
gcd = GCD(num1, num2);
lcm = (num1 * num2) / gcd;
Output:
28
32. To check whether a number is an Armstrong number using functions.
Program Algorithm and Flowchart
#include <stdio.h>
#include <conio.h>
void main()
{
int num, flag;
29
int Armstrong(int);
clrscr();
printf("Enter a numbers\n");
scanf("%d", &num);
flag = Armstrong(num);
if (flag)
printf ("\n %d is an armstrong no",
num);
else
printf ("\n %d is not an armstrong no",
num);
getch();
}
int Armstrong(int n)
{
int num, sum = 0, rem = 0;
num = n;
sum = 0;
while (num != 0)
{
rem = num % 10;
sum = sum + rem * rem * rem;
num = num / 10;
}
if (sum == n)
return (1);
else
retrun (0);
}
Output:
30
33. To calculate compound interest using functions.
Program Algorithm and Flowchart
#include <stdio.h>
#include <conio.h>
void main()
{
float prin, rate, ci;
31
int tim;
float compint(float, int, float);
clrscr();
printf("Enter prin., time and rate \n");
scanf("%f%d%f", &prin, &tim, &rate);
ci = compint(prin, tim, rate);
getch();
}
Output:
32
34. To find the factorial of a number using recursion.
Program Algorithm and Flowchart
#include <stdio.h>
#include <conio.h>
long int factorial(int n);
void main()
{
int n;
33
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n,
factorial(n));
getch();
}
Output:
34
clrscr();
printf("\nEnter value of x and y:
"); scanf("%f%d",&x,&y);
p = power(x,y);
printf("%f ^ %d = %f", x, y, p);
getch();
}
Output:
35
clrscr();
printf("\nEnter which number is to be
displayed: ");
scanf("%d", &num);
if (num < 0)
printf("Fibonacci of negative number is
not possible.\n");
else
{
result = fibo(num);
printf("The %d number in fibonacci
series is %d\n", num, result);
}
getch();
}
int fibo(int num)
{
if (num == 0)
return 0;
else if (num == 1)
return 1;
else
return(fibo(num - 1) + fibo(num - 2));
}
Output:
void main()
{
int a[50],i,n,large,small;
36
clrscr();
printf("\nHow many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=a[0];
small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
Output:
void main()
{
int count, temp, i, j, a[30];
37
clrscr();
printf("\nHow many numbers: ");
scanf("%d",&count);
printf("\nEnter %d numbers: ",count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=1;i< count;i++) {
for(j=0;j<=i;j++) {
if(number[j]>number[j+1]){
temp=number[j];
number[j]=number[j+1];
number[j+1]=temp;
}
}
}
printf("\nSorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
getch();
}
Output:
38
printf("\nEnter number of elements :");
scanf("%d",&n);
printf("\nEnter %d number :", n);
for (i=0; i<n; i++)
scanf("%d",&arr[i]);
printf("Enter a number to find :");
scanf("%d", &search);
first = 0;
last = n-1;
flag = 0;
while ((first <= last) && !(flag))
{ middle = (first+last)/2;
if(arr[middle] == search)
{
flag = 1;
break;
}
else if(arr[middle] < search)
first = middle + 1;
else
last = middle - 1;
}
if (flag)
printf("\n%d found at location %d",
search, middle+1);
else
printf("\n%dNot found!",search);
getch();
}
Output:
39
45. To find the sum of two matrices.
Program Algorithm and Flowchart
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10], c[10][10];
int i, j, m, n ;
clrscr();
40
printf("\nEnter the order of the
matrices");
scanf("%d%d", &m, &n);
printf("\nEnter matrix 1 elements :");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d",&a[i][j]);
Output:
clrscr();
41
printf("\nEnter the order of first
matrix");
scanf("%d%d", &m, &n);
printf("\nEnter the order of second
matrix");
scanf("%d%d", &p, &q);
if( n != p)
{
printf("\n Multiplication not
possible !");
break;
}
else
{
printf("\nEnter matrix 1
elements :");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d",&a[i][j]);
printf("\nEnter matrix 2
elements :");
for(i=0; i<p; i++)
for(j=0; j<q; j++)
scanf("%d",&b[i][j]);
42
Output:
clrscr();
printf("\nEnter the order of the matrix");
43
scanf("%d%d", &m, &n);
printf("\nEnter matrix elements :");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d",&a[i][j]);
printf("Transposing Array...\n");
for(i=0; i<n; i++)
for(j=0; j<m; j++)
b[i][j]=a[j][i];
Output:
void main()
{
char str[100], ch;
int i, count = 0;
44
gets(str);
printf("Enter a character \n");
ch = getchar();
getch();
}
Output:
void main()
{
char str[100], rstr[100];
int i, L;
45
gets(str);
L= strlen(str);
rstr[l]='\0';
getch();
}
Output:
void main()
{
char str[100], cstr[100];
int i, l;
46
gets(str);
l= strlen(str);
cstr[i]='\0';
getch();
}
Output:
void main ()
{
int a,b;
void swap(int *, int *);
clrscr();
printf("Enter two numbers \n"); scanf("%d
%d",&a,&b);
47
printf("Before swapping\n");
printf("a = %d , b = %d \n", a, b
);
swap(&a, &b);
printf("After swapping\n");
printf("a = %d , b = %d \n", a, b
); getch();
}
temp = *x;
*x = *y;
*y = temp;
return;
}
Output:
void main ()
{
float r,a,c;
void calc(float , float *, float *);
clrscr();
printf("Enter the radius \n");
scanf("%f",&r);
48
clac(r, &a, &c);
*y = 3.14 * x *x;
*z = 2 * 3.14 * x;
return;
}
Output:
clrscr();
printf("\nHow Many Numbers: ");
scanf("%d",&n);
49
for(i=0;i<n;i++)
scanf("%d",&a[i]);
p=a; for(i=0;i<n;i+
+) for(j=0;j<n;j++)
{
if(*(p+i)<*(p+j))
{
temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=temp;
}
}
printf("\nSorted Numbers Are:\n");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
getch();
}
Output:
struct student
{
char name[50];
int USN;
float marks;
};
50
void main()
{
int i;
struct student s[10];
clrscr();
printf("Enter information of 10
students:\n");
for(i=0; i<10; ++i)
{
s[i].roll = i+1;
printf("\nFor USN %d,\n",s[i].roll);
printf("Enter name: ");
gets(s[i].name);
printf("Enter Total marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf("Displaying Student
Information:\n\n");
for(i=0; i<10; ++i)
{
printf("\nUSN : %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Total Marks: %.1f",s[i].marks);
printf("\n");
}
getch();
}
Output:
51
61. To create and use an Union.
Program Algorithm and Flowchart
#include <stdio.h>
#include <conio.h>
union test
{
int x, y;
};
void main()
{
union test t;
52
clrscr();
t.x = 2;
printf(" After assigning value for x \
n"); printf(" x = %d y = %d \n",t.x, t.y);
t.y = 10;
printf(" After assigning value for y \
n"); printf(" x = %d y = %d \n",t.x, t.y);
getch();
}
Output:
void main()
{
FILE *fptr ;
int i, n, empno ;
float bpay, allow, ded ;
char name[10] ;
clrscr() ;
fptr = fopen("EMPLOYEE.DAT", "w") ;
53
printf("Enter number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter 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) ;
getch() ;
}
Output:
54