Practical no.1

Write a C program to display hexadecimal decimal octal format of the
entered numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
intnum;
clrscr();
printf("Enter the number : ");
scanf("%d",&num);
printf("nDecimal format : %d",num);
printf("nHexaDecimal format : %x",num);
printf("nOctal format : %o",num);
getch();
}




OUTPUT:

Enter the number:12

Decimal format :12

Hexadecimal format:c

Octal format:14
Practical no.2

To demonstrate all possible formatting specifiers.

Format Specifiers for C
%c      The character format specifier.
%d      The integer format specifier.
%i      The integer format specifier (same as %d).
%f      The floating-point format specifier.
%e      The scientific notation format specifier.
%E      The scientific notation format specifier.
%g      Uses %f or %e, whichever result is shorter.
%G      Uses %f or %E, whichever result is shorter.
%o      The unsigned octal format specifier.
%s      The string format specifier.
%u      The unsigned integer format specifier.
%x      The unsigned hexadecimal format specifier.
%X      The unsigned hexadecimal format specifier.
%p      Displays the corresponding argument that is a pointer.
%n      Records the number of characters written so far.
%%      Outputs a percent sign.

printf statements is called an escape sequence:
     n (newline)
     t (tab)
     v (vertical tab)
     f (new page)
     b (backspace)
     r (carriage return)
     n (newline)
Practical no.3

Write a program to find the largest and the smallest from the given
three integers.
#include<stdio.h>

#include<conio.h>

void main()

{

int num1, num2, num3;

clrscr();

printf("Enter three numbers = ");

scanf("%d %d %d", &num1, &num2, &num3);

printf("nnThe largest number is ");

if( (num1 > num2) && (num1 > num3) )

printf("%d", num1);

else if(num2 > num3)

printf("%d", num2);

else

printf("%d", num3);

printf("nnThe Smallest number is ");

if( (num1 < num2) && (num1 < num3) )

printf("%d", num1);

else if(num2 < num3)
printf("%d", num2);

else

printf("%d",num3);

getch();

}




OUTPUT:

Enter three numbers: 12

15

67

The largest no. is :67

The smallest no.is :12
Practical no.4
Write a C program to find even or odd numbers.
#include<stdio.h>
main()
{

intnum;

printf ("Enter a number to be checked for even/odd: ");
scanf ("%d",&num);

if (num%2==0)
{
printf ("The entered number is EVEN.n");

}
else
{
printf ("The entered number is ODD.n");

}
}




OUTPUT:

Enter a number to be checked for even/odd:15

The entered number is ODD.
Practical no.5
Write a C program to display menu 1.Addition 2.Subtraction
3.Multiplication 4.Division and execute it using switch case.
#include<stdio.h>
#include<conio.h>
void main()
{
inta,b,c;
int menu;
printf("choose the arithmetic optionn");
scanf("%d",&menu);
switch(menu)
{
case 1:
printf("Enter The Two Numbers:n");
scanf("%d%d",&a,&b);
c=a+b;
printf("The addition of two numbers %dn",c);
break;

case 2:
printf("Enter THE TWO NUMBERS:n");
scanf("%d%d",&a,&b);
c=a-b;
printf("The subtraction of two numbers %dn",c);
break;

case 3:

printf("Enter THE TWO NUMBERS:n");
scanf("%d%d",&a,&b);
c=a*b;
printf("The multiplication of two numbers %dn",c);
break;

case 4:

printf("Enter THE TWO NUMBERS:n");
scanf("%d%d",&a,&b);
c=a/b;
printf("The division of two numbers %dn",c);
break;
}
getch();
}




OUTPUT:

choose the arithmetic option

2

Enter THE TWO NUMBERS:

15 6

The subtraction of two numbers :9
Practical no.6
Write a c program to perform addition all numbers from 1 to 100.
#include<stdio.h>
#include<conio.h>

void main()

