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

1.

Write a C program for the following:


a) To calculate and display the volume of a CUBE having its height (h=10cm),
width (w=12cm) and depth (8cm).
b) To Search an element in an array.

A)

#include<stdio.h>

int main()

int v,l,b,d;

printf("ENTER LENGTH");

scanf("%d",&l);

printf("ENTER BREATH");

scanf("%d",&b);

printf("ENTER DEPTH");

scanf("%d",&d);

v=l*b*d;

printf("%d",v);

return 0;

b)

#include<stdio.h>

int main()

int a[100], n, element, pos=0;

int i;

printf("Enter array size [1-100]: ");

scanf("%d", &n);

printf("Enter array elements: ");


for(i=0; i<n; i++)scanf("%d", &a[i]);

printf("Enter element to search: ");

scanf("%d",&element);

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

if(a[i]==element)

printf("%d found at position %d", element, i+1);

return 0;

printf("%d not found.", element);

return 0;

2. Write a C program for the following:


a) To evaluate each of the following equations. (i)V = u + at. (ii) T=2*a+√b+9c
b) Addition of two matrices of any order.
A)1

#include<stdio.h>

int main()

int a,u,v,t;

printf("ENTER VALUES");

scanf("%d%d%d",&a,&u,&t);

v=u+(a*t);

printf("%d",v);

return 0;
}

2.

#include<stdio.h>

int main()

int a,b,c,t;

printf("ENTER VALUES");

scanf("%d%d%d",&a,&b,&c);

t=2*a+(b+9*c)^1/2; or t=2*a+(b^1/2)+9*c;

printf("%d",t);

return 0;

B)

#include<stdio.h>

int main()

int a[100][100],b[100][100],sum[100][100];

int r,c,i,j;

printf("Enter row and column size:");

scanf("%d %d",&r,&c);

printf("Enter element of M1:");

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

for(j=0;j<c;j++)

scanf("%d",&a[i][j]);}
printf("Enter element of M2:");

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

for(j=0;j<c;j++)

{scanf("%d",&b[i][j]);}

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

for(j=0;j<c;j++)

{sum[i][j]=a[i][j]+b[i][j];

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

for(j=0;j<c;j++)

printf("%d\t",sum[i][j]);

return 0;

3. Write a C program for the following:

c) Enter a 4-digit number from keyboard. Add 8 to the number and then divide it
by 3. Now, the modulus of that number is taken with 5 and then multiplies the
resultant value by 5. Display the final result.
d) To perform addition of all elements in Array and display the result.

A)
#include<stdio.h>
int main()
{
int n,a,b,c,d;
printf("enter number");
scanf("%d",&n);
a=n+8;
b=a/3;
c=b%5;
d=c*5;
printf("%d",d);
return 0;
}
B)
#include <stdio.h>

void main()

int a[100];

int i, n, sum=0;

printf("Enter total number of elements:");

scanf("%d",&n);

printf("Input %d elements in the array :\n",n);

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

printf("element - %d : ",i);

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

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

sum += a[i];

printf("Sum of all elements : %d\n\n", sum);

4. Write a C program for the following:


a) To swap values of two variables with and without using third variable.
b) To read a string and check for palindrome using built-in string functions.
A 1)
#include<stdio.h>
int main()
{
int a,b,c;
printf("enter two nos");
scanf("%d%d",&a,&b);
c=0;
{
c=a;
a=b;
b=c;
}
printf("%d%d",a,b);
return 0;
}
2)
#include<stdio.h>
void main()
{
int a,b,temp;
printf("\nEnter the value of A\n");
scanf("%d",&a);
printf("\nEnter the value of B\n");
scanf("%d",&b);
printf("\nBefore swapping\n");
printf("A::%d\nB::%d",a,b);
a=b+a;
b=a-b;
a=a-b;
printf("\nAfter swapping\n");
printf("%d%d",a,b);
}
B)
#include<stdio.h>
#include<string.h>
int main()
{
char a[10];
int i,n,c=0;
printf("Enter the string : ");
gets(a);
n=strlen(a);
for(i=0;i<n/2;i++)
{
if(a[i]==a[n-i-1])
c++;
}
if(c==i)
printf("string is palindrome");
else
printf("string is not palindrome");
return 0;
}

5.Write a C program for the following:

a) To print whether a given number is even or odd


b)Write a program to swap two integers using call by value and call by reference methods of
passing arguments to a function.
A)

#include <stdio.h>

int main()

int n;

printf("Enter an integer: ");

scanf("%d", &n);

if(n % 2 == 0)

