[go: up one dir, main page]

0% found this document useful (0 votes)
344 views16 pages

Practical Programs 1. Write A Program To Find The Largest of Three Numbers Using If and Conditional Operator

The document contains 15 programming problems with solutions in C language. The problems cover concepts like if-else conditional statements, loops, functions, arrays, structures, file handling and more. Each problem has the code snippet to solve it along with sample input-output. The problems progress from basic to more advanced concepts like recursion, dynamic memory allocation, file operations etc. Overall, the document aims to provide practical programming problems and their solutions to help learn various aspects of C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
344 views16 pages

Practical Programs 1. Write A Program To Find The Largest of Three Numbers Using If and Conditional Operator

The document contains 15 programming problems with solutions in C language. The problems cover concepts like if-else conditional statements, loops, functions, arrays, structures, file handling and more. Each problem has the code snippet to solve it along with sample input-output. The problems progress from basic to more advanced concepts like recursion, dynamic memory allocation, file operations etc. Overall, the document aims to provide practical programming problems and their solutions to help learn various aspects of C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

PRACTICAL PROGRAMS

1. Write a program to find the largest of three numbers using if and


conditional operator
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter any three numbers \n");
scanf("%d%d%d", &a,&b,&c);
if((a>b) && (a>c))
printf("%d is big",a);
if((b>a) && (b>c))
printf("%d is big",b);
if((c>a) && (c>b))
printf("%d is big",c);
getch();
}
OUTPUT
Enter any three numbers
10
20
30
30 is big

2. Write a program to print the reverse of a given number:


#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,sum=0;
clrscr();
printf("Enter any number \n");
scanf("%d", &n);
while(n!=0)
{
d=n%10;
sum=sum*10+d;
n=n/10;
}
printf("Reversing of the given number =%d \n",sum);
getch();
}

OUTPUT
Enter any number
1234
Reversing of the given number= 4321

3. Write a program to print the prime numbers from 2 to n where n is given


by the user.

#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

4. Write a program to find the roots of a quadratic equation using switch


statement

#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

5. Write a program to print a triangle of stars as follows


*
***
*****
*******
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
print("Enter the number of rows \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" ");
}
for(k=1;k<=(2*i)-1;k++)
printf("*");

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

Before function call x=100


Before adding value inside function num=100
After adding value inside function num=200
After function call x=100

#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

Before function call x=100


Before adding value inside function num=100
After adding value inside function num=200
After function call x=200

11 . Write a program that prints a table indicating the number of occurrences


of each alphabet in the text entered as command line arguments.

#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.

13.WAP that opens a file and counts the number of characters in


a file
#include<stdio.h>

#include<conio.h>

void main() {

char ch;

int count=0;

FILE *fptr;

clrscr();

fptr=fopen("text.txt","w");

if(fptr==NULL) {

printf("File can't be created\a");


getch();

exit(0);

printf("Enter some text and press enter key:\n");

while((ch=getche())!='\r') {

fputc(ch,fptr);

fclose(fptr);

fptr=fopen("text.txt","r");

printf("\nContents of the File is:");

while((ch=fgetc(fptr))!=EOF) {

count++;

printf("%c",ch);

fclose(fptr);

printf("\nThe number of characters present in file is: %d",count);

getch();

14.WAP to create a structure student containing fields for Roll


no,Name,Class,Year and TOTAL Marks . create ten students and store
them in a file.

#include <stdio.h>

struct student

char name[50];
int roll;

float marks;

} s[10];

int main()

int i;

printf("Enter information of students:\n");

// storing information

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

{ s[i].roll = i+1;

printf("\nFor roll number%d,\n",s[i].roll);

printf("Enter name: ");

scanf("%s",s[i].name);

printf("Enter marks: ");

scanf("%f",&s[i].marks);

printf("\n"); }

printf("Displaying Information:\n\n");

// displaying information

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

printf("\nRoll number: %d\n",i+1);

printf("Name: ");

puts(s[i].name);

printf("Marks: %.1f",s[i].marks);
printf("\n");

return 0;

Output

Enter information of students:

For roll number1,

Enter name: Tom

Enter marks: 98

For roll number2,

Enter name: Jerry

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]++;
}

You might also like