Practical Programs 1. Write A Program To Find The Largest of Three Numbers Using If and Conditional Operator
Practical Programs 1. Write A Program To Find The Largest of Three Numbers Using If and Conditional Operator
OUTPUT
Enter any number
1234
Reversing of the given number= 4321
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,j,count=0;
clrscr();
printf("Enter the n value \n");
scanf("%d", &n);
printf("Prime numbers \n");
for(i=1;i<=n;i++)
{
count=0;
for(j=2;j<i;j++)
{
if(i%j==0)
count=count+1;
}
if(count==0)
printf("%d\n",i);
}
getch();
}
OUTPUT
Enter the n value
10
Prime numbers
2
3
5
7
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,k,disc;
float root1,root2,img;
clrscr();
printf("Enter a,b,c values \n");
scanf("%d%d%d", &a,&b,&c);
disc=((b*b)-(4*a*c));
if(disc>0)
k=1;
else if(k<0)
k=2;
else
k=3;
switch(k)
{
case 1:
root1= (-b+sqrt(disc))/(2*a);
root2= (-b-sqrt(disc))/(2*a);
printf("Two distinct and real roots exists : %2f and %2f \n", root1, root2);
break;
case 2:
root1=root2= -b/(2*a);
img=sqrt(-disc)/(2*a);
printf("Two distinct complex roots are exists : %2f +i%2f \n %2f - i %2f ",
root1,img,root2,img);
break;
case 3:
root1=root2= -b/ ( 2*a);
printf("Two roots are real and equal : %2f and %2f \n", root1,root2);
break;
}
getch();
}
OUTPUT
Enter a,b,c values
121
Two roots are real and equal :-1.0 and -1.0
printf("\n");
}
getch();
}
OUTPUT
Enter the n value
4
*
***
*****
*******
6. Write a program to find largest and smallest elements in a given list of
numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i.min,max,n;
clrscr( );
printf("Enter the n value \n");
scanf("%d",&n);
printf("Enter the elements into the array \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=min=a[0];
for(i=1 i<n;i++)
{
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
printf("Largest element in the given list =%d \n",max);
printf("Smallest lement in the given list = %d \n",min);
getch();
}
OUTPUT
Enter the n value
6
Enter the elements the array
12
44
33
66
21
3
Largest element in the given list=66
Smallest element in the given list=3
7. Write a program to find the product of two matrices.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,k;
clrscr();
printf("Enter the row & column size of the matrix \n");
scanf("%d%d",&m,&n);
printf("Enter the elements into the Matrix A \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
printf("Enter the elements into the Matrix B \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d", &b[i][j]);
printf("Matrix Multiplication \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("Resultant Matrix C is \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%5d", c[i][j]);
}
printf("\n");
}
getch( );
}
OUTPUT
Enter the row & column size of the Matrix
2
2
Enter the elements into the matrix A
12
34
Enter the Elements into the Matrix B
10
01
Matrix Multiplication
Resultant Matrix c
12
34
8. Write a program to find the GCD of two numbers using iteration and
recursion.
#include<stdio.h>
#include<conio.h>
void main( )
{
int n1,n2;
int hcf(int,int);
clrscr();
printf("enter any two integers \n");
scanf("%d%d", &n1,&n2);
printf("GCD of %d and %d is %d \n", n1,n2,hcf(n1,n2));
getch();
}
int hcf(int n1, int n2)
{
if(n2!=0)
{
return(hcf(n2,n1%n2);
else
return n1;
}
OUTPUT
Enter any two integers 25 4
GCD of 25 and 4 is 1
9.Write a program to the use of storage Classes
include <stdio.h>
void abc();
int main()
{
abc();
abc();
abc();
return 0;
}
void abc()
{
auto int a = 5;
++a;
printf("\n a = %d ", a);
}
10. Write a Program for call by value and the Call by Reference
#include<stdio.h>
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(x);//passing value in function
printf("After function call x=%d \n", x);
return 0;
}
Output
#include<stdio.h>
void change(int *num) {
printf("Before adding value inside function num=%d \n",*num);
(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(&x);//passing reference in function
printf("After function call x=%d \n", x);
return 0;
}
Output
#include<stdio.h>
#include<conio.h>
void main()
{
char s[100];
int c=0;count[26]={0},x;
clrscr();
printf("Enter any string \n");
scanf("%s",s);
while(s[c] !='\0')
{
if(s[c]>='a' && s[c]<='z')
{
x=s[c]-'a';
count[x]++;
}
c++;
}
for(c=0;c<26;c++)
printf("%c occurs %d times in the string :\n", c + 'a', count[c]);
getch( );
}
OUTPUT
Enter any String
Apple
a occurs 1 times in the string
b occurs 0 times in the string
c occurs 0 times in the string
d occurs 0 times in the string
e occurs 1 times in the string
f occurs 0 times in the string
g occurs 0 times in the string
h occurs 0 times in the string
i occurs 0 times in the string
j occurs 0 times in the string
k occurs 0 times in the string
l occurs 1 times in the string
m occurs 0 times in the string
n occurs 0 times in the string
o occurs 0 times in the string
p occurs 2 times in the string
q occurs 0 times in the string
r occurs 0 times in the string
12. Write a program to demonstrate use of string functions string.h header
file.
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
intl;
clrscr();
printf("enter any 2 strings \n");
scanf("%s%s",s1,s2);
l=strlen(s1);
strcat(s1,s2);
printf("Length of string s1=%d\n",s1);
printf("After concatenation s1=%s\n",s1);
strrev(s1);
printf("Reversing the string s1=%s",s1);
getch();
}
OUTPUT
Enter any 2 strings
rama
sita
Length of string s1=4
After concatenation s1=ramasita
Reversing the string s1=amar.
#include<conio.h>
void main() {
char ch;
int count=0;
FILE *fptr;
clrscr();
fptr=fopen("text.txt","w");
if(fptr==NULL) {
exit(0);
while((ch=getche())!='\r') {
fputc(ch,fptr);
fclose(fptr);
fptr=fopen("text.txt","r");
while((ch=fgetc(fptr))!=EOF) {
count++;
printf("%c",ch);
fclose(fptr);
getch();
#include <stdio.h>
struct student
char name[50];
int roll;
float marks;
} s[10];
int main()
int i;
// storing information
{ s[i].roll = i+1;
scanf("%s",s[i].name);
scanf("%f",&s[i].marks);
printf("\n"); }
printf("Displaying Information:\n\n");
// displaying information
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
return 0;
Output
Enter marks: 98
Enter marks: 89
Displaying Information:
Roll number: 1
Name: Tom
Marks: 98
15.Write a programe that open an existing text files and copies it to a new text
files
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
Output:
Enter the filename to open for reading
a.txt
Enter the filename to open for writing
b.txt
Contents copied to b.txt
16. write a program that print a table indicating the Number of Occurrence of
each alphabet in the text entered as command line arguments
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char *argv[])
{
string str=””;
static int alphabet[26];
int x;
cout<<“\n\n Command-Line Argument\n”;
for(int i=0;i<argc;i++)
{
cout<<“\n “<<argv[i];
str+=argv[i];
}
for(int i=0;i<str.length();i++)
{
if(str[i]>=’A’ && str[i]<=’Z’)
{
x=((int)str[i])-65;
alphabet[x]++;
}
else if(str[i]>=’a’ && str[i]<=’z’)
{
x=((int)str[i])-97;
alphabet[x]++;
}