printf("%d is even.", n);

else

printf("%d is odd.", n);

return 0;

B)1.CALL BY REFERENCE

#include <stdio.h>

void swap(int*x,int*y);

int main()

int a,b;

printf("Enter numbers: ");

scanf("%d%d",&a,&b);

swap(&a,&b);

printf("In main:a=%d,b=%d",a,b);

return 0;

}
void swap(int*x,int*y)

int t;

t=*x;

*x=*y;

*y=t;

printf("In swap function:x=%d,y=%d",*x,*y);

2.CALL BY VALUE

#include <stdio.h>

void swap(int x,int y);

int main()

int a,b;

printf("Enter numbers: ");

scanf("%d%d",&a,&b);

swap(a,b);

printf("In main:a=%d,b=%d",a,b);

return 0;

void swap(int x,int y)

int t;

t=x;

x=y;

y=t;
printf("In swap function:x=%d,y=%d",x,y);

6.Write a C program for the following:

a) Write a program to find the largest and smallest among three entered numbers and
also display whether the identified largest/smallest number is even or odd.

b) Write a C program to sort an array using functions.

A)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,c,min,max;
printf("Enter Three number : ");
scanf("%d%d%d",&a,&b,&c);
if(a < b&& a < c){
min = a;
}
else if (b < a && b < c)
{
min = b;
}
else
{
min = c;
}
if(a > b && a > c){
max = a;
}
else if ( b > a && b > c){
max = b;
}
else{
max = c;
}
printf("\nThe smallest number is : %d",min);
if(min%2 == 0){
printf("\n The smallest number is even");
}
else
{
printf("\n The smallest number is odd");
}
printf("\nThe largest number is : %d",max);
if(max%2 == 0){
printf("\n The largest number is even");
}
else
{
printf("\n The largest number is odd");
}
}
B)
#include<stdio.h>

void sort(int a[100], int n);

void main()

int a[100], i, n;

printf("Enter n:\n");

scanf("%d", &n);

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

printf("a[%d]=",i);

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

sort(a,n);

printf("Array in ascending order is:\n");

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

printf("%d\t", a[i]);

void sort(int a[10], int n)

{
int i, j, temp;

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

for(j=i+1;j< n;j++)

if(a[i]>a[j])

temp = a[i];

a[i] = a[j];

a[j] = temp;

7. A student will not be allowed to sit in exam if his/her attendance is less than 75%.
Write and implement a C program to take following input from user

1. Number of classes held


2. Number of classes attended and
Print attendance percentage. Is the student allowed to sit in exam or
not?
b) Write and implement a C program to find the sum of all the elements in an array using
pointers

A)
#include<stdio.h>

int main()

{ int a,h=100;

float p;

char c='%';
printf("Total no of days held:%d\n",h);

printf("Enter total no of days attended:");

scanf("%d",&a);

p=a*100/100;

printf("Attendance percentage: %.1f%c\n",p,c);

if(p>75)

printf("You are allowed to write exam.\n ");

else

printf("You are not allowed to write exam.\n");

return 0;

B)
#include <stdio.h>

void main()

static int array[5] = { 200, 400, 600, 800, 1000 };

int sum;

int addnum(int *ptr);

sum = addnum(array);

printf("Sum of all array elements = %5d\n", sum);

int addnum(int *ptr)

int index, total = 0;

for (index = 0; index < 5; index++)

total += *(ptr + index);


}

return(total);

8. a) Write and implement a C program to check whether the entered year is leap year or
not (a year is leap if it is divisible by 4 and divisible by 100 or 400.)

b) Write and implement a C program for the following pointer operations

1. Addition/ Subtraction of a constant number to a pointer


2. Subtraction of one pointer from another
3. Comparison of two pointers

A)

#include <stdio.h>

int main()

int year;

printf("Enter a year: ");

scanf("%d", &year);

if (year % 400 == 0)

printf("%d is a leap year.", year);

else if (year % 100 == 0)

printf("%d is a leap year.", year);

}
else if (year % 4 == 0)

printf("%d is a leap year.", year);

else

printf("%d is not a leap year.", year);

return 0;

B)

#include<stdio.h>

int main()

int number=50;

int*p,*q;

p=&number;

printf("Address of p variable is %u\n",p);

p=p-3;

printf("Address of p variable is %u\n",q);

q=&number;

q=q-3;

printf("After subtracting 3:Address of p and q variable is%u\n",p,q);

return 0;

2.
#include <stdio.h>

#include <stdlib.h>

int main()

int num1,num2;

int *ptr1,*ptr2;

