Professional Documents
Culture Documents
Lab Manual
Lab Manual
#include<stdio.h>
#include<string.h>
main()
{
char st[100];
int x;
printf("enter any phrase:");
gets(st);
x=strlen(st);
printf("the length of entered string is: %d",x);
}
#include<stdio.h>
#include<string.h>
main()
{
char st[100];
printf("enter any phrase:");
scanf("%s",&st);
printf("string in Uppercase is : %s",strupr(st));
}
3. Write a program which finds the first occurrence of given character in string.
#include<stdio.h>
#include<string.h>
main()
{
char ch[100]="this is a string",a;
char *x;
printf("which character do you want to find?");
scanf("%c",&a);
x=strchr(ch,a);
printf("%d",x-ch+1);
}
4. Write a program which append your last name with your first name at the
end.
#include<stdio.h>
#include<string.h>
main()
{
char st1[20],st2[20];
printf("enter your first name:");
scanf("%s",&st1);
printf("enter your last name:");
scanf("%s",&st2);
printf("your name is : %s",strcat(st1,st2));
}
2. Write a c program which find SUM and AVERAGE of two integer numbers
using User Defined Functions.
#include<stdio.h>
int add(int a,int b)
{
return a+b;
}
float av(int sum)
{
return sum/2;
}
int sum(int,int);
main()
{
int a,b,sum;
float avg;
printf("enter 2 numbers:");
scanf("%d %d",&a,&b);
sum=add(a,b);
avg=av(sum);
printf("sum is : %d\n average is :%0.2f",sum,avg);
}
#include<stdio.h>
void table(int x)
{
int i;
for(i=1;i<=10;i++)
{
printf("%d x %d = %d\n",x,i,x*i);
}
}
main()
{
int x;
printf("enter number:");
scanf("%d",&x);
table(x);
}
4. Write a C program using 2 functions: 1 taking arguments by reference and 1
by value. Find the 10% increase in a given salary with the two functions.
#include<stdio.h>
float inc(int a)
{
return ((a*0.1)+a);
}
#include<stdio.h>
#include<string.h>
main()
{
int n,i;
char name[10][10];
printf("how many names you want to enter:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",&name[i]);
}
printf("the names you entered are:\n\n");
for(i=0;i<n;i++)
{
printf("%s\n",name[i]);
}
}
6. Write a program which finds the roots of quadratic equation. Input the
values of a,b and c. determine if the roots are real, imaginary or equal.
#include<stdio.h>
#include<math.h>
void disc(int a,int b,int c)
{
int d;
d=b*b-4*a*c;
if(d>0)
printf("roots are real");
else if(d<0)
printf("roots are imaginary");
else
printf("roots are equal");
}
#include<stdio.h>
main()
{
int a[10],i,j,n;
printf("enter 10 numbers:");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(a[i]>a[j]);
n=a[i];
a[i]=a[j];
a[j]=n;
}
}
printf("Number in Ascending order are:\n");
for(i=0;i<10;i++)
{
printf("%d\t",a[i]);
}
}
2. Write a program which takes array elements form user and display the
largest element of array.
#include<stdio.h>
main()
{
int i,max,min,array[10];
printf("enter 10 numbers:");
for(i=0;i<10;i++)
{
scanf("%d",&array[i]);
}
max=array[0];
for(i=0;i<10;i++)
{
if(array[i]>max)
max=array[i];
}
printf("maximum is : %d\n",max);
}
1. C program to find sum of all array elements by passing as an argument
using user defined functions.
#include<stdio.h>
void array(int a[10])
{
int i,sum=0;
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
printf("sum is : %d",sum);
}
main()
{
int a[10];
printf("enter 10 numbers:");
array(a);
}
#include <stdio.h>
main()
{
int arr[10], i, j, k, size;
#include<stdio.h>
void trans(int m[3][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&m[j][i]);
}
printf("Given matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",m[j][i]);
}
printf("\n");
}
5. Write a program having a function sum(int _ _ ) which finds the sum of each
row and column of a matrix.
#include<stdio.h>
void sum_r_c(int m[3][3])
{
int i,j,rs=0,cs=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
rs=rs+m[i][j];
cs=cs+m[j][i];
}
printf("\n sum of r %d is : %d",i+1,rs);
printf("\n sum of c %d is : %d",i+1,cs);
rs=0;
cs=0;
}
}
main()
{
int m[3][3],i,j;
printf("enter matrix :");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&m[i][j]);
}
}
sum_r_c(m);
}
#include<stdio.h>
main()
{
int m[3][3],i,j,t;
printf("enter matrix:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&m[i][j]);
}
}
for(i=0;i<3;i++) //enter change 1st and 2nd row
{
for(j=0;j<3;j++)
{
t=m[0][i];
m[0][i]=m[1][i];
m[1][i]=t;
}
}
printf("after enterchaing :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
}
#include<stdio.h>
main()
{
int i,j,m[3][3],c=1;
printf("enter matix ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&m[3][3]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if((i==j && m[i][j])!=1)
{
c=0;
break;
}
else if((i!=j && m[i][j] )!=0 )
{
c=0;
break;
}
}
}
if(c==1)
printf("matrix is identity");
else
printf("matrix is not identity");
}
1. Write a C program by using Pointer Comparison Operators which creates
array of three values stored in variables and show the address of those variables.
#include<stdio.h>
main()
{
int a=5,b=10,c=15,array[3],i;
int *p;
array[0]=a;
array[1]=b;
array[2]=c;
p=array;
for(i=0;i<3;i++)
{
printf("address of %d is %u\n",*(p+i),p+i);
}
}
2. Write a C program that creates four variables of different data types and
also gives reference of those variables to access them by their reference.
#include<stdio.h>
main()
{
int a=10,*p1;
float b=15.5,*p2;
long c=15232,*p3;
char ch='a',*p4;
p1=&a;
p2=&b;
p3=&c;
p4=&ch;
printf("a= %d\n",*p1);
printf("b= %f\n",*p2);
printf("c= %ld\n",*p3);
printf("ch= %c\n",*p3);
}
3. (i) Explain the error?
char c=’A’;
double *p=&c;
(iii) after these statements, which of the following statements will change
the Value of i to 75?
(i) k=75;
(ii) *k=75;
(iii) p=75;
(iv) *p=75;
(v) two or more of the answers will change the i to 75.
#include<stdio.h>
void swap(int *,int *);
main()
{
int a,b;
printf("enter two numbers");
scanf("%d %d",&a,&b);
printf("address of %d is %d \n",a,&a);
printf("address of %d id %d",b,&b);
swap(&a,&b);
printf("\naddress of %d is %d \n",a,&a);
printf("address of %d id %d",b,&b);
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
Correction: sqr(a)
{
return (a*a);
}
4. Write a function to calculate the factorial value of any integer given by user?
#include<stdio.h>
int result(int x)
{
int n=1;
while(x>=1)
{
n=n*x;
x--;
}
return n;
}
main()
{
int x,r;
printf("enter any number:");
scanf("%d",&x);
r=result(x);
printf("factorial is : %d",r);
}
1. Attach the tasks given in the class.
#include<stdio.h>
main()
{
struct student{
char name[10];
int r_n;
int marks;
float gpa;
};
student mystruct;
printf("enter you name:");
scanf("%s",&mystruct.name);
printf("enter your roll number");
scanf("%d",&mystruct.r_n);
printf("enter your marks:");
scanf("%d",&mystruct.marks);
printf("enter your gpa:");
scanf("%f",&mystruct.gpa);
printf("Your name is : %s\n",mystruct.name);
printf("Your roll number is : %d\n",mystruct.r_n);
printf("Your marks are : %d\n",mystruct.marks);
printf("Your gpa is : %f\n",mystruct.gpa);
}
1. Write a program which take user input and append it with previous text of
the file.
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 1000
main()
{
FILE *fPtr;
char filePath[100];
char dataToAppend[BUFFER_SIZE];
printf("Enter file path: ");
scanf("%s", filePath);
fPtr = fopen(filePath, "a");
if (fPtr == NULL)
{
/* Unable to open file hence exit */
printf("\nUnable to open '%s' file.\n", filePath);
printf("Please check whether file exists and you have write privilege.\n");
exit(EXIT_FAILURE);
}
/* Input data to append from user */
printf("\nEnter data to append: ");
fflush(stdin); // To clear extra white space characters in stdin
fgets(dataToAppend, BUFFER_SIZE, stdin);
/* Append data to file */
fputs(dataToAppend, fPtr);
fPtr = freopen(filePath, "r", fPtr);
printf("\nSuccessfully appended data to file. \n");
printf("Changed file contents:\n\n");
//readFile(fPtr);
fclose(fPtr);
}
2. Write a program that will read a file and count how many characters, spaces,
tabs and new lines are present in it.
#include <stdio.h>
int main()
{
char in_name[80];
FILE *in_file;
int ch, character = 0, line = 0, space = 0, tab = 0;
if (in_file == NULL)
printf("Can't open %s for reading.\n", in_name);
else
{
while ((ch = fgetc(in_file)) != EOF)
{
character++;
if (ch == ' ')
space++;
if (ch == '\n')
line++;
if (ch == '\t')
tab++;
}
fclose(in_file);
3. C program which read a file if it exists else notify a message that file does not
exist.
#include<stdio.h>
int main() {
/* try to open file to read */
FILE *file;
if (file = fopen("myfile.txt", "r")) {
fclose(file);
printf("file exists");
} else {
printf("file doesn't exist");
}
}