{

int i=0,sum=0;

clrscr();

while(i<100)

{

sum=sum+i;

i++;

}

printf("Addition of all numbers from 1-100=%d",sum);

getch();

}




OUTPUT:

Addition of all numbers from 1-100=4950
Practical no.7

Write a C program to find smallest and largest number from array
elements.
#include<stdio.h>
#include<conio.h>
Void main()

{

       int a[20],min,max;

       intn,i;

clrscr();

       printf("enter the num of elementst:");

       scanf("%d",&n);



       printf("enter the elementsn");

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

       {

                 scanf("%d",&a[i]);

                 if(i==0)

                 {

                       min=max=a[i];



                 }

                 if(a[i]<min)

                       min=a[i];

                 else if(a[i]>max)
max=a[i];

       }

       printf("largest element is %d n Smallest element is %d ",max,min);

getch();

}




OUTPUT:

enter the num of elements:5

enter the element:12

56

23

10

78

largest element is 78

Smallest element is 10
Practical no.8
Write a C program to enter elements for 3*3matrix and display them.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf("ENTER ELEMENTSn");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("THE ELEMENTS IN 3*3 MATRIX FORM ISn");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",a[i][j]);
}
printf("n");
}
getch();
}




OUTPUT:

ENTER ELEMENTS
1
2
3
4
5
6
7
8
9
THE ELEMENTS IN 3*3 MATRIX FORM IS
1      2      3
4      5      6
7      8      9
Practical no.9
Write a C program to demonstrate output of standard library
functions. Strlent( ),strcpy( ),strcat( ),strcmp( ).
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10],s2[10],s3[30];
int a;
clrscr();
printf("enter the two stringsn");
scanf("%s%s",&s1,&s2);
printf("1st String=%sn2ndString=%s",s1,s2);
if(strcmpi(s1,s2) == 0 )
printf("Entered strings are equal.n");
else
printf("Entered strings are not equal.n");

a=strlen(s1);
printf("nLength=%d",a);
strcat(s1,s2);
printf("nconcatinated string=%s",s1);
strcpy(s1,s2);
printf("ncopied string=%s",s1);

getch();
}
OUTPUT:

enter the two strings
Good
Morning
1st String=Good
2ndString=Morning
Entered strings are not equal.
Length=4
concatinated string=GoodMorning
copied string=Morning
Practical no.10
Write a C program to calculatefactorial of any number using
recursion.
#include<stdio.h>
#include<conio.h>
int fact(int );
int main()
{
  intnum,res;
  printf("Enter any number: ");
  scanf("%d", &num);
  res = fact(num);
  printf("Factorial value is: %d",res);
  getch();
  return 0;
}
int fact(int n)
{
  int f;
  if(n==1)
    return(1);
  else
    f = n*fact(n-1);
  return(f);
}



OUTPUT:

Enter any number:5

Factorial value is:120
Practical no.11
Write a C program to demonstrate call by value and call by reference.
//CALL BY VALUE
#include<stdio.h>
#include<conio.h>
int swap(int,int);
void main()
{
inta,b;
printf("nenter two values:");
scanf("%d%d",&a,&b);
printf("Before swapping:n");
printf("A=%dn B=%d",a,b);
swap(a,b);
getch();
}


int swap(intx,int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("After swapping:n A=%dnB=%d",x,y);
return(0);
}



OUPUT:
enter two values
30
20
Before swapping:
A=30
B=20
After swapping:
A=20
B=30
//CALL BY REFERENCE
#include<stdio.h>
#include<conio.h>
int swap(int *,int *);
void main()
{
inta,b;
printf("nenter two values:");
scanf("%d%d",&a,&b);
printf("Before swapping:n");
printf("A=%dn B=%d",a,b);
swap(&a,&b);
getch();
}
int swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
printf("After swapping:n A=%dnB=%d",*x,*y);
return(0);
}
OUPUT:
enter two values
30
20
Before swapping:
A=30
B=20
After swapping:
A=20
B=30