int sub;

num1=350;

num2=50;

ptr1=&num1;

ptr2=&num2;

sub=*ptr1 - *ptr2;

printf("Subtraction of the given two integer values is: %d",sub);

return 0;

3.

#include <stdio.h>

int main(void)

int a[] = {10, 20, 30, 40, 50};

int i,*p;

for(p=a;p<=a+4;p++)

printf("%d\n",*p);

return 0;

9.Write and implement C program for the following:

a) To determine the factorial of a number.


b) Enter the marks of 5 students in Chemistry, Mathematics and Physics (each out of
100) using a structure named Marks having elements roll no., name, chem_marks,
maths_marks and phy_marks and then display the percentage of each student using C
program.

A)

#include<stdio.h>

int main()

int i=1,fact=1,n;

printf("enter number");

scanf("%d",&n);

while(i<=n)

fact=fact*i;

i=i+1;

printf("the factorial of the number %d is %d",n,fact);

return 0;

B)

#include <stdio.h>

struct student {

char firstName[50];

int roll;

int mat;

int phy;

int che;
float percentage;

} s[5];

int main() {

int i;

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

for (i = 0; i < 5; ++i) {

s[i].roll = i + 1;

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

printf("Enter first name: ");

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

printf("Enter maths mark: ");

scanf("%d", &s[i].mat);

printf("Enter physics mark: ");

scanf("%d", &s[i].phy);

printf("Enter chemistry mark: ");

scanf("%d",&s[i].che);

s[i].percentage=(s[i].che+s[i].mat+s[i].phy)/3;}

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

for (i = 0; i < 5; ++i) {

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

printf("Name: ");

puts(s[i].firstName);

printf("Chemistry Mark:%d\n",s[i].che);

printf("Maths Mark:%d\n",s[i].mat);

printf("Physics Mark:%d\n", s[i].phy);


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

printf("\n");

return 0;

10.Write and implement C program for the following:

a) Use structures to get user input of name, rollno and marks obtained by a student in 4
subjects of 100 marks each and display the name, rollno with percentage score secured.

b) To find whether a character is consonant or vowel using switch statement.

A)

#include <stdio.h>
struct student {
char firstName[50];
int roll;
int m1;
int m2;
int m3;
int m4;
float percentage;
} s;

int main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 1; ++i) {
s.roll = i + 1;
printf("Enter roll no:");
scanf("%d",&s.roll);
printf("Enter first name: ");
scanf("%s", s.firstName);
printf("Enter Mark1: ");
scanf("%d", &s.m1);
printf("Enter Mark2: ");
scanf("%d", &s.m2);
printf("Enter Mark3: ");
scanf("%d",&s.m3);
printf("Enter Mark4: ");
scanf("%d",&s.m4);

s.percentage=(s.m1+s.m2+s.m3+s.m4)/4;}
printf("Displaying Information:\n\n");
for (i = 0; i < 1; ++i) {
printf("\nRoll number: %d\n",s.roll);
printf("Name: ");
puts(s.firstName);
printf("Mark1:%d\n",s.m1);
printf("Mark2:%d\n",s.m2);
printf("Mark3:%d\n",s.m3);
printf("Mark3:%d\n",s.m4);
printf("percentage :%.1f\n",s.percentage);
printf("\n");
}
return 0;
}

B)

#include <stdio.h>
int main()
{
char ch;
printf("Enter any alphabet: ");
scanf("%c", &ch);
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
break;
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}

return 0;
}

11. Write and implement C program for the following:

a) To convert Celsius to Fahrenheit temperature.

b) To find the Length of a String without using in-built string functions

A)

#include <stdio.h>

int main()

float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 9 / 5) + 32;

printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);

return 0;

B)

#include<stdio.h>

#include<string.h>

int main()

char name[10];

int n=0,count=0;
printf("Input:");

scanf("%s",name);

while(name[n]!='\0')

count ++;

n ++;

printf("Length of the string:%d",count);

return 0;

12. Write and implement C program for the following:


a) To display the size of every data type using “sizeof” operator.
b) To create a file called emp.txt and store information about a person, in terms of his
name, age and salary

A)

#include<stdio.h>

int main()

printf("Size of int: %d bytes\n", sizeof(int));

printf("Size of float: %d bytes\n", sizeof(float));

printf("Size of double: %d bytes\n", sizeof(double));

printf("Size of char: %d byte\n", sizeof(char));

return 0;

B)

