You are on page 1of 25

PROGRAMS TO UNDERSTAND DATA TYPES/INPUT/OUTPUT

FUNCTIONS
1a)Program to understand basic data types
#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d
\n",sizeof(a));
printf("Storage size for char data type:%d
\n",sizeof(b));
printf("Storage size for float data type:%d
\n",sizeof(c));
printf("Storage size for double data
type:%d\n",sizeof(d));
return 0;
}
Output:
Storage size for int data type:4
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8

1b) program to understand input-output functions

UNDERSTAND PRINTF

#include <stdio.h>
main()
{
intdec = 5;
charstr[] = "abc";
charch = 's';
float pi = 3.14;

printf("%d %s %f %c\n", dec, str, pi, ch);


}

Output:

abc 3.140000 c

UNDERSTAND SCANF

#include <stdio.h>

main()
{
int x;
intargs;

printf("Enter an integer: ");


if (( args = scanf("%d", &x)) == 0) {
printf("Error: not an integer\n");
} else {
printf("Read in %d\n", x);
}

Output

Enter an integer: 20
Read in 20
PROBLEM SOLVING USING DECISION MAKING AND
LOOPING STATEMENTS

EX: NO: 02 (A) Odd And Even Number

#include<stdio.h>
main()
{
inta,rem;
printf("Enter a Number\n");
scanf("%d",&a);
rem=a%2;
if(rem==0)
printf("The Given Number is Even");
else
printf("The Given Number is Odd");
}

Output:

Enter a Number
13
The Given Number is Odd
EX: NO: 02 (b) Biggest of Three Numbers

{
#include<stdio.h> printf("The Second Number %d(b) is
main() } Biggest\n",b);
else
{ printf("The Third Number %d(c) is
inta,b,c; Biggest\n",c);
printf("Enter 3 Numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("The First Number %d(a) is Biggest\n",a);
}
}
else if(b>c)
}

Output:

Enter 3 Numbers
5
9
2
The Second Number 89(b) is Biggest

EX: NO: 02 (c) Sum of N natural Numbers

#include<stdio.h>
main()
{
inti,n,sum=0;
printf("Enter the range\n");
scanf("%d",&n);
i=1;
while(i<=n)
{
sum=sum+i;
i++;
}
printf("\nThe sum of first %d numbers is %d\n",n,sum);
}

Output:
Enter the range
16
The sum of first 16 numbers is 136

EX: NO: 02 (D) Sum of Digits

#include<stdio.h>
main()
{
intn,i,sum=0;
printf("Enter a Number\n");
scanf("%d",&n);
do
{
i=n%10;
sum=sum+i;
n=n/10;
}while(n>0);
printf("The Sum of digit is %d\n",sum);
}
Output:

Enter a Number
5891
The Sum of digit is 23

PROGRAMS ON FUNCTIONS

EX: NO: 03 (A) AREA OF TRAINGLE USING FUNCTIONS

#include<stdio.h>
#include<math.h>
float area()
{
inta,b,c;
floats,ar;
printf("Enter 3 Sides\n");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
returnar;
}
main()
{
float a;
a=area();
printf("The Area of Triangle is %f\n",a);
}
Output:
Enter 3 Sides
12
8
7
The Area of Triangle is 19.748418
EX: NO: 03 (B) FACTORIAL

#include<stdio.h>
int factorial(int n)
{
if(n==0 || n==1)
return 1;
else
return n*factorial(n-1);
}
main()
{
int n;
printf("\nEnter a Number\n");
scanf("%d",&n);
printf("\nThe factorial of %d is %d\n",n,factorial(n));
}

OUTPUT:

Enter a Number
6
The factorial of 6 is 720

PROGRAMS ON ARRAYS
EX: NO: 04(A) PROGRAM TO CALCULATE AVERAGE USING
ARRAY

#include <stdio.h>

int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int sum, loop;
floatavg;

sum = avg = 0;

for(loop = 0; loop < 10; loop++) {


sum = sum + array[loop];
}

avg = (float)sum / loop;

printf("Average of array values is %.2f", avg);

return 0;
}

OUTPUT:
Average of array values is 4.50

EX: NO: 04(B) MATRIX MULTIPLICATION USING ARRAYS

#include<stdio.h>
int main(){
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
printf("\nEnter the row and column of first matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second matrix");
scanf("%d %d",&o,&p);
if(n!=o){
printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second
matrix");
}
else{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",a[i][j]);
}
}
printf("\nThe Second matrix is\n") ;
for(i=0;i<o;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++){ //row of first matrix
for(j=0;j<p;j++){ //column of second matrix
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("\nThe multiplication of two matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",c[i][j]);
}
}
return 0;
}

