Escolar Documentos
Profissional Documentos
Cultura Documentos
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
Practical-9
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:
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
fopen:
Decalartion:
FILE *fopen( const char
*mode );
--->
--->
--->
--->
fclose:
Decalartion:
int fclose( FILE *stream );
Description: Closes the given file stream.
fprintf:
Declaration: int fprintf( FILE
*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
*format, ... );
fgets:
Declaration: char *fgets( char
*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.