#include<stdio.h>
int main(){

char name[20];

int age;

float salary;

FILE *fp;

fp=fopen("emp.txt","w+");

printf("enter name");

scanf("%s",&name);

printf("enter age");

scanf("%d",&age);

printf("Enter salary");

scanf("%f",&salary);

fprintf(fp,"%s %d %f",name,age,salary);

printf("\n \n");

fscanf(fp,"%s %d %f",name,age,salary);

printf("Name:%s\nAge:%d\nSalary:%f\n",name,age,salary);

return 0;

13) Write and implement C program for the following:

a) To find the largest of three numbers using ternary operators.


b) To perform file operations using fseek(), ftell() and rewind() function

A)

#include<stdio.h>
int main()

int a,b,c,t,max;

printf("enter 3 numbers\n");

scanf("%d%d%d",&a,&b,&c);

t=a>b?a:b;

max=t>c?t:c;

printf("\nmax value is %d",max);

return 0;

B)

#include<stdio.h>

#include<stdlib.h>

int main()

char name[10];

FILE*fp;

fp=fopen("student.txt","w+");

if(fp==NULL)

printf("Error opening a file");

exit(1);}

printf("Enter name:");

scanf("%s",name);

fprintf(fp,"%s",name);

printf("%d",ftell(fp));
rewind(fp);

printf("%d",ftell(fp));

fseek(fp,3,2);

printf("%d",ftell(fp));

return 0;

14) Write and implement C program for the following:


a) A company decided to give bonus of 5% to employee if his/her year of service is
more than 5 years. Ask user for salary and year of service to print the net bonus
amount.
b) To find the largest and smallest element in an Array.
A)
#include<stdio.h>

int main()
{
int bonus,salary,year;
printf("Enter your years of service:");
scanf("%d",&year);
if(year>5)
{printf("Enter your salary:");
scanf("%d",&salary);
bonus=salary*5/100;
printf("Your bonus amount: %d",bonus);}
else
{
printf("You are not eligible for bonus");}
return 0;}

B)
#include<stdio.h>

int main()
{
int a[50],i,n,large,small;
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("\nThe smallest element is %d",small);
return 0;
}

15 ) Write and implement a C program for the following


a) To display the following pattern.

**

***

****

*****

b) To add, subtract, multiply and divide two integers using user defined function
for each.

A)
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}

B)
ADD

#include<stdio.h>
#include<conio.h>
struct sum
{
int a;
int b;
};
void main()
{
int sum1;
struct sum s;
clrscr();

printf("Enter two numbers:");


scanf("%d%d",&s.a,&s.b);

sum1=s.a+s.b;
printf("Sum=%d",sum1);

SUBTRACT

#include<stdio.h>
#include<conio.h>
struct sum
{
int a;
int b;
};

void main()
{
int sub1;
struct sub s;

printf("Enter two numbers:");


scanf("%d%d",&s.a,&s.b);

sum1=s.a-s.b;
printf("Sub=%d",sub1);

MULTIPLY

#include<stdio.h>
#include<conio.h>
struct mult
{
int a;
int b;
};

void main()
{
int mult1;
struct mult s;

printf("Enter two numbers:");


scanf("%d%d",&s.a,&s.b);

mult1=s.a*s.b;
printf("mult=%d",mult1);

DIVIDE

#include<stdio.h>
#include<conio.h>
struct divd
{
int a;
int b;
};

void main()
{
int divd1;
struct divd s;

printf("Enter two numbers:");


scanf("%d%d",&s.a,&s.b);

divd1=s.a/s.b;
printf("divd=%d",divd1);

16) Write and implement C program for the following:

a) To calculate sum of first 20 natural numbers using recursive function.


b) To illustrate the dynamic memory allocation concept.

A)

#include <stdio.h>

int addNumbers(int n);

int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", addNumbers(num));
return 0;
}

int addNumbers(int n) {
if (n != 0)
return n + addNumbers(n - 1);
else
return n;
}

B)

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,count,*arr,sum = 0,x;
printf("Enter the total number: ");
scanf("%d", &count);
arr = (int *)malloc(count * sizeof(int));
for (i = 0; i < count; i++)
{
printf("Enter element %d : ", (i + 1));
scanf("%d", arr + i);
sum += *(arr + i);
}
printf("sum by malloc is %d \n", sum);
sum= 0;
printf("Enter the resizing number: ");
scanf("%d", &x);
arr= realloc(arr,x);
for (i = 0; i < x; i++)
{
printf("Enter element %d : ", (i + 1));
scanf("%d", arr + i);
sum += *(arr + i);
}

printf("sum by realloc is %d \n", sum);


free(arr);
return 0;
}

You might also like