c lab manual(r23)-1
c lab manual(r23)-1
WEEK2:
# include <stdio.h>
int main()
{
//Simple interset program
int P, R, T;
float SI;
printf("Enter the principal: ");
scanf("%d", &P);
printf("Enter the rate: ");
scanf("%d", &R);
printf("Enter the time: ");
scanf("%d", &T);
SI = (P * R * T) / 100;
printf("The Simple interest is %f", SI);
return 0;
}
Output:
Enter the principal: 12000
Enter the rate: 10
Enter the time: 3
The Simple interest is 3600.000000
WEEK3:
5. Finding the square root of a given
number
Aim: Write a C program to find the square root of a given number
Program:
#include <stdio.h>
#include <math.h>
int main(){
float num, root;
root = sqrt(num);
printf("The Square Root of %f is %f.", num, root);
return 0;
}
Output:
Enter an integer: 36
The Square Root of 36.000000 is 6.000000.
6. Finding compound interest
Aim: Write a C program to find compound interest
Program:
#include <stdio.h>
#include <math.h>
int main()
{
float p, r, t, ci;
ci = p * pow((1 + r / 100), t) - p;
int main()
{
float a, b, c, s, area;
printf("\nEnter three sides of
triangle\n");
scanf("%f%f%f",&a,&b,&c);
s = (a+b+c)/2;
return 0;
}
Output:
Enter three sides of triangle
563
Area of triangle: 7.483315
8. Distance travelled by an object
Aim: Write a C program to find Distance travelled by an object.
Program:
#include<stdio.h>
int main() {
float u, a, d;
int t;
d = (u * t) + (a * t * t) / 2;
return 0;
}
Output:
Enter the value of a : 3
Enter the value of u : 5
Enter the value of t : 2
The Distance : 16.00
WEEK4:
9. Evaluate the following expressions.
a. A+B*C+(D*E) + F*G
b. A/B*C-B+A*D/3
c. A+++B---A
d. J= (i++) + (++i)
void main()
{
int a, b, c, big ;
11) Take marks of 5 subjects in integers, and find the total, average in float.
AIM: Take marks of 5 subjects in integers, and find the total, average in float
Program:
#include <stdio.h>
#include<conio.h>
int main ()
{
int eng, phy, chem, math, comp;
float total, average;
printf ("Enter marks of five subjects: \n");
scanf ("%d%d%d%d%d", &eng, &phy, &chem, &math, &comp);
total = eng + phy + chem + math + comp;
average = total / 5.0;
printf("Total marks = %.2f\n", total);
printf("Average marks = %.2f\n", average);
return 0;
}
Output:
Enter marks of five subjects:
89
92
86
84
95
Total marks = 446.00
Average marks = 89.20
WEEK5:
12) Write a C program to find the max and min of four numbers using if-else.
AIM: Write a C program to find the max and min of four numbers using if-else.
Program:
#include <stdio.h>
#include<conio.h>
main()
{
int a, b, c, d, y;
printf("Enter four integers (separate them with spaces): ");
scanf("%d %d %d %d", &a, &b, &c, &d);
if (a>b && a>c && a>d){
if (b<c && b<d){
y = b;
}
else if (c<b && c<d){
y = c;
}
else if (d<b && d<c){
y = d;
}
printf("Largest: %d\n", a);
printf("Smallest: %d", y);
}
else if (b>a && b>c && b>d) {
if (a<c && a<d){
y = a;
}
else if(c<a && c<d){
y = c;
}
else if(d<a && d<c){
y = d;
}
printf("Largest: %d\n", b);
printf("Smallest: %d", y);
}
else if (c>a && c>b && c>d)
{
if (a<b && a<d){
y = a;
}
else if(b<a && b<d){
y = b;
}
else if(d<a && d<b){
y = d;
}
printf("Largest: %d\n", c);
printf("Smallest: %d", y);
}
else if (d>a && d>b && d>c) {
if (a<b && a<c){
y = a;
}
else if(b<a && b<c){
y = b;
}
else if(c<a && c<b){
y = c;
}
printf("Largest: %d\n", d);
printf("Smallest: %d", y);
}
return 0;
}
Output:
Enter four integers (separate them with spaces):
21
8
4
52
Largest: 52
Smallest: 4
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int units;
float amount, charge, total_amount;
printf ("Enter total units consumed: ");
scanf ("%d", &units);
Output
Enter total units consumed: 280
Electricity Bill: 1220.00
Program:
# include<stdio.h>
# include<math.h>
int main ()
{
float a, b, c, r1, r2, d;
printf ("Enter the values of a b c: ");
scanf (" %f %f %f", &a, &b, &c);
d= b*b - 4*a*c;
if (d>0)
{
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf ("The real roots = %f %f", r1, r2);
}
else if (d==0)
{
r1 = -b/(2*a);
r2 = -b/(2*a);
printf ("Roots are equal =%f %f", r1, r2);
}
else
printf ("Roots are imaginary");
return 0;
}
Output:
Case 1: Enter the values of a b c:
1
4
3
The real roots = -3.000000 -5.000000
Case 2:
Enter the values of a b c: 1
2
1
Roots are equal =-1.000000 -1.000000
Case 3:
Enter the values of a b c: 1
1
2
Roots are imaginary
switch (ch)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf ("Invalid operation\n");
}
printf("The value = %f", result);
return 0;
}
OUTPUT:
Enter first number = 8
Enter second number = 9
Choose operator to perform operations = +
The value = 17.000000
16) Write a C program to find the given year is a leap year or not.
AIM: Write a C program to find the given year is a leap year or not
Program:
#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if(year%400 == 0)
{
printf("%d is a leap year.\n", year);
}
else
if(year%100 == 0)
printf("%d is not a leap year.\n", year);
else
if(year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
Output:
Enter a year to check if it is a leap year
2016
2016 a leap year.
WEEK6:
if (c == 2)
{
printf ("n is a Prime number");
}
else
{
printf ("n is not a Prime number");
}
return 0;
}
Output:
Enter any number n: 7
n is a Prime number
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x, sum, term;
int i,n;
clrscr ();
printf ("enter the no of terms: \n");
scanf ("%d",&n);
printf ("enter the angle in degrees x: \n");
scanf("%f", &x);
x=(x*3.14)/180;
sum=1;
term=1;
for (i=1; i<=n; i++)
{
term=(term*(-1)*x*x)/((2*i)*(2*i-1));
sum+=term;
}
printf("cos valve of given angle is %f",sum);
getch();
}
Output:
enter the no of terms: 3
enter the angle in degrees x: 30
cos valve of given angle is 0.866158
Output:
Enter the number=151
palindrome number
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i;
++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows: 8
*
***
*****
*******
*********
***********
*************
***************
WEEK7:
int main()
{
int arr1[100];
int i, mx, mn, n;
mx = arr1[0];
mn = arr1[0];
for(i=1; i<n; i++)
{
if(arr1[i]>mx)
{
mx = arr1[i];
}
if(arr1[i]<mn)
{
mn = arr1[i];
}
}
printf("Maximum element is : %d\n",
mx);
printf("Minimum element is : %d\n\n",
mn);
}
Output:
#include <stdio.h>
int main()
{
int array[100], search, c, n, count = 0;
return 0;
}
Output:
Enter number of elements in array
8
Enter 8 numbers
5
5
6
5
6
5
6
5
Enter a number to search
6
6 is present at location 3.
6 is present at location 5.
6 is present at location 7.
6 is present 3 times in the array.
int main()
{
int i,j,n,a[10],m,o,b[10];
printf("\nEnter the Limit:");
scanf("%d",&n);
printf("\nEnter the values:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nGiven values are:");
for(i=0;i<n;i++)
{
printf("\na[%d]=%d",i,a[i]);
}
for(i=0;i<n;i++)
{
b[i]=a[n-i-1];
}
printf("\nUpdated values are :");
for(i=0;i<n;i++)
{
printf("\na[%d]=%d",i,b[i]);
}
return 0;
}
Output:
Enter the Limit: 8
#include <stdio.h>
#define SIZE 8
int main()
{
char binary[SIZE + 1], onesComp[SIZE +
1], twosComp[SIZE + 1];
int i, carry=1;
printf("Enter %d bit binary value: ", SIZE);
gets(binary);
for(i=0; i<SIZE; i++)
{
if(binary[i] == '1')
{
onesComp[i] = '0';
}
else if(binary[i] == '0')
{
onesComp[i] = '1';
}
}
onesComp[SIZE] = '\0';
return 0;
}
Output:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[50],i,j,k, count = 0, dup[50], number;
printf("Enter size of the array");
scanf("%d",&number);
printf("Enter Elements of the array:");
for(i=0;i<number;i++){
scanf("%d",&a[i]);
dup[i] = -1;
}
printf("Entered element are: ");
for(i=0;i<number;i++){
printf("%d ",a[i]);
}
for(i=0;i<number;i++){
for(j = i+1; j < number; j++){
if(a[i] == a[j]){
for(k = j; k <number; k++){
a[k] = a[k+1];
}
j--;
number--;
}
}
}
printf("After deleting the duplicate
element the Array is:");
for(i=0;i<number;i++){
printf("%d ",a[i]);
}
}
Output:
Enter size of the array5
Enter Elements of the array:
5
5
6
8
9
Entered element are: 5 5 6 8 9
After deleting the duplicate element the
Array is: 5 6 8 9
WEEK8:
#include <stdio.h>
int main ()
{
int mat1[3][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8} };
int mat2[3][3] = { {9, 10, 11}, {12, 13, 14}, {15, 16, 17} };
int sum[3][3], i, j;
Output:
Matrix 1 is:
0 1 2
3 4 5
6 7 8
Matrix 2 is:
9 10 11
12 13 14
15 16 17
15 17 19
21 23 25
#include<stdio.h>
int main(void)
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}
Output:
31. Reverse a string using built-in and without built-in string functions
AIM:(i) To write a program in Reverse a string using built-in string functions
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char s[100];
printf("Enter a string to reverse\n");
gets(s);
strrev(s);
printf("Reverse of the string: %s\n", s);
return 0;
}
Output : Hello
olleH
AIM:(ii) To write a program in Reverse a string with out using built-in string functions
Program:
#include <stdio.h>
#include <string.h>
void main()
{
char string[20],temp;
int i,length;
printf("Enter String : ");
scanf("%s",string);
length=strlen(string)-1;
for(i=0;i<strlen(string)/2;i++)
{
temp=string[i];
string[i]=string[length];
string[length--]=temp;
}
printf("Reverse string :%s",string);
}
Out put : Enter String : hai
Reverse string :iah
WEEK9:
return 0;
}
Output : Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,
35.Read student name and marks from the command line and display the student
details along with the total
Aim:Read student name and marks from the command line and display the student
details along with the total
Program:
#include <stdio.h>
struct student {
char name[50];
int roll;
float sub1;
float sub2;
float marks;
} s;
int main()
{
printf("Enter information:\n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter sub1 ");
scanf("%f", & s.sub1);
printf("Enter sub2 ");
scanf("%f", & s.sub2);
s.marks=s.sub1+s.sub2;
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("Marks: %.1f\n", s.marks);
return 0;
}
Output:Enter information:
Enter name: darshi
Enter roll number: 1
Enter sub1 89
Enter sub2 90
Displaying Information:
Name: darshi
Roll number: 1
Marks: 179.0
36.Write a C program to implement realloc()
Aim:Write a C program to implement realloc()
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr = (int*) malloc(3 * sizeof(int));
ptr[0] = 1;
ptr[1] = 2;
ptr[2] = 3;
ptr = (int*) realloc(ptr, 5 * sizeof(int));
ptr[3] = 4;
ptr[4] = 5;
for (int i = 0; i< 5; i++) {
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}
Output: 1 2 3 4 5
WEEK10:
void main()
{
struct struct_example s = { 18, 38, "geeksforgeeks" };
union union_example u = { 18, 38, "geeksforgeeks" };
printf("structure data:\n integer: %d\n"
"decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
printf("\nunion data:\n integer: %d\n"
"decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
printf("\nsizeof structure : %d\n", sizeof(s));
printf("sizeof union : %d\n", sizeof(u));
printf("\n Accessing all members at a time:");
s.integer = 183;
s.decimal = 90;
strcpy(s.name, "geeksforgeeks");
printf("structure data:\n integer: %d\n "
"decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
u.integer = 183;
u.decimal = 90;
strcpy(u.name, "geeksforgeeks");
printf("\nunion data:\n integer: %d\n "
"decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
printf("\n Accessing one member at time:");
printf("\nstructure data:");
s.integer = 240;
printf("\ninteger: %d", s.integer);
s.decimal = 120;
printf("\ndecimal: %f", s.decimal);
strcpy(s.name, "C programming");
printf("\nname: %s\n", s.name);
printf("\n union data:");
u.integer = 240;
printf("\ninteger: %d", u.integer);
u.decimal = 120;
printf("\ndecimal: %f", u.decimal);
strcpy(u.name, "C programming");
printf("\nname: %s\n", u.name);
// difference four
printf("\nAltering a member value:\n");
s.integer = 1218;
printf("structure data:\n integer: %d\n "
" decimal: %.2f\n name: %s\n",
s.integer, s.decimal, s.name);
u.integer = 1218;
printf("union data:\n integer: %d\n"
" decimal: %.2f\n name: %s\n",
u.integer, u.decimal, u.name);
}
Output : structure data:
integer: 18
decimal: 38.00
name: geeksforgeeks
union data:
integer: 18
decimal: 0.00
name:
sizeof structure : 28
sizeof union : 20
Accessing all members at a time:structure data:
integer: 183
decimal: 90.00
name: geeksforgeeks
union data:
integer: 1801807207
decimal: 277322871721159507258114048.00
name: geeksforgeeks
Accessing one member at time:
structure data:
integer: 240
decimal: 120.000000
name: C programming
union data:
integer: 240
decimal: 120.000000
name: C programming
Altering a member value:
structure data:
integer: 1218
decimal: 120.00
name: C programming
union data:
integer: 1218
decimal: 0.00
name:
39.Write a C program to shift/rotate using bitfields
Aim:Write a C program to shift/rotate using bitfields
Program:
#include <stdio.h>
#define INT_BITS 32
int leftRotate(int n, unsigned int d)
{
return (n << d) | (n >> (INT_BITS - d));
}
int rightRotate(int n, unsigned int d)
{
return (n >> d) | (n << (INT_BITS - d));
}
void main()
{
int n = 16;
int d = 2;
printf("Left Rotation of %d by %d is ", n, d);
printf("%d", leftRotate(n, d));
printf(" Right Rotation of %d by %d is ", n, d);
printf("%d", rightRotate(n, d));
}
Output : Left Rotation of 16 by 2 is 64 Right Rotation of 16 by 2 is 4.
40.Write a C program to copy one structure variable to another structure of the same
type
Aim:Write a C program to copy one structure variable to another structure of the same
type
Program:
#include <stdio.h>
struct myStructure
{
int myNum;
char myLetter;
char myString[30];
};
int main()
{
struct myStructure s1 = {13, 'B', "Some text"};
struct myStructure s2={14,'c',"hello"};
struct myStructure s3;
s3 = s2;
printf("%d %c %s", s3.myNum, s3.myLetter, s3.myString);
return 0;
}
Output:14 c hello
WEEK11:
}
Output:
Enter a string:programming
String length=11
}
printf("\nMatrix is:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
}
printf("\nTranspose Matrix is:\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)
printf("%d\t",a[j][i]);
}
}
Output:
Enter how many rows & columns:3 3
Enter 9 elements:1 2 3 4 5 6 7 8 9
Matrix is:
1 2 3
4 5 6
7 8 9
Transpose Matrix is:
1 4 7
2 5 8
3 6 9
WEEK12:
int main()
{
int a, b, result;
#include<stdio.h>
int A(int m, int n);
main()
{
int m,n;
printf("Enter two numbers :: \n");
scanf("%d%d",&m,&n);
printf("\nOUTPUT :: %d\n",A(m,n));
}
// Recursive call
return n + recSum(n - 1);
}
// Driver code
int main()
{
int n;
printf("enter a value for series of sum\n ");
scanf("%d",&n);
printf("Sum = %d ", recSum(n));
return 0;
}
OUTPUT:
enter a value for series of sum
55
Sum = 1540
WEEK13:
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
char *str1, *str2;
int i;
clrscr();
printf("Enter the string : ");
scanf("%s", str2);
for(i = 0; *str2 != '\0'; i++, str1++, str2++)
*str1 = *str2;
*str1 = '\0';
str1 = str1 - i;
printf("\nThe copied string is : %s", str1);
getch();
}
OUTPUT:
53. Write a C program to count no.of characters, digits and special character in a string.
#include<stdio.h>
int main()
{
char str[100];
int i,a=0,d=0,s=0;
printf("\nEnter The String : ");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
if((str[i]>=65&&str[i]<=90) || (str[i]>=97&&str[i]<=122))
a++;
else if (str[i]>=48&&str[i]<=57)
d++;
else
s++;
}
printf("\nTotal Alphabets : %d",a);
printf("\nTotal Digits : %d",d);
printf("\nTotal Special : %d",s);
return 0;
}
Output
Enter The String : Tutor joes @ 2014
Total Alphabets :9
Total Digits :4
Total Special :5
Week14:
54. Write a C program to write and read text into a file.
Source code:
int main()
char fName[20];
scanf("%s",fName);
fp=fopen(fName,"w");
if(fp==NULL)
putc('A',fp);
putc('B',fp);
putc('C',fp);
printf("\nData written successfully.");
fclose(fp);
fp=fopen(fName,"r");
if(fp==NULL)
exit(0);
printf("%c",getc(fp));
printf("%c",getc(fp));
printf("%c",getc(fp));
fclose(fp);
return 0;
Output
Contents of file is :
ABC
55. Write a c program to write and read text into a binary file using fread() and fwrite()
Source code:
#include<stdio.h>
/* Our structure */
struct record
{
int a,b,c;
};
int main()
int count;
FILE *ptr;
ptr=fopen("test.bin","rb");
if (!ptr)
} fclose(ptr);
return 0;
#include<stdio.h>
/* Our structure */
struct record
int a,b,c;
};
int main()
int count;
FILE *ptr;
if (!ptr)
return 1;
myRecord.a= count;
fclose(ptr);
return 0;
56. write a c program to copy the contents of one file to another file.
Source code:
#include <stdio.h>
int main(){
char filename[100], c;
scanf("%s",filename);
if (fptr1 == NULL){
scanf("%s", filename);
if (fptr2 == NULL){
exit(0);
c = fgetc(fptr1);
while (c != EOF){
fputc(c, fptr2);
c = fgetc(fptr1);
printf("
Contents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
Output:
Source code:
#include <stdio.h>
#include <stdlib.h>
int main()
char c;
exit(0);
fputc(c, fp3);
fputc(c, fp3);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
Output:
58. Write a c program to find no. of lines words and characters in a file.
Source code:
int main()
FILE *fp;
char filename[MAX_FILE_NAME];
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL)
return 0;
}
// Extract characters from file and store in character c
count = count + 1;
else if(c==’ ‘)
word++;
else
character++;
fclose(fp);
return 0;
Output:
102 words
210 characters
#include<stdlib.h>
int main() {
FILE *fp;
char ch;
int num;
long length;
scanf("%d", &num);
fp = fopen("C:\\Users\\acer\\Documents\\file4.txt", "r");
if (fp == NULL) {
exit(1);
fseek(fp, 0, SEEK_END);
length = ftell(fp);
do {
ch = fgetc(fp);
putchar(ch);
fclose(fp);
return(0);
OUTPUT : :