SaraPIC

  • 1.
    Practical no.1 Write aC program to display hexadecimal decimal octal format of the entered numbers. #include<stdio.h> #include<conio.h> void main() { intnum; clrscr(); printf("Enter the number : "); scanf("%d",&num); printf("nDecimal format : %d",num); printf("nHexaDecimal format : %x",num); printf("nOctal format : %o",num); getch(); } OUTPUT: Enter the number:12 Decimal format :12 Hexadecimal format:c Octal format:14
  • 2.
    Practical no.2 To demonstrateall possible formatting specifiers. Format Specifiers for C %c The character format specifier. %d The integer format specifier. %i The integer format specifier (same as %d). %f The floating-point format specifier. %e The scientific notation format specifier. %E The scientific notation format specifier. %g Uses %f or %e, whichever result is shorter. %G Uses %f or %E, whichever result is shorter. %o The unsigned octal format specifier. %s The string format specifier. %u The unsigned integer format specifier. %x The unsigned hexadecimal format specifier. %X The unsigned hexadecimal format specifier. %p Displays the corresponding argument that is a pointer. %n Records the number of characters written so far. %% Outputs a percent sign. printf statements is called an escape sequence: n (newline) t (tab) v (vertical tab) f (new page) b (backspace) r (carriage return) n (newline)
  • 3.
    Practical no.3 Write aprogram to find the largest and the smallest from the given three integers. #include<stdio.h> #include<conio.h> void main() { int num1, num2, num3; clrscr(); printf("Enter three numbers = "); scanf("%d %d %d", &num1, &num2, &num3); printf("nnThe largest number is "); if( (num1 > num2) && (num1 > num3) ) printf("%d", num1); else if(num2 > num3) printf("%d", num2); else printf("%d", num3); printf("nnThe Smallest number is "); if( (num1 < num2) && (num1 < num3) ) printf("%d", num1); else if(num2 < num3)
  • 4.
    printf("%d", num2); else printf("%d",num3); getch(); } OUTPUT: Enter threenumbers: 12 15 67 The largest no. is :67 The smallest no.is :12
  • 5.
    Practical no.4 Write aC program to find even or odd numbers. #include<stdio.h> main() { intnum; printf ("Enter a number to be checked for even/odd: "); scanf ("%d",&num); if (num%2==0) { printf ("The entered number is EVEN.n"); } else { printf ("The entered number is ODD.n"); } } OUTPUT: Enter a number to be checked for even/odd:15 The entered number is ODD.
  • 6.
    Practical no.5 Write aC program to display menu 1.Addition 2.Subtraction 3.Multiplication 4.Division and execute it using switch case. #include<stdio.h> #include<conio.h> void main() { inta,b,c; int menu; printf("choose the arithmetic optionn"); scanf("%d",&menu); switch(menu) { case 1: printf("Enter The Two Numbers:n"); scanf("%d%d",&a,&b); c=a+b; printf("The addition of two numbers %dn",c); break; case 2: printf("Enter THE TWO NUMBERS:n"); scanf("%d%d",&a,&b); c=a-b; printf("The subtraction of two numbers %dn",c); break; case 3: printf("Enter THE TWO NUMBERS:n"); scanf("%d%d",&a,&b); c=a*b; printf("The multiplication of two numbers %dn",c); break; case 4: printf("Enter THE TWO NUMBERS:n"); scanf("%d%d",&a,&b); c=a/b; printf("The division of two numbers %dn",c); break; }
  • 7.
    getch(); } OUTPUT: choose the arithmeticoption 2 Enter THE TWO NUMBERS: 15 6 The subtraction of two numbers :9
  • 8.
    Practical no.6 Write ac program to perform addition all numbers from 1 to 100. #include<stdio.h> #include<conio.h> void main() { int i=0,sum=0; clrscr(); while(i<100) { sum=sum+i; i++; } printf("Addition of all numbers from 1-100=%d",sum); getch(); } OUTPUT: Addition of all numbers from 1-100=4950
  • 9.
    Practical no.7 Write aC program to find smallest and largest number from array elements. #include<stdio.h> #include<conio.h> Void main() { int a[20],min,max; intn,i; clrscr(); printf("enter the num of elementst:"); scanf("%d",&n); printf("enter the elementsn"); for( i=0;i<n;i++) { scanf("%d",&a[i]); if(i==0) { min=max=a[i]; } if(a[i]<min) min=a[i]; else if(a[i]>max)
  • 10.
    max=a[i]; } printf("largest element is %d n Smallest element is %d ",max,min); getch(); } OUTPUT: enter the num of elements:5 enter the element:12 56 23 10 78 largest element is 78 Smallest element is 10
  • 11.
    Practical no.8 Write aC program to enter elements for 3*3matrix and display them. #include<stdio.h> #include<conio.h> void main() { int a[3][3],i,j; clrscr(); printf("ENTER ELEMENTSn"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } printf("THE ELEMENTS IN 3*3 MATRIX FORM ISn"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d",a[i][j]); } printf("n"); } getch(); } OUTPUT: ENTER ELEMENTS 1 2 3 4 5 6 7 8 9 THE ELEMENTS IN 3*3 MATRIX FORM IS 1 2 3 4 5 6 7 8 9
  • 12.
    Practical no.9 Write aC program to demonstrate output of standard library functions. Strlent( ),strcpy( ),strcat( ),strcmp( ). #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s1[10],s2[10],s3[30]; int a; clrscr(); printf("enter the two stringsn"); scanf("%s%s",&s1,&s2); printf("1st String=%sn2ndString=%s",s1,s2); if(strcmpi(s1,s2) == 0 ) printf("Entered strings are equal.n"); else printf("Entered strings are not equal.n"); a=strlen(s1); printf("nLength=%d",a); strcat(s1,s2); printf("nconcatinated string=%s",s1); strcpy(s1,s2); printf("ncopied string=%s",s1); getch(); }
  • 13.
    OUTPUT: enter the twostrings Good Morning 1st String=Good 2ndString=Morning Entered strings are not equal. Length=4 concatinated string=GoodMorning copied string=Morning
  • 14.
    Practical no.10 Write aC program to calculatefactorial of any number using recursion. #include<stdio.h> #include<conio.h> int fact(int ); int main() { intnum,res; printf("Enter any number: "); scanf("%d", &num); res = fact(num); printf("Factorial value is: %d",res); getch(); return 0; } int fact(int n) { int f; if(n==1) return(1); else f = n*fact(n-1); return(f); } OUTPUT: Enter any number:5 Factorial value is:120
  • 15.
    Practical no.11 Write aC program to demonstrate call by value and call by reference. //CALL BY VALUE #include<stdio.h> #include<conio.h> int swap(int,int); void main() { inta,b; printf("nenter two values:"); scanf("%d%d",&a,&b); printf("Before swapping:n"); printf("A=%dn B=%d",a,b); swap(a,b); getch(); } int swap(intx,int y) { int temp; temp=x; x=y; y=temp;
  • 16.
    printf("After swapping:n A=%dnB=%d",x,y); return(0); } OUPUT: entertwo values 30 20 Before swapping: A=30 B=20 After swapping: A=20 B=30
  • 17.
    //CALL BY REFERENCE #include<stdio.h> #include<conio.h> intswap(int *,int *); void main() { inta,b; printf("nenter two values:"); scanf("%d%d",&a,&b); printf("Before swapping:n"); printf("A=%dn B=%d",a,b); swap(&a,&b); getch(); } int swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; printf("After swapping:n A=%dnB=%d",*x,*y); return(0); }
  • 18.
    OUPUT: enter two values 30 20 Beforeswapping: A=30 B=20 After swapping: A=20 B=30