OUTPUT :

Enter the value of the first matrix:


23
34
Enter the value of the second matrix:
34
45
Product of the two matrices is
18 23
25 32

PROGRAMS ON STRING MANIPULATION

EX: NO: 05(A) STRING CONCATENATION

#include<stdio.h>
#include<string.h>
main()
{
char s[20],s1[20];
printf("Enter a String1\n");
scanf("%s",s);
printf("Enter a String2\n");
scanf("%s",s1);
strcat(s,s1);
printf("The Concatenated String is %s\n",s);
}

OUTPUT:

Enter a String1
hai
Enter a String2
hello
The Concatenated String is haihello
EX: NO: 05(B) STRING PALINDROME CHECKING

#include<stdio.h>
#include<string.h>
main()
{
char s[20],s1[20];
printf("Enter a String\n");
scanf("%s",s);
strcpy(s1,s);
if(strcmp(s,s1)==0)
printf("The Given String is Palindrome\n");
else
printf("The Given String is Not Palindrome\n");
}

OUTPUT:

Enter a String
madam
The Given String is Palindrome

PROGRAMS USING STRUCTURE AND UNIONS

EX: NO: 06(A) STUDENT RECORD

#include<stdio.h>
struct student
{
int rno,m1,m2,m3;
floatavg;
char name[20],dept[10];
};
main()
{
struct student s;
printf("Enter the Student Details:\n");
printf("Enter the Stuent roll no:\n");
scanf("%d",&s.rno);
printf("Enter the Stuent Name:\n");
scanf("%s",&s.name);
printf("Enter the StuentDept:\n");
scanf("%s",&s.dept);
printf("Enter the 3 marks:\n");
scanf("%d%d%d",&s.m1,&s.m2,&s.m3);
s.avg=(s.m1+s.m2+s.m3)/3;
printf("The Student Average is :%f\n",s.avg);
}
OUTPUT :

Enter the Student Details:


Enter the Stuent roll no:
12
Enter the Stuent Name:
Kumar
Enter the StuentDept:
CSE
Enter the Stuent marks:
40
18
90
The Student Average is :49.000000
EX: NO: 06(B) STORE BOOK INFORMATION USING UNION

#include<stdio.h>
union book
{
int price;
charbname[20];
};

main()
{
union book b;
printf("Enter the Book Details:\n");
printf("Enter the Book Name:\n");
scanf("%s",&b.bname);
printf("Enter the Book Price:\n");
scanf("%d",&b.price);
printf("BOOK DETAILS:\n");
printf("%s\t%d\n",b.bname,b.price);
printf("Enter the Book Name:\n");
scanf("%s",b.bname);
printf("Book Name=%s\n",b.bname);
}

OUTPUT:

Enter the Book Details:


Enter the Book Name:
English
Enter the Book Price:
150
BOOK DETAILS:
û150
Enter the Book Name:
English
Book Name=English

PROGRAMS ON POINTERS

EX: NO: 07 SWAPPING OF TWO NUMBERS USING


POINTERS

#include <stdio.h>
void swap(int *a,int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}

int main()
{
int num1,num2;
printf("Enter value of num1: ");
scanf("%d",&num1);
printf("Enter value of num2: ");
scanf("%d",&num2);
printf("Before Swapping: num1=%d, num2=%d\n",num1,num2);
swap(&num1,&num2);
printf("After Swapping: num1=%d, num2=%d\n",num1,num2);
return 0;
}
OUTPUT :

Enter value of num1: 10

Enter value of num2: 20

Before Swapping: num1=10, num2=20

After Swapping: num1=20, num2=10

PROGRAMS ON COMMAND LINE ARGUMENTS

EX: NO: 08 PROGRAM FOR ARGC() AND ARGV()


FUNCTIONS IN C:

