Você está na página 1de 37

Practical-1:

Aim: write a program to print HELLO FRIENDS.


#include<stdio.h>
#include<conio.h>
void main()
{
printf("HELLO FRIENDS...!!!");
getch();
}
Output:

Practical-2:
Aim: write a program that reads two numbers from keyboard and gives their addition,s
ubtraction, multiplication, division and modulo.
#include<stdio.h>
#include<conio.h>
#include<float.h>
#include<math.h>
void main()
{
int a,b,c,d,e,f;
float j;
clrscr();
printf("value of a:");
scanf("%d",&a);
printf("value of b:");
scanf("%d",&b);
c=a+b;
d=a-b;
e=a*b;
f=(a%b);
j=a/(float)b;
printf("\n addition of a & b is:");
printf("%d",c);
printf("\n subtraction of a & b is:");
printf("%d",d);
printf("\n multiplication of a & b is:");
printf("%d",e);
printf("\n modulo of a & b is:");
printf("%d",f);
printf("\n division of a & b is:");
printf("%f",j);
getch();
}
Output:

Practical-3:
Aim: write a program to convert days into months and days.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int d,m,rd;
clrscr();
printf("enter no. of days:");
scanf("%d",&d);
printf("month is:");
m=d/30;
rd=d%30;
printf("month is:%d\n days are: %d",m,rd);
getch();
}
Output:

Practical-4:
Aim: write a program to select and print the largest of the three numbers using nested if else
statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int a ,b,c,max;
clrscr();
printf("enter value of a:");
scanf("%d",&a);
printf("enter value of b:");
scanf("%d",&b);
printf("enter value of c:");
scanf("%d",&c);
if(a>b)
{
if(a>c)
{
printf("a=%d is maximum..",a);
}
else
{
printf("c=%d is maximum..",c);
}
}
else
{
if(b>c)
{
printf("b=%d is maximum..",b);
}
else
{
printf("c=%d is maximum..",c);
}
}
getch();
}

Output:

Practical-5
Aim: write a program to display multiplication table.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("enter value of n:");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("%d * %d=%d\n",n,i,n*i);
}
getch();
}
Output:

Practical-6
Aim: write a program to print 1+1/2+1/3+1/4+..+1/n series.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("enter value of n:");
scanf("%d",&n);
printf("1+");
for(i=2;i<=n;i++)
{
if(i!=n)
{
printf("1/%d+",i);
}
else
{
printf("1/%d",i);
}
}
getch();
}
Output:

Practical-7
Aim: write a program to print following patterns
a)
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("value of n:");
scanf("%d",&n);
printf("pattern is:\n");
for(i=1;i<n;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("* ");
}
}
getch();
}
Output:

b)
1
2
3
4
5

2
3
4
5

3
4
5

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("value of n:");
scanf("%d",&n);
printf("pattern is:\n");
for(i=1;i<=5;i++)
{
printf("\n");
for(j=i;j<=n;j++)
{
printf("%d ",j);
}
}
getch();
}
Output:

4
5

c)
A
B
C
D
E

A
B
C
D

A
B
C

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("pattern is:\n");
for(i=65;i<=69;i++)
{
printf("\n");
for(j=i;j<=69;j++)
{
printf("%c ",i);
}
}
getch();
}
Output:

d)

A
B

1
0
1
0

1
0
1

