Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

1. Write a program which finds the length of the entered string.

#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);
}

2. write a program which converts a string from lowercase to uppercase.

#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);
}

3. Write a C program to print Table of an Integer number using User Defined


Functions.

#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);
}

void inc2(int *a)


{
*a=((*a*0.1)+*a);
}
main()
{
int a;
printf("enter salary:");
scanf("%d",&a);
printf("salary increase by value: %0.2f",inc(a));
inc2(&a);
printf("\n salary increase by value: %d",a);
}

5. Write a C program to input list of names and print them.

#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");
}

void roots(int a,int b,int c)


{
int x1,x2;
x1=(-b+sqrt(b*b-4*a*c))/2*a;
x2=(-b-sqrt(b*b-4*a*c))/2*a;
printf("\nx1= %d and x2 = %d",x1,x2);
}
main()
{
int a,b,c;
printf("enter values of a,b,c");
scanf("%d %d %d",&a,&b,&c);
disc(a,b,c);
roots(a,b,c);
}
1. Write a C program which takes values of an array as an input, and sort
them in ascending order.

#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);
}

2. Write a program which removes the duplicate element in an array.

#include <stdio.h>
main()
{
int arr[10], i, j, k, size;

printf("enter size of array:");


scanf("%d", &size);
printf("\n Please Enter %d elements of an Array \n",size);
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++)
{
for(j = i + 1; j < size; j++)
{
if(arr[i] == arr[j])
{
for(k = j; k < size; k++)
{
arr[k] = arr[k + 1];
}
size--;
j--;
}
}
}

printf("\n Final Array after Deleting Duplicate Array Elements is:\n");


for (i = 0; i < size; i++)
{
printf("%d\t", arr[i]);
}
}

3. Write a program for multiplication of two matrices. The program must


check the compatibility before multiplication.
#include<stdio.h>
main()
{
int i,j,k,r1,c1,r2,c2,m1[5][5],m2[5][5],sum=0,product[5][5];
printf("enter rows and column of first matrix:");
scanf("%d %d",&r1,&c1);
printf("enter rows and column of second matrix:");
scanf("%d %d",&r2,&c2);
if(c1!=r2)
printf("Matrix can not be multiplayed!");
else if(c1==r2) //condition to check if coloumn of 1st equals to rows of second
{
printf("matrix can be multiplayed: \n");
printf("enter 1st matrix: \n"); //1st matrix input
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}

printf("enter 2nd matrix: \n"); //2nd matrix input


for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++)
{
sum= sum+ (m1[i][k]*m2[k][j]);
}
product[i][j]=sum;
sum=0;
}
}
printf("matrix multiplication is :\n\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",product[i][j]);
}
printf("\n");
}
}

4. Find the transpose of matrix using function transpose(int _ _).

#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");
}

printf("\n\nTranspose of given matrix is:\n");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
}
main()
{
int m[3][3];
printf("enter matrix:\n");
trans(m);
}

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);
}

6. C program to enter change two rows & column of matrix.

#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");
}
}

7. C program to check if a given matrix is identity or not.

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

(ii) consider the following statements:


int *p;
int i;
int k;
i=42;
k=i;
p=&i;

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

Statement (iv) *p=75; will change the value of i to 75.


1. Write a program that asks user to enter two integers into variable a and b
prints their address in memory and values first, than swap both values and
finally prints again value of a and b and their address.

#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;
}

2. Write a program that compute area of rectangle and perimeters of rectangle


using a single function.
#include<stdio.h>
void rectangle(int *p1,int *p2)
{
int a,p;
a= *p1 * *p2;
p= 2* *p1 * *p2;
printf("area = %d and perimeter = %d",a,p);
}
main()
{
int l,w;
printf("ente length and width of rectangle");
scanf("%d %d",&l,&w);
rectangle(&l,&w);
}
3. Is this a correctly written function: if not then correct it?
sqr(a);
int a;
{
return (a*a);
}

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;

printf("Enter file name:\n");


scanf("%s", in_name);

in_file = fopen(in_name, "r");

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);

printf("\nNumber of characters = %d", character);


printf("\nNumber of spaces = %d", space);
printf("\nNumber of tabs = %d", tab);
printf("\nNumber of lines = %d", line);
}
return 0;
}

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");
}
}

You might also like