#include <stdio.h>
#include <stdlib.hc
>`

int main(intargc, char *argv[]) // command line


arguments/////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
{
if(argc!=5)
{
printf("Arguments passed through command line " \
"not equal to 5");
return 1;
}

printf("\n Program name : %s \n", argv[0]);


printf("1st arg : %s \n", argv[1]);
printf("2nd arg : %s \n", argv[2]);
printf("3rd arg : %s \n", argv[3]);
printf("4th arg : %s \n", argv[4]);
printf("5th arg : %s \n", argv[5]);

return 0;
}
OUTPUT:
Program name : test
1st arg : this
2nd arg : is
3rd arg : a
4th arg : program
5th arg : (null)

PROGRAMS USING DYNAMIC MEMORY ALLOCATION


EX: NO: 09 PROGRAM TO READ AND PRINT THE STUDENT
DETAILS USING STRUCTURE AND DYNAMIC MEMORY
ALLOCATION.

#include <stdio.h>
#include <stdlib.h>

/*structure declaration*/
struct student
{
char name[30];
int roll;
floatperc;
};

int main()
{
struct student *pstd;

/*Allocate memory dynamically*/


pstd=(struct student*)malloc(1*sizeof(struct student));

if(pstd==NULL)
{
printf("Insufficient Memory, Exiting... \n");
return 0;
}

/*read and print details*/


printf("Enter name: ");
gets(pstd->name);
printf("Enter roll number: ");
scanf("%d",&pstd->roll);
printf("Enter percentage: ");
scanf("%f",&pstd->perc);

printf("\nEntered details are:\n");


printf("Name: %s, Roll Number: %d, Percentage: %.2f\n",pstd-
>name,pstd->roll,pstd->perc);

return 0;
}
OUTPUT:

Enter name: Mike


Enter roll number: 1
Enter percentage: 87.50

Entered details are:


Name: Mike, Roll Number: 1, Percentage: 87.50

PROGRAM TO IMPLEMENT RANDOM ACCESS IN FILES

EX: NO: 10 C PROGRAM TO READ NAME AND MARKS OF N


NUMBER OF STUDENTS FROM USER AND STORE THEM IN
A FILE

#include<stdio.h>
int main()
{
char name[50];
int marks, i, num;

printf("Enter number of students: ");


scanf("%d", &num);

FILE *fptr;
fptr = (fopen("C:\\student.txt", "w"));
if(fptr == NULL)
{
printf("Error!");
exit(1);
}

for(i = 0; i<num; ++i)


{
printf("For student%d\nEnter name: ", i+1);
scanf("%s", name);

printf("Enter marks: ");


scanf("%d", &marks);

fprintf(fptr,"\nName: %s \nMarks=%d \n", name, marks);


}

fclose(fptr);
return0;
}

PROGRAMS TO IMPLEMENT MATH FUNCTIONS

EX: NO: 10 PROGRAMS TO FIND MATH.H FUNCTIONS IN


MATH.HEADER FILE
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(){
clrscr();
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%d",abs(-12));
getch();
}

OUTPUT :
4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12

PROGRAMS TO IMPLEMENT SORTING

EX: NO: 11 TO IMPLEMENT INSERTION SORT

#include <stdio.h>
#define MAX 7
voidinsertion_sort(int*);

void main()
{
int a[MAX],i;

printf("enter elements to be sorted:");


for(i=0;i <MAX;i++)
{
scanf("%d",&a[i]);
}
insertion_sort(a);
printf("sorted elements:\n");
for(i=0;i < MAX;i++)
{
printf(" %d", a[i]);
}
}

voidinsertion_sort(int* x)
{
int temp,i, j;

for(i=1;i <MAX;i++)
{
temp= x[i];
j =i-1;
while(temp < x[j]&& j >=0)
{
x[j +1]= x[j];
j = j -1;
}
x[j +1]= temp;
}
}

OUTPUT:

enter elements to be sorted:8249361


sorted elements:
1234689

PROGRAM TO IMPLEMENT SEARCHING

EX NO:12 PROGRAM TO IMPLEMENT LINEAR SEARCH

#include <stdio.h>

Voidmain()
{
int array[10];
inti,num,keynum, found =0;

printf("Enter the value of num\n");


scanf("%d",&num);
printf("Enter the elements one by one \n");
for(i=0;i<num;i++)
{
scanf("%d",&array[i]);
}
printf("Input array is \n");
for(i=0;i<num;i++)
{
printf("%dn", array[i]);
}
printf("Enter the element to be searched \n");
scanf("%d",&keynum);
/* Linear search begins */
for(i=0;i<num;i++)
{
if(keynum== array[i])
{
found=1;
break;
}
}
if(found ==1)
printf("Element is present in the array\n");
else
printf("Element is not present in the array\n");
}

OUTPUT:

Enter the value of num


5
Enter the elements one by one
456
78
90
40
100
Input array is
456
78
90
40
100
Enter the element to be searched
70
Element is not present in the array

You might also like