1
0

}
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l,sym;
clrscr();
for(i=1;i<5;i++)
{
printf("\n");
if((i%2)!=0)
{
sym=1;
}
else
{
sym=0;
}
for(j=1;j<=i;j++)
{
if(sym==1)
{
printf("%d\t",sym);
sym=0;
}
else
{
printf("%d\t",sym);
sym=1;
}

Output:

Practical-8

Aim: write a program to print Fibonacci series, 1,1,2,3,5,,n


#include<stdio.h>
#include<conio.h>
main()
{
int n, first = 0, second = 1, next, c;
clrscr();
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
getch();
}
Output:

Practical-9

Aim: write a program to reverse the digit.


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
printf("enter number:");
scanf("%d",&i);
while(i)
{
j=i%10;
i=i/10;
printf("%d",j);
}
getch();
}
Output:

Practical-10
Aim: write a program to add, subtract and multiply two numbers using switch statement.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l,m,ch;
clrscr();
printf("enter value of i and j:");
scanf("%d %d",&i,&j);
printf("nenter your choice:\n1.addition\n2.subtraction\n3.multiplication");
scanf("%d",&ch);
switch(ch)
{
case 1:
k=i+j;
printf("\naddition is:%d",k);
break;
case 2:
l=i-j;
printf("subtraction is:%d",l);
break;
case 3:
m=i*j;
printf("multiplication is:%d",m);
break;
}
getch();
Output:

Practical-11
Aim: write a program to add and multiply two matrices.

a) addition
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
clrscr();
printf("enter matrix a:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\nenter matrix b:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
}
printf("\naddition of two matrix:\n");
for(i=0;i<3;i++)
{

for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}
Output:

b) Multiplication

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
clrscr();
printf("enter matrix a:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\nenter matrix b:");
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
scanf("%d",&b[j][k]);
}
}
for(j=0;j<3;j++)
{
printf("\n");
for(k=0;k<3;k++)
{
printf("%d\t",b[j][k]);
}
}
printf("\nmultiplication of two matrix:\n");
for(i=0;i<3;i++)
{
for(k=0;k<3;k++)
{ c[i][k]=0;
for(j=0;j<3;j++)

{
c[i][k]+=a[i][j]*b[j][k];
}
}
}
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}
Output:

Practical-12
Aim: write a program to sort given number in ascending order.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,temp;
clrscr();
printf("enter array elements:");
for(i=0;i<7;i++)
{
scanf("%d",&a[i]);
}
printf("\nelements are:");
for(i=0;i<7;i++)
{
printf("%d\n",a[i]);
}
for(i=0;i<7;i++)
{
for(j=i+1;j<7;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nelements in ascending order:\n");
for(i=0;i<7;i++)
{
printf("%d\n",a[i]);
}
getch();
}

Output:

Practical-13
Aim: write a program which describes use of string functions.

a)strlen()
#include<stdio.h>
#include<string.h>
void main()
{
int i=0,j=1;
char str[30];
clrscr();
printf("enter string:");
gets(str);
puts(str);
j=strlen(str);
printf("length of string is:%d",j);
getch();
}
Output:

b) strcpy()
#include <stdio.h>

#include <string.h>
void main()
{
char string1[20],string2[10];
clrscr();
printf("enter string1:");
gets(string1);
printf("\n enter string2:");
gets(string2);
strcpy(string1,string2);
printf("\nafter copy string1 is:%s",string1);
getch();
}
Output:

c) tolower()

#include<stdio.h>
#include<ctype.h>
void main()
{
int i;
char str[10],str1[10];
clrscr();
printf("enter string:");
gets(str);
for(i=0;i<strlen(str);i++)
{
str1[i]=tolower(str[i]);
}
puts(str1);
getch();
}
Output:

Practical-14
Aim: write a program to find factorial using recursion.

#include<stdio.h>
#include<conio.h>
int fact=1;
void main()
{
int a,n;
clrscr();
printf("enter number:");
scanf("%d",&n);
factn(n);
printf("factorial is:%d",fact);
getch();
}
int factn(int n)
{
if(n==1)
{
return 1;
}
else
{
fact=n*factn(n-1);
}
return(fact);
}

Output:

Practical-15
Aim: write a function prime that return 1 if its argument is prime and return 0 otherwise.

#include<stdio.h>
#include<conio.h>
int count=0,i,a,n;
void main()
{
clrscr();
printf("enter number:");
scanf("%d",&n);
prime(n);
printf("%d",a);
getch();
}
int prime(int n)
{
for(i=2;i<=(n/2);i++)
{
if((n%i)==0)
{
count=1;
break;
}
}
if(count==1)
{
a=0;
return(a);
}
else
{
a=1;
return(a);
}
}

Output:

Practical-16

