C Programming (Unit 2 - Unit 5 Programs)
C Programming (Unit 2 - Unit 5 Programs)
}
2. Determinant of 3x3 matrix
#include<stdio.h>
int main()
{
int rows, columns, a[3][3];
int x, y, z, Determinant = 0;
return 0;
}
4. Addition of a matrix
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int mat1[5][5], mat2[5][5], i, j, mat3[5][5],m,n;
printf("Enter the no of rows and cols for first matrix:");
scanf("%d %d",&m,&n);
printf("Enter the no of rows and cols for second matrix:");
scanf("%d %d",&p,&q);
if((m==p)&&(n==q))
{
printf("Matrix can be added");
}
else
{
printf("Rows and columns are not equal");
}
printf("Enter matrix 1 elements :");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter matrix 2 elements :");
for(i=0; i<p; i++)
{
for(j=0; j<q; j++)
{
scanf("%d",&mat2[i][j]);
}
}
printf("Adding the two matrix to form the third matrix .....\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf("The two matrix added successfully...!!");
printf("The new matrix will be :\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ",mat3[i][j]);
}
printf("\n");
}
getch();
}
5. Multiplication of a matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,r1,c1,r2,c2;
int sum=0;
clrscr();
printf("Enter number of rows and columns of first matrix (MAX 10)\n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns of second matrix MAX 10)\n");
scanf("%d%d",&r2,&c2);
if(r2==c1)
{
}
else
{
printf("Matrix Multiplication is Not Possible");
}
getch();
}
COMPARISON OF STRINGS:
#include<stdio.h>
#include<conio.h>
void main()
{
char string1[5],string2[5];
int i,temp = 0;
printf("Enter the string1 value: ");
gets(string1);
printf(" Enter the String2 value: ");
gets(string2);
for(i=0; string1[i]!='\0'; i++)
{
if(string1[i] == string2[i])
temp = 1;
else
temp = 0;
}
if(temp == 1)
printf("Both strings are same.");
else
printf("Both strings are not same.");
getch();
}
#include <stdio.h>
#include <string.h>
void main()
{
char a[100], b[100];
printf("Enter a string\n");
gets(a);
printf("Enter a string\n");
gets(b);
if (strcmp(a,b) == 0)
printf("The strings are equal.\n");
else
printf("The strings are not equal.\n");
getch();
}
2.CONCATENATION OF STRINGS
1.USING LIBRARY FUNCTION
#include <stdio.h>
#include <string.h>
void main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a, b);
printf("String obtained on concatenation: %s\n", a);
getch();
}
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
void main()
{
char str1[MAX_SIZE], str2[MAX_SIZE];
int i, j;
/* Input two strings from user */
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
/* Move till the end of str1 */
i=0;
while(str1[i] != '\0')
{
i++;
}
/* Copy str2 to str1 */
j = 0;
while(str2[j] != '\0')
{
str1[i] = str2[j];
i++;
j++;
}
// Make sure that str1 is NULL terminated
str1[i] = '\0';
printf("Concatenated string = %s", str1);
getch();
}
3.COPYING A STRING
#include <stdio.h>
#include<conio.h>
void main()
{
char s1[100], s2[100], i;
printf("Enter string s1: ");
scanf("%s",s1);
for(i = 0; s1[i] != '\0'; ++i)
{
s2[i] = s1[i];
}
s2[i] = '\0';
printf("String s2: %s", s2);
getch(); }
2.USING LIBRARY FUNCTION
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char source[100], destination[1000];
printf("Input a string\n");
gets(source);
strcpy(destination, source);
printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);
getch();
}
SELECTION SORT:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, count, temp, number[25];
printf("How many numbers u are going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
{
scanf("%d",&number[i]);
}
// Logic of selection sort algorithm
for(i=0;i<count;i++)
{
for(j=i+1;j<count;j++)
{
if(number[i]>number[j])
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
printf("Sorted elements: ");
for(i=0;i<count;i++)
{
printf(" %d",number[i]);
}
getch();
}
LINEAR SEARCH
METHOD 1:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10], i, num, n, c=0, pos;
printf("Enter the array size : ");
scanf("%d",&n);
printf("Enter Array Elements : ");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter the number to be search : ");
scanf("%d",&num);
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
printf("Number not found..!!");
}
else
{
printf("%d found at position %d",num, pos);
}
getch();
}
METHOD 2:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter element to search:");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
getch();
}
BINARY SEARCH:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[50],i,n,x,flag=0,first,last,mid;
printf("Enter size of array:");
scanf("%d",&n);
printf("\nEnter array element(ascending order)\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\nEnter the element to search:");
scanf("%d",&x);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(x==arr[mid])
{
flag=1;
break;
}
else
{
if(x>arr[mid])
first=mid+1;
else
last=mid-1;
}
}
if(flag==1)
printf("\nElement found at position %d",mid+1);
else
printf("\nElement not found");
getch();}
UNIT -3
ARRAY OF POINTERS
#include<stdio.h>
#define SIZE 10
int main()
{
int *arrop[3];
int a = 10, b = 20, c = 50, i;
arrop[0] = &a;
arrop[1] = &b;
arrop[2] = &c;
return 0;
}
POINTER TO ARRAYS
#include <stdio.h>
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int*p = &a[0]
for (i = 0; i < 5; i++)
{
printf("%d", *p);
p++;
}
return 0;
}
EXAMPLE 2
#include <stdio.h>
int main () {
return 0;
}
OUTPUT
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
#include <stdio.h>
int main()
scanf("%d", x+i);
sum += *(x+i);
return 0;
Enter 6 numbers: 2
3
4
12
Sum = 29
#include <stdio.h>
int main()
int* ptr;
ptr = &x[2];
return 0;
}
Example: Access Array Elements Using Pointers
#include <stdio.h>
void main()
int data[5], i;
getch();
POINTER ARITHMETIC:
Pointer Operators:
2 Special operators:
1. * (dereference operator) – gives the value at the address
2. & (reference operator) – gives the address of a variable
INCREMENTING A POINTER
#include<stdio.h>
void main()
int number=50;
p=p+1;
getch();
OUTPUT:
Address of p variable is 3214864300
After increment: Address of p variable is 3214864304
DECREMENTING A POINTER
#include <stdio.h>
void main()
int number=50;
p=p-1;
ation
}
OUTPUT
POINTER ADDITION
#include<stdio.h>
void main()
number=50;
getch();
OUTPUT:
POINTER SUBTRACTION:
#include<stdio.h>
int main()
int number=50;
return 0;
OUTPUT :
#include <stdio.h>
int main()
int num=45;
int *ptr1,*ptr2;
ptr1=#
ptr2=ptr1+1;
return 0;
OUTPUT :
sub address 1
POINTER COMPARISONS
#include <stdio.h>
int main ()
int i, *ptr;
ptr = var;
i = 0;
ptr++;
i++;
return 0;
EXAMPLE 2:
#include<stdio.h>
void main()
{
int a=6;
int*p1,*p2;
p1=&a;
p2=&a;
if(p1==p2)
{
printf("The addresses are equal");
}
getch();
}
#include<stdio.h>
void main()
{
char *T;
int I,J,K;
char *ARRAY[5]={"SUNIL","ANIL","DILIP","JAY","BHARAT"};
clrscr();
for(I=0;I<5;I++)
{
printf("%s \t",ARRAY[I]);
}
printf("\n");
for(I=0;I<4;I++)
{
for(J=0;J<4;J++)
{
K=strcmp(ARRAY[J],ARRAY[J+1]);
if(K>0)
{
T=ARRAY[J];
ARRAY[J]=ARRAY[J+1];
ARRAY[J+1]=T;
}
}
}
for(I=0;I<5;I++)
{
printf("%s \t",ARRAY[I]);
}
getch();
}
EXAMPLE 2
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *x[20];
int i,n=0;
void reorder(int n,char *x[]);
clrscr();
printf("Enter no. of String : ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("Enter the Strings %d : ",i+1);
x[i]=(char *)malloc(20*sizeof(char));
scanf("%s",x[i]);
}
reorder(n,x);
printf("\nreorder list is : \n");
for(i=0;i<n;i++)
{
printf("%d %s\n",i+1,x[i]);
}
getch();
}
void reorder(int n,char *x[])
{
int i,j;
char t[20];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(x[i],x[j])>0)
{
strcpy(t,x[j]);
strcpy(x[j],x[i]);
strcpy(x[i],t);
}
}
}
PARAMETER PASSING
#include <stdio.h>
void add(int num);
void main()
{
int num = 5;
printf("\n The value of 'num' before the calling function is = %d", num);
add(num);
printf("\n The value of 'num' after calling the function is = %d", num);
getch();
}
void add(int num)
{
num = num +10;
printf("\n Value of 'num' in the called function is = %d", num);
}
OUTPUT:
OUTPUT:
#include<stdio.h>
#include<conio.h>
void swap(int,int) ; // function declaration
void main()
{
int num1, num2 ;
clrscr() ;
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(num1, num2) ; // calling function
printf("\nAfter swap: num1 = %d,num2 = %d", num1, num2);
getch() ;
}
void swap(int num1, int num2) // called function
{
int temp ;
temp = num1;
num1 = num2;
num2 = temp;
printf("\nIn swap function num1=%d num2=%d",num1,num2);
}
OUTPUT:
#include<stdio.h>
#include<conio.h>
void swap(int*,int*) ; // function declaration
void main()
{
int num1, num2 ;
clrscr() ;
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(&num1,&num2) ; // calling function
printf("\nAfter swap: num1 = %d,num2 = %d", num1, num2);
getch() ;
}
void swap(int *num1, int *num2) // called function
{
int temp ;
temp = *num1;
*num1 = *num2;
*num2 = temp;
printf("\nIn swap function num1=%d num2=%d",*num1,*num2);
}
OUTPUT:
UNIT -4
STRUCTURES
#include <stdio.h>
#include <string.h>
struct student
{
int roll_no;
char name[30];
int phone_number;
};
void main()
{
struct student p1 = {1,"Brown",123443};
p2.roll_no = 2;
strcpy(p2.name,"Sam");
p2.phone_number = 1234567822;
p3.roll_no = 3;
strcpy(p3.name,"Addy");
p3.phone_number = 1234567844;
printf("First Student\n");
printf("Second Student\n");
printf("Third Student\n");
COPYING A STRUCTURE
#include <stdio.h>
#include <string.h>
int main()
{
struct student
{
int roll_no;
char name[30];
int phone_number;
};
struct student p1 = {1,"Brown",123443};
struct student p2;
p2 = p1;
printf("roll_no : %d\n", p2.roll_no);
printf("name : %s\n", p2.name);
printf("phone_number : %d\n", p2.phone_number);
return 0;
}
ARRAY OF STRUCTURES
#include <stdio.h>
struct student
{
int roll_no;
char name[30];
int phone_number;
};
void main()
{
struct student stud[5];
int i;
for(i=0; i<4; i++)
{
printf("Student %d\n",i+1);
printf("Enter roll no. :\n");
scanf("%d", &stud[i].roll_no);
printf("Enter name :\n");
scanf("%s",stud[i].name);
printf("Enter phone number :\n");
scanf("%d", &stud[i].phone_number);
}
for(i=0; i<4; i++)
{
printf("Student %d\n",i+1);
printf("Roll no. : %d\n", stud[i].roll_no);
printf("Name : %s\n", stud[i].name);
printf("Phone no. : %d\n", stud[i].phone_number);
}
getch();
}
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;
void main()
{
printf("Enter information:\n");
scanf("%s", s.name);
scanf("%d", &s.roll);
scanf("%f", &s.marks);
printf("Displaying Information:\n");
printf("Name: ");
puts(s.name);
getch();
OUTPUT:
Enter information:
Displaying Information:
Name: Jack
Roll number: 23
Marks: 34.
NESTED STRUCTURES
struct structure1
{
----------
----------
};
struct structure2
{
----------
----------
struct structure1 obj1;
};
EXAMPLE: 1
#include<stdio.h>
struct person
{
char name[20];
int age;
char dob[10];
};
struct student
{
struct person info;
int roll_no;
float marks;
};
void main()
{
struct student s1;
scanf("%s", s1.info.name);
scanf("%s", s1.info.dob);
scanf("%d", &s1.roll_no);
scanf("%f", &s1.marks);
printf("\n*******************************\n\n");
getch();
OUTPUT
EXAMPLE :
#include<stdio.h>
struct Address
{
char HouseNo[25];
char City[25];
char PinCode[25];
};
struct Employee
{
int Id;
char Name[25];
float Salary;
struct Address Add;
};
void main()
{
int i;
struct Employee E;
printf("\nDetails of Employees");
printf("\n\tEmployee Id : %d",E.Id);
printf("\n\tEmployee Name : %s",E.Name);
printf("\n\tEmployee Salary : %f",E.Salary);
printf("\n\tEmployee House No : %s",E.Add.HouseNo);
printf("\n\tEmployee City : %s",E.Add.City);
printf("\n\tEmployee House No : %s",E.Add.PinCode);
Output :
Details of Employees
Employee Id : 101
EMBEDDED STRUCTURES
#include <stdio.h>
#include <string.h>
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
} doj;
} e1;
void main( )
{
//storing employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;
#include <stdio.h>
int main()
{
int x[4];
int i;
return 0;
}
When you run the program, the output will be something like:
&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
EXAMPLE 1
#include <stdio.h>
struct Book
{
char bname[10];
int price;
char author[15];
};
void main()
{
struct Book a ={"c program",170,"Reema Thareja"}; //Single structure
variable
struct Book* ptr; //Pointer of Structure type
ptr = &a;
printf("\nBOOK NAME:\t%s",ptr->bname);
printf("\nPRICE:\t%d",a.price);
printf("\nAUTHOR:\t%s",(*ptr).author);
getch();
}
EXAMPLE 2 :
#include <stdio.h>
struct student
{
char id[15]; // student structure
char firstname[64];
char lastname[64];
float points;
};
void main()
{
ptr = &std;
scanf("%s", ptr->id);
scanf("%s", ptr->firstname);
scanf("%s", ptr->lastname);
scanf("%f", &ptr->points);
getch();
}
OUTPUT
#include <stdio.h>
#include <stdlib.h>
int main()
{
#include <stdio.h>
#include <stdlib.h>
int main()
{
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
}
else
{
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
}
else
{
free(ptr);
}
return 0;
}
Declaration of structure:
struct book
{
char title[20];
char publisher[20];
char author[20];
int year;
int pages;
};
Typedef structure in C
SYNTAX
Example:
Method 1:
Book b1,b2;
Method 2:
struct book
{
char title[20];
char publisher[20];
char author[20];
int year;
int pages;
};
typedef struct book Book;
Example 2:
typedef struct Record
{
char ename[30];
int ssn;
int deptno;
}employee;
employee e1,e2;
PROGRAM:
#include <stdio.h>
//structure declaration
struct employee
{
char name[100];
int age;
};
int main()
{
//declare structure variable
EMP employee1;
UNIT -5
File Operations
File Mode Meaning of Mode
r Open for reading.
rb Open for reading in binary mode.
w Open for writing.
wb Open for writing in binary mode.
a Open for append. i.e, Data is added to end of file.
ab Open for append in binary mode. i.e, Data is added to end of file.
r+ Open for both reading and writing.
rb+ Open for both reading and writing in binary mode.
w+ Open for both reading and writing.
wb+ Open for both reading and writing in binary mode.
a+ Open for both reading and appending.
ab+ Open for both reading and appending in binary mode.
In C, you can perform four major operations on the file, either text or binary:
Opening a file is performed using the library function in the "stdio.h" header file:
fopen().
The syntax for opening a file in standard I/O is:
ptr = fopen("fileopen","mode")
For Example:
fopen("E:\\cprogram\\newprogram.txt","w");
fopen("E:\\cprogram\\oldprogram.bin","rb");
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
fptr = fopen("C:\\program.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
fscanf(fptr,"%d", &num);
return 0;
}