Aim: Define a structure ,personal,that would contain person name,date od joining and salary.
Using this structure write a program to read this information for one person from the keyboard
and print the same on the screen.
#include<stdio.h>
#include<conio.h>
struct personal
{
char name[30];
char join[15];
double salary;
};
void main()
{
int a;
struct personal p1;
clrscr();
printf("enter name,date of joining and birthdate:");
scanf("%s %s %lf",p1.name,p1.join,&p1.salary);
printf("\nname:%s \ndate of joining:%s \nsalary:%lf",p1.name,p1.join,p1.salary);
getch();
}
Output:

Practical-17:

Aim:write a program using pointer to compare two strings.


#include<stdio.h>
int compare_string(char*, char*);
main( )
{
char first[100], second[100], result;
clrscr();
printf("Enter first string\n");
gets(first);
printf("Enter second string\n");
gets(second);
result = compare_string(first, second);
if ( result == 0 )
printf("Both strings are same.\n");
else
printf("Entered strings are not equal.\n");
return 0;
getch();
}
int compare_string(char *first, char *second)
{
while(*first==*second)
{
if ( *first == '\0' || *second == '\0' )
break;
first++;
second++;
}
if( *first == '\0' && *second == '\0' )
return 0;
else
return -1;
}

Output:

Practical-18:

Aim:write a program using pointer to read an aaray of integer and print element in reverse order.
#include<stdio.h>
#include<conio.h>
#define MAX 30
void main()
{
int size,i,arr[MAX];
int *ptr;
clrscr();
ptr=&arr[0];
printf("Enter the size of array : ");
scanf("%d",&size);
printf("\nEnterintegers into array:");
for(i=0;i<size;i++)
{
scanf("%d",ptr);
ptr++;
}
ptr=&arr[size-1];
printf("\nElements of array in reverse order are:");
for(i=size-1;i>=0;i--)
{
printf("\n%d",*ptr);
ptr--;
}
getch();
}
Output:

Practical-19

Aim: Explain various file accessing functions.

fopen:
Decalartion:
FILE *fopen( const char

*filename, const char

*mode );

Description: Opens a file indicated by filename and returns a file stream


associated with that file. mode is used to determine the file access mode.
Explanation:
"r"
r+
w
w++

--->
--->
--->
--->

Open a file for reading


Open a file for read/write
Create a file for writing
Create a file for read/write

fclose:
Decalartion:
int fclose( FILE *stream );
Description: Closes the given file stream.

fprintf:
Declaration: int fprintf( FILE

*stream, const char

*format, ... );

Description: Loads the data from the given locations, converts them to character
string equivalents and writes the results to a variety of sinks.

fscanf:
Declaration:
int fscanf( FILE

*stream, const char

*format, ... );

Description: Reads data from the a variety of sources, interprets it according to


format and stores the results into given locations.

fgets:
Declaration: char *fgets( char

*str, int count, FILE

*stream );

Description: Reads at most count - 1 characters from the given file stream and stores
them in str.

fputc:
Declaration:
int fputc( int ch, FILE *stream );
Description: Writes a character ch to the given output stream stream.

Practical:20 Write a Program to open and close file using


library function.
#include<stdio.h>
int main()
{
FILE *ptr_file;
int x;
ptr_file =fopen("output.txt", "w"); //any file name
if (!ptr_file)
return 1;
for (x=1; x<=10; x++)
fprintf(ptr_file,"%d\n", x);
fclose(ptr_file);
return 0;
}
Output:

Practical :21 Write a Program to read contain of file using


fscanf and fgets.
#include <stdio.h>
main()
{
FILE *fp;
char buff[255];
fp = fopen("test.txt", "r");
fscanf(fp, "%s", buff);
printf("1 : %s\n", buff );
fgets(buff, 255, (FILE*)fp);
printf("2: %s\n", buff );
fgets(buff, 255, (FILE*)fp);
printf("3: %s\n", buff );
fclose(fp);
}

/*call to previous Programe*/

Practical 22 : Write a Program to write contain in file using


fprintf and fputs
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fh;
fh=fopen("hello.txt","w");
if(fh==NULL)
{
puts("Can't open that file!");
exit(1);
}
fprintf(fh,"Look what I made!\n");
fclose(fh);
return(0);
}
Output:

Você também pode gostar