0% found this document useful (0 votes)
3 views

C Programming Lab File

Uploaded by

Mehnaaz Ansari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C Programming Lab File

Uploaded by

Mehnaaz Ansari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 123

Mehnaaz Ansari

07701182023

INDIRA GANDHI DELHI TECHNICAL UNIVERSITY FOR WOMEN,


KASHMERE GATE, DELHI 110006

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING


1ST SEMESTER
C PROGRAMMING LAB FILE

SUBMITTED BY: Mehnaaz Ansari SUBMITTED TO: Ms. Bhawna Jain


ENROLLMENT NO.: 07701182023 SUBJECT CODE: BCS-110
BATCH: ECE-AI 2

1
Mehnaaz Ansari
07701182023

List of Experiments

Paper Code: BCS110 Branch: ECE-AI 2


Subject Name: C-programming
Faculty Name: Ms. Bhawna Jain Year: 2023
Software used: Online Compilers/Code Blocks
S.No. List of Experiments Remarks

1. Input/output

2. Operators and expressions

3. If, else, else if ladder

4. For, while, do while loop

5. Break switch, continue, goto

6. Function

7. Recursion

8. Arrays

9. Pointers

10. Strings

11. Structures

12. Problems on MATH

13. Operations on Bits

14. Miscellaneous coding

2
Mehnaaz Ansari
07701182023

INDEX
LAB-1

S.No. Program/Code Name Date


1. Write a C program to display “This is my first C program 30/08/23

2. Write a C program that prints the perimeter of a rectangle to 30/08/23


take its height and width as input

3. Write a C program to calculate area and circumference of a 30/08/23


circle

4. Write a C program to perform addition, subtraction, 30/08/23


multiplication and division of two numbers

5. Write a C program to evaluate each of the following 30/08/23


equations
a) V= u+ gt
b) S= ut+ 1/2gt2
c) V2= u2+ 2gs

6. Write a C program that takes hours and minutes as Input, 30/08/23


and calculates the total number of minutes

7. Write a program in C that reads a forename, surname and 30/08/23


year of birth and display the names and the year one after
another sequentially

8. Write a program that converts centigrade to fahrenheit 30/08/23

9. C program to calculate the value of nCr 30/08/23

10. C program to extract the last two digits from a given year 30/08/23

3
Mehnaaz Ansari
07701182023

LAB-2
S.No. Program/Code Name Date
1. Write a program to find the following +, -, *, / and %, between two 06/09/23
numbers

2. Write a program to find the following a++, a--, ++a and –a 06/09/23

3. Write a program to find the following a+=b, a-=b, a*=b, a/=b and a%=b 06/09/23
between two numbers.

4. Write a program to check whether two numbers are ==, <, >, >=, <= and != 06/09/23

5. Write a program to find the following result. 06/09/23


Int a=5, b=5, c=10, result;
a) Result = (a==b) && (c>b);
b) Result = (a==b) && (c<b);
c) Result = (a==b) || (c<b);
d) Result = (a!=b) || (c<b);
e) Result = !(a!=b);
f) Result = !(a==b);

6. Write a program to find the following &, >>, <<, |, ~, ^ between two 06/09/23
numbers.

7. Write a program to find the size of the following data types int, float, char, 06/09/23
string, long int, double, long float.

4
Mehnaaz Ansari
07701182023

LAB-3
S.No. Program/Code Name Date

1. Write a C program to check whether a given number is even or odd. 20/09/23

2. Write a C program to find whether a given year is a leap year or not. 20/09/23

3. Write a C program to read the age of a candidate and determine whether its 20/09/23
eligible for casting his/her own vote.
4. Write a C program to find the largest of three numbers. 20/09/23

5. Write a C program to accept a coordinate point in a XY coordinate system and 20/09/23


determine in which quadrant the coordinate point lies.

6. Write a C program to check whether a triangle is Equilateral, Isosceles or 20/09/23


Scalene.

7. Write a C program to check whether an alphabet is a vowel or consonant. 20/09/23

8. Write a C program to read roll no., name and marks of three subjects and 20/09/23
calculate the total, percentage and division.

5
Mehnaaz Ansari
07701182023

LAB-4
S.No. Program/Code Name Date
1. Write a program in C to display the first 10 natural numbers. 27/09/23

2. Write a program in C to display the cube of the number up to given an 27/09/23


integer.

3. Write a program in C to display the n terms of odd natural number and their 27/09/23
sum.

4. Write a program in C to display the pattern like right angle triangle with a 27/09/23
number. The pattern like:

1
12
123
1234

5. Write a program in C to make such a pattern like right angle triangle with a 27/09/23
number which will repeat a number in a row.

The pattern like:

1
22
333
4444

6. Write a program in C to make such a pattern like right angle triangle with 27/09/23
number increased by 1.

The pattern like:

1
23
456
7 8 9 10

6
Mehnaaz Ansari
07701182023

7. Write a program in C to make such a pattern like a pyramid with numbers 27/09/23
increased by 1.

1
23
456
7 8 9 10

8. Write a program in C to make such a pattern like a pyramid with an asterisk. 27/09/23
*
**
***
****

9. Write a program in C to display the pattern like a diamond. 27/09/23

10. Write a C program to display Pascal's triangle. 27/09/23

LAB-5

7
Mehnaaz Ansari
07701182023

1. Print set of even number from 1 to 20 using continue 01/11/23

2. Print set of prime number from 1 to 30 using continue 01/11/23

3. Print set of odd number from 1 to 20 using continue 01/11/23

4. Write a program in C to make such a pattern like right angle triangle with 01/11/23
number increased by 1 using break.

The pattern like:

1
23
456
7 8 9 10

5. Write a program in C to make such a pattern like a pyramid with an asterisk 01/11/23
using break.
*
**
***
****

6. Write a C program to print day of week name using switch case 01/11/23

7. Write a C program to check whether an alphabet is vowel or consonant using 01/11/23


switch case

LAB-6

8
Mehnaaz Ansari
07701182023

1. Write a program in C to find the square of any number using the function 15/11/23

2. Write a program in C to swap two numbers using function 15/11/23

3. Write a program in C to check a given number is even or odd using the 15/11/23
function

4. Write a program in C to convert decimal number to binary number using the 15/11/23
function

5. Write a program in C to check whether a number is a prime number or not 15/11/23


using the function

6. Convert a binary number to decimal and vice-versa 15/11/23

7. Convert an octal Number to decimal and vice-versa 15/11/23

8. Convert a binary number to octal and vice-versa 15/11/23

9. Write a C program to find maximum and minimum between two numbers 15/11/23
using functions

10. Write a C program to print all perfect numbers between given interval using 15/11/23
functions

LAB-7

9
Mehnaaz Ansari
07701182023

1. Write a C program to find power of any number using recursion 22/11/23

2. Write a C program to print all natural numbers between 1 to n using 22/11/23


recursion

3. Write a C program to print all even or odd numbers in given range using 22/11/23
recursion

4. Write a C program to find sum of all natural numbers between 1 to n using 22/11/23
recursion

5. Write a C program to find sum of all even or odd numbers in given range 22/11/23
using recursion

6. Write a C program to find reverse of any number using recursion 22/11/23

7. Write a C program to check whether a number is palindrome or not using 22/11/23


recursion

8. Write a C program to find sum of digits of a given number using recursion 22/11/23

9. Write a C program to find factorial of any number using recursion 22/11/23

10. Write a C program to generate nth Fibonacci term using recursion 22/11/23

LAB-8

10
Mehnaaz Ansari
07701182023

1. Write a program in C to store elements in an array and print it 08/11/23

2. Write a program in C to read n number of values in an array and display it in 08/11/23


reverse order

3. Write a program in C to find the sum of all elements of the array 08/11/23

4. Write a program in C to copy the elements of one array into another array 08/11/23

5. Write a program in C to find the maximum and minimum element in an array 08/11/23

6. Write a program in C to separate odd and even integers in separate arrays 08/11/23

7. Write a program in C to count a total number of duplicate elements in an 08/11/23


array

8. Write a program in C to print all unique elements in an array 08/11/23

9. Write a program in C to count the frequency of each element of an array 08/11/23

10. Write a program in C to find the second largest element in an array 08/11/23

LAB-9

11
Mehnaaz Ansari
07701182023

1. Write a program in C to show the basic declaration of pointer 22/11/23

2. Write a C program to add two numbers using pointers 22/11/23

3. Write a C program to swap two numbers using pointers 22/11/23

4. Write a C program to input and print array elements using pointer 22/11/23

5. Write a C program to copy one array to another using pointers 22/11/23

6. Write a C program to swap two arrays using pointers 22/11/23

7. Write a C program to reverse an array using pointers 22/11/23

8. Write a C program to search an element in array using pointers 22/11/23

9. Write a C program to access two-dimensional array using pointers 22/11/23

10. Write a C program to add two matrix using pointers 22/11/23

LAB-10

12
Mehnaaz Ansari
07701182023

1. Write a program in C to input a string and print it 22/11/23

2. Write a program in C to find the length of a string without using library 22/11/23
function

3. Write a program in C to separate the individual characters from a string 22/11/23

4. Write a program in C to print individual characters of string in reverse order 22/11/23

5. Write a program in C to count the total number of words in a string 22/11/23

6. Write a program in C to compare two strings without using string library 22/11/23
functions

7. Write a program in C to count total number of alphabets, digits and special 22/11/23
characters in a string

8. Write a program in C to count total number of vowel or consonant in a string 22/11/23

9. Write a program in C to find maximum occurring character in a string 22/11/23

10. Write a C program to check whether a given substring is present in the given 22/11/23
string

LAB-1
EXPERIMENT -1
13
Mehnaaz Ansari
07701182023

Aim:
Write a program to display “This is my first C Program”

Algorithm:
Step1: Start
Step2: Print “This is my first C program”
Step3: Stop

Flow chart:

Source Code:

Output:

EXPERIMENT-2
Aim:

14
Mehnaaz Ansari
07701182023

Write a C program that prints the perimeter of a rectangle to take its height and width as Input

Algorithm:
Height, width, perimeter: type float
Step1: Start
Step2: Input the height of rectangle
Step3: Input the width of rectangle
Step4: Perimeter= 2*(height +width)
Step5: Print “Perimeter of rectangle”
Step6: Stop

Flow chart:

Source Code:

15
Mehnaaz Ansari
07701182023

Output:

EXPERIMENT-3
Aim:

16
Mehnaaz Ansari
07701182023

Write a C program to calculate area and circumference of a circle

Algorithm:
Radius, area, circumference: type float
Step1: Start
Step2: Input the radius of circle
Step3: Area= 3.14*radius*radius
Step4: Print “Area of the circle”
Step5: Circumference= 2*3.14*radius
Step6: Print “Circumference of circle”
Step7: Stop

Flow chart:

Source Code:

17
Mehnaaz Ansari
07701182023

Output:

EXPERIMENT-4
Aim:
Write a C Program to perform addition, subtraction, multiplication and division of two numbers

18
Mehnaaz Ansari
07701182023

Algorithm:
a, b, addition, subtraction, multiplication, division: type float
Step1: Start
Step2: Input a
Step3: Input b
Step4: Addition= a+ b
Step5: Subtraction= a- b
Step6: Multiplication= a* b
Step7: Division = a/b
Step8: Print “Addition”
Step9: Print “Subtraction”
Step10: Print “Multiplication”
Step11: Print “Division”
Step12: Stop

Flow chart:

Source Code:

19
Mehnaaz Ansari
07701182023

Output:

EXPERIMENT-5
20
Mehnaaz Ansari
07701182023

Aim:
Write a C Program to evaluate each of the following equations.
1. v = u+ gt
2. s = ut + ½ gt2
3. v2 = u2 + 2gs

Algorithm:
v, u, g, t, s, b: type float
Step1: Start
Step2: Input u
Step3: Input g
Step4: Input t
Step5: v=u+ g*t
Step6: Print “Final velocity v”
Step7: s= u*t + g*t*t
Step8: Print “Displacement s”
Step9: b= u*u + 2*g*s
Step10: Print “Vsquare b”
Step11: Stop

Flow chart:

Source Code:

21
Mehnaaz Ansari
07701182023

Output:

22
Mehnaaz Ansari
07701182023

EXPERIMENT-6
Aim:
Write a C program that takes hours and minutes as Input, and calculates the total number of minutes

Algorithm:
hours, minutes, totalminutes: type float
Step1: Start
Step2: Input no. of hours
Step3: Input minutes
Step4: Totalminutes= (hours*60) + minutes
Step5: Print “Total minutes”
Step6: Stop

Flow chart:

Source Code:

23
Mehnaaz Ansari
07701182023

Output:

24
Mehnaaz Ansari
07701182023

EXPERIMENT-7
Aim:
Write a program in C that reads a forename, surname and year of birth and display the names and the year
one after another sequentially.

Algorithm:
forename, surname: type char
yearofbirth: type integer
Step1: Start
Step2 Input the forename
Step3: Input the surname
Step4: Input the yearofbirth
Step5: Print “Forename, surname, year of birth”
Step6: Stop

Flow chart:

Source Code:

25
Mehnaaz Ansari
07701182023

Output:

26
Mehnaaz Ansari
07701182023

EXPERIMENT-8
Aim:
Write a program that converts Centigrade to Fahrenheit.

Algorithm:
Centigrade, Fahrenheit: type float
Step1: Input temperature in centigrade
Step2: Fahrenheit = centigrade*(9/5) + 32
Step3: Print “Temperature in Fahrenheit”
Step4: Stop

Flow chart:

Source Code:

27
Mehnaaz Ansari
07701182023

Output:

28
Mehnaaz Ansari
07701182023

EXPERIMENT-9
Aim:
C program to calculate the value of nCr

Source code:

Output:

29
Mehnaaz Ansari
07701182023

EXPERIMENT-10
Aim:
Write a C program to extract the last two digits from a given year.

Algorithm:

Year, lasttwodigits: type integer


Step1: Start
Step2: Input year
Step3: lasttwodigits= year%100
Step4: Print “Last two digits”
Step5: Stop

Flow chart:

Source Code:

30
Mehnaaz Ansari
07701182023

Output:

31
Mehnaaz Ansari
07701182023

LAB-2
EXPERIMENT-1
Aim:
Write a program to find the following +, -, *, / and %, between two numbers

Source Code:
#include <stdio.h>
int main () {
float a, b, addition, subtraction, multiplication, modulus;
float division;
printf("a: ");
scanf("%f", &a);
printf("b: ");
scanf("%f", &b);
addition= a+ b;
printf("addition is: %f \n", addition);
subtraction= a-b;
printf("subtraction is: %f \n", subtraction);
multiplication= a*b;
printf("multiplication is: %f \n", multiplication);
division = a/b;
modulus= a % b;
if (b==0) {
printf("division and modulus not defined");
} else {
printf("division is: %f \n", division);
printf("modulus is: %f \n", modulus);
}
return 0;
}

Output:

32
Mehnaaz Ansari
07701182023

EXPERIMENT-2
Aim:
Write a program to find the following a++, a--, ++a and –a

Source Code:
#include <stdio.h>
int main () {
int a;
printf("Enter your Number: ");
scanf("%d", &a);
printf("post increment: %d \n", a++);
printf("post decrement: %d \n", a--);
printf("pre increment: %d \n",++a);
printf("post decrement: %d \n",--a);
return 0;
}

Output:

33
Mehnaaz Ansari
07701182023

EXPERIMENT-3
Aim:
Write a program to find the following a+=b, a-=b, a*=b, a/=b and a%=b between two numbers.

Source Code:
#include <stdio.h>
int main () {
int a,b;
printf("Enter 1st number: ");
scanf("%d", &a);
printf("Enter 2nd number: ");
scanf("%d", &b);
printf("1st equation: %d \n", a+=b);
printf("2nd equation: %d \n", a-=b);
printf("3rd equation: %d \n", a*=b);
printf("4th equation: %d \n", a/=b);
printf("5th equation: %d ",a%=b);
return 0;
}

Output:

34
Mehnaaz Ansari
07701182023

EXPERIMENT-4
Aim:
Write a program to check whether two numbers are ==, <, >,
>=, <= and !=

Source Code:
#include <stdio.h>
int main () {
int a, b;
printf("Enter 1st number: ");
scanf("%d", &a);
printf("Enter 2nd number: ");
scanf("%d", &b);
printf("1st equation: %d \n", a==b);
printf("2nd equation: %d \n", a<b);
printf("3rd equation: %d \n", a>b);
printf("4th equation: %d \n", a>=b);
printf("5th equation: %d \n", a<=b);
printf("6th equation: %d ",a!=b);
return 0;
}

Output:

35
Mehnaaz Ansari
07701182023

36
Mehnaaz Ansari
07701182023

EXPERIMENT-5
Aim:
Write a program to find the following result.
Int a=5, b=5, c=10, result;
a) Result = (a==b) && (c>b);
b) Result = (a==b) && (c<b);
c) Result = (a==b) || (c<b);
d) Result = (a!=b) || (c<b);
e) Result = !(a!=b);
f) Result = !(a==b);

Source Code:
#include <stdio.h>
int main () {
int a=5, b=5, c=10;
printf("1st result: %d \n",(a==b)&&(c>b));
printf("2nd result: %d \n",(a==b)&&(c<b));
printf("3rd result: %d \n",(a==b)||(c<b));
printf("4th result: %d \n",(a!=b)||(c<b));
printf("5th result: %d \n",!(a!=b));
printf("6th result: %d ",!(a==b));
return 0;
}

Output:

37
Mehnaaz Ansari
07701182023

EXPERIMENT-6
Aim:
Write a program to find the following &, >>, <<, |, ~, ^ between two numbers.

Source Code:
#include <stdio.h>
int main () {
int a, b;
printf("Enter 1st number: ");
scanf("%d", &a);
printf("Enter 2nd number: ");
scanf("%d", &b);
printf("& Result:%d\n", a & b);
printf(">> Result:%d\n", a>>1);
printf("<< Result:%d\n", a<<1);
printf("| Result:%d\n", a | b);
printf("~ Result:%d\n", ~a);
printf("^ Result:%d", a ^ b);
return 0;
}

Output:

38
Mehnaaz Ansari
07701182023

EXPERIMENT-7
Aim:
Write a program to find the size of the following data types int, float, char, string, long int, double, long
float.

Source Code:
#include <stdio.h>
int main () {
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of float: %lu bytes\n", sizeof(float));
printf("Size of char: %lu bytes\n", sizeof(char));
printf("Size of long int: %lu bytes\n", sizeof(long int));
printf("Size of double: %lu bytes\n", sizeof(double));
printf("Size of long double: %lu bytes\n", sizeof(long double));
char str [] = "Hello, World!";
printf("Size of string: %lu bytes\n", sizeof(str));
return 0;
}

Output:

39
Mehnaaz Ansari
07701182023

LAB-3
EXPERIMENT-1
Aim:
Write a C program to check whether a given number is even or odd.

Source code:
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d",&number);
if (number%2==0){
printf("Number is even",number);
}
else {
printf("Number is odd",number);
}
return 0;
}

Output:

40
Mehnaaz Ansari
07701182023

EXPERIMENT-2
Aim:
Write a C program to find whether a given year is a leap year or not.

Source code:
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d",&year);
if (year%4==0){
printf("Its a leap year",year);
}
else {
printf("Not a leap year",year);
}
return 0;
}

Output:

41
Mehnaaz Ansari
07701182023

EXPERIMENT-3
Aim:
Write a C program to read the age of a candidate and determine whether its eligible for casting his/her own
vote.

Source code:
#include <stdio.h>
int main() {
int age;
printf("Enter the age of candidate: ");
scanf("%d",&age);
if (age>=18){
printf("Candidate can cast their vote",age);
}
else {
printf("Candidate cannot cast their vote",age);
}
return 0;
}

Output:

42
Mehnaaz Ansari
07701182023

EXPERIMENT-4
Aim:
Write a C program to find the largest of three numbers.

Source code:
#include <stdio.h>
int main() {
int a,b,c;
printf("Enter 1st number: ");
scanf("%d",&a);
printf("Enter 2nd number: ");
scanf("%d",&b);
printf("Enter 3rd number: ");
scanf("%d",&c);
if(a>b && a>c){
printf("The largest nuber is %d",a); }
else if(b>a && b>c){
printf("The largest number is %d",b); }
else{
printf("The largest number is %d",c);
}
return 0;
}

Output:

43
Mehnaaz Ansari
07701182023

EXPERIMENT-5
Aim:
Write a C program to accept a coordinate point in a XY coordinate system and determine in which quadrant
the coordinate point lies.

Source code:
#include <stdio.h>
int main() {
float x,y;
printf("Enter the x coordinate: ");
scanf("%f",&x);
printf("Enter the y coordinate: ");
scanf("%f",&y);
if (x>0 && y>0){
printf("The point (%f,%f) lies in first quadrant\n",x,y);
}else if (x<0 && y>0){
printf("The point (%f,%f) lies in second quadrant\n",x,y);}
else if (x<0 && y<0){
printf("The point (%f,%f) lies in third quadrant\n",x,y);}
else if (x>0 && y<0){
printf("The point (%f,%f) lies in fourth quadrant\n",x,y);}
else if (x==0 && y==0){
printf("The point (%f,%f) is at the origin\n",x,y);}
else if (x==0){
printf("The point (%f,%f) lies on the y axis\n",x,y);}
else {
printf("The point (%f,%f) lies on the x axis\n",x,y);
}
return 0;
}

Output:

44
Mehnaaz Ansari
07701182023

EXPERIMENT-6
Aim:
Write a C program to check whether a triangle is Equilateral, Isosceles or Scalene.

Source code:
#include <stdio.h>
int main() {
float a,b,c;
printf("Enter a: ");
scanf("%f",&a);
printf("Enter b: ");
scanf("%f",&b);
printf("Enter c: ");
scanf("%f",&c);
if (a==b && b==c){
printf("This is an equilateral triangle\n");
}else if (a==b || a==c){
printf("This is an isosceles triangle\n");}
else {
printf("This is a scalene triangle");
}
return 0;
}

Output:

45
Mehnaaz Ansari
07701182023

EXPERIMENT-7
Aim:
Write a C program to check whether an alphabet is a vowel or consonant.

Source code:
#include <stdio.h>
int main() {
char alphabet;
printf("Enter an alphabet : ");
scanf("%c",&alphabet);

if (alphabet== 'a' || alphabet== 'e' || alphabet== 'i' || alphabet== 'o' || alphabet== 'u' || alphabet== 'A' ||
alphabet== 'E' || alphabet== 'I' || alphabet== 'O' || alphabet== 'U'){
printf("%c is a vowel\n",alphabet);
}
else {
printf("%c is a consonant",alphabet);
}
return 0;
}

Output:

46
Mehnaaz Ansari
07701182023

EXPERIMENT-8
Aim:
Write a C program to read roll no., name and marks of three subjects and calculate the total, percentage and
division.

Source code:
#include <stdio.h>
int main() {
char name[20];
int rollno,marks1,marks2,marks3;
float totalmarks,percentage;
printf("Enter Roll Number: ");
scanf("%d",&rollno);
printf("Enter Name: ");
scanf(" %[^\n]", name);
printf("Enter Marks for Subject1: ",marks1);
scanf("%d",&marks1);
printf("Enter Marks for Subject2: ",marks2);
scanf("%d",&marks2);
printf("Enter Marks for Subject3: ",marks3);
scanf("%d",&marks3);
totalmarks= marks1+marks2+marks3;
printf("Total Marks:%f \n",totalmarks);
percentage= (totalmarks/300)*100;
printf("Percentage:%f \n",percentage);
if(percentage>=80){
printf("Passed with 1st division");}
else if(percentage<80 && percentage>=60){
printf("Passed with 2nd divison");
}else if(percentage<60 && percentage>=40){
printf("Passed with 3rd division");
}else{
printf("Fail");
}
return 0;
}
Output:

47
Mehnaaz Ansari
07701182023

LAB-4
EXPERIMENT-1
Aim:
Write a program in C to display the first 10 natural numbers

Source code:
#include <stdio.h>
int main() {
int i;
printf("The first 10 natural numbers are:\n");
for (i=1;i<=10;i++){
printf("%d \n",i);
}
return 0;
}

Output:

48
Mehnaaz Ansari
07701182023

EXPERIMENT-2
Aim:
Write a program in C to display the cube of the number up to given an integer

Source code:
#include <stdio.h>
int main() {
int i,a;
printf("Enter an integer: ");
scanf("%d",&a);
printf("Cubes of number from 1 to %d are: \n",a);
for (i=1;i<=a;i++){
int cube= i*i*i;
printf("Number is:%d and cube of %d is:%d \n",i,i,cube);
}
return 0;
}

Output:

49
Mehnaaz Ansari
07701182023

EXPERIMENT-3
Aim:
Write a program in C to display the n terms of odd natural number and their sum

Source code:
#include <stdio.h>
int main() {
int i,a,sum;
printf("Enter number of terms: ");
scanf("%d",&a);
printf("The odd numbers are:");
for (i=1; a>0 ;i+=2){
printf("%d \t",i);
sum +=i;
a--;
}
printf("\nThe sum of odd natural numbers upto %d terms is: %d",a,sum);
return 0;
}

Output:

50
Mehnaaz Ansari
07701182023

EXPERIMENT-4
Aim:
Write a program in C to display the pattern like right angle triangle with a number. The pattern like:

1
12
123
1234

Source code:
#include <stdio.h>
int main() {
int n=4,i,j;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf("%d",j);
}printf("\n");
}
return 0;
}

Output:

51
Mehnaaz Ansari
07701182023

EXPERIMENT-5
Aim:
Write a program in C to make such a pattern like right angle triangle with a number which will repeat a
number in a row.

The pattern like:

1
22
333
4444

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

Output:

52
Mehnaaz Ansari
07701182023

EXPERIMENT-6
Aim:
Write a program in C to make such a pattern like right angle triangle with number increased by 1.

The pattern like:

1
23
456
7 8 9 10

Source code:
#include <stdio.h>
int main() {
int n,i,j,k=1;
printf("Enter n: ");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf("%d",k++);
}
printf("\n");
}

return 0;
}

Output:

53
Mehnaaz Ansari
07701182023

EXPERIMENT-7
Aim:
Write a program in C to make such a pattern like a pyramid with numbers increased by 1.

1
23
456
7 8 9 10

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

Output:

54
Mehnaaz Ansari
07701182023

EXPERIMENT-8
Aim:
Write a program in C to make such a pattern like a pyramid with an asterisk.
*
**
***
****

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

Output:

55
Mehnaaz Ansari
07701182023

EXPERIMENT-9
Aim:
Write a program in C to display the pattern like a diamond.

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

56
Mehnaaz Ansari
07701182023

EXPERIMENT-10
Aim:
Write a C program to display Pascal's triangle.

Source code:
#include <stdio.h>
unsigned long long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
unsigned long long nCr(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
int main() {
int rows;
printf("Enter the number of rows for Pascal's Triangle: ");
scanf("%d", &rows);
for (int i = 0; i < rows; i++) {
for (int space = 0; space < rows - i; space++) {
printf(" ");
}
for (int j = 0; j <= i; j++) {
printf("%4llu", nCr(i, j));
}
printf("\n");
}
return 0;
}

Output:

57
Mehnaaz Ansari
07701182023

LAB-5
EXPERIMENT-1
Aim:
Print set of even number from 1 to 20 using continue.

Source code:
#include <stdio.h>
int main() {
for(int i=1;i<=20;i++){
if(i%2 !=0){
continue;
}
printf("%d \n",i);
}
return 0;
}

Output:

58
Mehnaaz Ansari
07701182023

EXPERIMENT-2
Aim:
Print set of prime number from 1 to 30 using continue

Source code:
#include <stdio.h>
int is_prime(int num) {
if (num <= 1) {
return 0; }
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;}
int main() {
printf("Prime numbers from 1 to 30:\n");
for (int i = 1; i <= 30; i++) {
if (!is_prime(i)) {
continue;
}
printf("%d ", i);
}
return 0;}

Output:

59
Mehnaaz Ansari
07701182023

EXPERIMENT-3
Aim:
Print set of odd number from 1 to 20 using continue

Source code:
#include <stdio.h>
int main() {
for(int i=1;i<=20;i++){
if(i%2==0){
continue;
}
printf("%d \n",i);
}
return 0;
}

Output:

60
Mehnaaz Ansari
07701182023

EXPERIMENT-4
Aim:
Write a program in C to make such a pattern like right angle triangle with number increased by 1 using
break.

The pattern like:

1
23
456
7 8 9 10

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

61
Mehnaaz Ansari
07701182023

EXPERIMENT-5
Aim:
Write a program in C to make such a pattern like a pyramid with an asterisk using break.
*
**
***
****

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

Output:

62
Mehnaaz Ansari
07701182023

EXPERIMENT-6
Aim:
Write a C program to print day of week name using switch case

Source code:
#include <stdio.h>
int main() {
int day;
printf("Enter a number from 1-7: ");
scanf("%d",&day);
switch(day){
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Incorrect number!");
}
return 0;
}

Output:

63
Mehnaaz Ansari
07701182023

EXPERIMENT-7
Aim:
Write a C program to check whether an alphabet is vowel or consonant using switch case.

Source code:
#include <stdio.h>
int main() {
char alphabet;
printf("Enter an alphabet: ");
scanf(" %c",&alphabet);
switch(alphabet){
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a vowel",alphabet);
break;
default:
printf("%c is a consonant",alphabet);
} return 0;}

Output:

64
Mehnaaz Ansari
07701182023

LAB-6
EXPERIMENT-1
Aim:
Write a program in C to find the square of any number using the function

Source code:
#include <stdio.h>
int square(int num){
return num*num;
}
int main() {
int number,result;
printf("Enter a number: ");
scanf("%d",&number);
result = square(number);
printf("Square of %d is %d\n",number,result);
return 0;
}

Output:

65
Mehnaaz Ansari
07701182023

EXPERIMENT-2
Aim:
Write a program in C to swap two numbers using function

Source code:
#include <stdio.h>
void swap(int *a, int *b) {
int k = *a;
*a = *b;
*b = k;
}
int main() {
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}

Output:

66
Mehnaaz Ansari
07701182023

EXPERIMENT-3
Aim:
Write a program in C to check a given number is even or odd using the function

Source code:
#include <stdio.h>
void checkEvenOdd(int num) {
if (num %2 ==0) {
printf("%d is even", num);
}
else{
printf("%d is odd", num);
}
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
checkEvenOdd(number);
return 0;
}

Output:

67
Mehnaaz Ansari
07701182023

EXPERIMENT-4
Aim:
Write a program in C to convert decimal number to binary number using the function

Source code:
#include <stdio.h>
void decimalToBinary(int decimal) {
int binary[32];
int i=0;
while (decimal>0){
binary[i]=decimal%2;
decimal/=2;
i++;
}
printf("Binary: ");
for (int j=i-1;j>=0;j--){
printf("%d",binary[j]);
}
printf("\n");}
int main() {
int decimalnumber;
printf("Enter a decimal number: ");
scanf("%d", &decimalnumber);
decimalToBinary(decimalnumber);
return 0;}

Output:

68
Mehnaaz Ansari
07701182023

EXPERIMENT-5
Aim:
Write a program in C to check whether a number is a prime number or not using the function

Source code:
#include <stdio.h>
int primeOrNot(int num) {
if (num<=1){
return 0;}
for (int i=2;i<num;i++){
if (num%i==0){
return 0;}
}
return 1;}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if(primeOrNot(number)){
printf("%d is a prime number\n",number);
}else{
printf("%d is not a prime number\n",number);
}return 0;}

Output:

69
Mehnaaz Ansari
07701182023

EXPERIMENT-6
Aim:
Convert a binary number to decimal and vice-versa

Source code:
#include <stdio.h>
#include <math.h>
int binaryToDecimal(long long binaryNumber) {
int decimalNumber = 0, remainder, i = 0;
while (binaryNumber != 0) {
remainder = binaryNumber % 10;
binaryNumber /= 10;
decimalNumber += remainder * pow(2, i);
++i; }
return decimalNumber;
}
long long decimalToBinary(int decimalNumber) {
long long binaryNumber = 0;
int remainder, i = 1, step = 1;
while (decimalNumber != 0) {
remainder = decimalNumber % 2;
decimalNumber /= 2;
binaryNumber += remainder * i;
i *= 10; }
return binaryNumber;
}
int main() {
int choice;
long long binaryNumber;
int decimalNumber;
printf("Choose conversion:\n");

70
Mehnaaz Ansari
07701182023

printf("1. Binary to Decimal\n");


printf("2. Decimal to Binary\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a binary number: ");
scanf("%lld", &binaryNumber);
printf("Decimal equivalent: %d\n", binaryToDecimal(binaryNumber));
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);
printf("Binary equivalent: %lld\n", decimalToBinary(decimalNumber));
break;
default:
printf("Invalid choice\n");
}
return 0;}

Output:

71
Mehnaaz Ansari
07701182023

EXPERIMENT-7
Aim:
Convert an octal Number to decimal and vice-versa

Source code:
#include <stdio.h>
#include <math.h>
int octalToDecimal(long long octalNumber) {
int decimalNumber = 0, i = 0, remainder;
while (octalNumber != 0) {
remainder = octalNumber % 10;
octalNumber /= 10;
decimalNumber += remainder * pow(8, i);
++i; }
return decimalNumber;}
long long decimalToOctal(int decimalNumber) {
long long octalNumber = 0;
int i = 1, remainder;
while (decimalNumber != 0) {
remainder = decimalNumber % 8;
decimalNumber /= 8;
octalNumber += remainder * i;
i *= 10; }
return octalNumber;}
int main() {
int choice;
long long octalNumber;
int decimalNumber;
printf("Choose conversion:\n");
printf("1. Octal to Decimal\n");
printf("2. Decimal to Octal\n");

72
Mehnaaz Ansari
07701182023

printf("Enter your choice (1 or 2): ");


scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter an octal number: ");
scanf("%lld", &octalNumber);
printf("Decimal equivalent: %d\n", octalToDecimal(octalNumber));
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);
printf("Octal equivalent: %lld\n", decimalToOctal(decimalNumber));
break;
default:
printf("Invalid choice\n");
}
return 0;}

Output:

73
Mehnaaz Ansari
07701182023

EXPERIMENT-8
Aim:
Convert a binary number to octal and vice-versa

Source code:
#include <stdio.h>
#include <math.h>
long long binaryToOctal(long long binaryNumber) {
int octalNumber = 0, decimalNumber = 0, i = 0, remainder;
while (binaryNumber != 0) {
remainder = binaryNumber % 10;
binaryNumber /= 10;
decimalNumber += remainder * pow(2, i);
++i;
}
i = 1;
while (decimalNumber != 0) {
remainder = decimalNumber % 8;
decimalNumber /= 8;
octalNumber += remainder * i;
i *= 10;
}
return octalNumber;
}
long long octalToBinary(long long octalNumber) {
long long binaryNumber = 0, i = 1, remainder;
while (octalNumber != 0) {
remainder = octalNumber % 10;
octalNumber /= 10;
binaryNumber += remainder * i;
i *= 8;
}
long long reversedBinary = 0, base = 1;
while (binaryNumber > 0) {
remainder = binaryNumber % 2;
reversedBinary = reversedBinary + remainder * base;
binaryNumber /= 2;
base = base * 10;
}
return reversedBinary;
}
int main() {
int choice;
long long binaryNumber;
long long octalNumber;
printf("Choose conversion:\n");
printf("1. Binary to Octal\n");
printf("2. Octal to Binary\n");
74
Mehnaaz Ansari
07701182023

printf("Enter your choice (1 or 2): ");


scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a binary number: ");
scanf("%lld", &binaryNumber);
printf("Octal equivalent: %lld\n", binaryToOctal(binaryNumber));
break;
case 2:
printf("Enter an octal number: ");
scanf("%lld", &octalNumber);
printf("Binary equivalent: %lld\n", octalToBinary(octalNumber));
break;
default:
printf("Invalid choice\n");
}
return 0;
}

Output:

75
Mehnaaz Ansari
07701182023

EXPERIMENT-9
Aim:
Write a C program to find maximum and minimum between two numbers using functions

Source code:
#include <stdio.h>
int findMaximum(int num1, int num2) {
return (num1 > num2) ? num1 : num2;
}
int findMinimum(int num1, int num2) {
return (num1 < num2) ? num1 : num2;
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
int maximum = findMaximum(num1, num2);
int minimum = findMinimum(num1, num2);
printf("Maximum: %d\n", maximum);
printf("Minimum: %d\n", minimum);
return 0;
}

Output:

76
Mehnaaz Ansari
07701182023

EXPERIMENT-10
Aim:
Write a C program to print all perfect numbers between given interval using functions

Source code:
#include <stdio.h>
int isPerfect(int num) {
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;}
} return (sum == num); }
void printPerfectNumbers(int start, int end) {
printf("Perfect numbers between %d and %d are:\n", start, end);
for (int i = start; i <= end; i++) {
if (isPerfect(i)) {
printf("%d\n", i); } } }
int main() {
int start, end;
printf("Enter the start of the interval: ");
scanf("%d", &start);
printf("Enter the end of the interval: ");
scanf("%d", &end);
printPerfectNumbers(start, end);
return 0;}

Output:

77
Mehnaaz Ansari
07701182023

LAB-7
EXPERIMENT-1
Aim:
Write a C program to find power of any number using recursion

Source code:
#include <stdio.h>
double power(double base, int exponent) {
if (exponent == 0) {
return 1;
}
return base * power(base, exponent - 1);
}
int main() {
double base;
int exponent;
printf("Enter the base: ");
scanf("%lf", &base);
printf("Enter the exponent: ");
scanf("%d", &exponent);
double result = power(base, exponent);
printf("%.2lf ^ %d = %.2lf\n", base, exponent, result);
return 0;
}

Output:

78
Mehnaaz Ansari
07701182023

EXPERIMENT-2
Aim:
Write a C program to print all natural numbers between 1 to n using recursion.

Source code:
#include <stdio.h>
void printNaturalNumbers(int start, int n) {
if (start > n) {
return;
}
printf("%d ", start);
printNaturalNumbers(start + 1, n);
}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Natural numbers between 1 and %d are: ", n);
printNaturalNumbers(1, n);
printf("\n");
return 0;
}

Output:

79
Mehnaaz Ansari
07701182023

EXPERIMENT-3
Aim:
Write a C program to print all even or odd numbers in given range using recursion

Source code:
#include <stdio.h>
void printEvenOdd(int start, int end, int type) {
if (start > end) {
return;}
if ((start % 2 == 0 && type == 1) || (start % 2 != 0 && type == 2)) {
printf("%d ", start);}
printEvenOdd(start + 1, end, type);}
int main() {
int start, end, choice;
printf("Enter the start of the range: ");
scanf("%d", &start);
printf("Enter the end of the range: ");
scanf("%d", &end);
printf("Choose even (1) or odd (2): ");
scanf("%d", &choice);
if (choice == 1) {
printf("Even numbers between %d and %d are: ", start, end);
printEvenOdd(start, end, 1);
} else if (choice == 2) {
printf("Odd numbers between %d and %d are: ", start, end);
printEvenOdd(start, end, 2);
} else {
printf("Invalid choice\n");
return 1;}
printf("\n");
return 0;

80
Mehnaaz Ansari
07701182023

Output:

81
Mehnaaz Ansari
07701182023

EXPERIMENT-4
Aim:
Write a C program to find sum of all natural numbers between 1 to n using recursion

Source code:
#include <stdio.h>
int sumOfNaturalNumbers(int n) {
if (n == 0) {
return 0;
}
return n + sumOfNaturalNumbers(n - 1);
}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
int sum = sumOfNaturalNumbers(n);
printf("Sum of natural numbers between 1 and %d is: %d\n", n, sum);
return 0;
}

Output:

82
Mehnaaz Ansari
07701182023

EXPERIMENT-5
Aim:
Write a C program to find sum of all even or odd numbers in given range using recursion

Source code:
#include <stdio.h>
int sumEvenOdd(int start, int end, int type) {
if (start > end) {
return 0;
}
if ((start % 2 == 0 && type == 1) || (start % 2 != 0 && type == 2)) {
return start + sumEvenOdd(start + 1, end, type);
}
return sumEvenOdd(start + 1, end, type);
}
int main() {
int start, end, choice;
printf("Enter the start of the range: ");
scanf("%d", &start);
printf("Enter the end of the range: ");
scanf("%d", &end);
printf("Choose even (1) or odd (2): ");
scanf("%d", &choice);
int sum = sumEvenOdd(start, end, choice);
if (choice == 1) {
printf("Sum of even numbers between %d and %d is: %d\n", start, end, sum);
} else if (choice == 2) {
printf("Sum of odd numbers between %d and %d is: %d\n", start, end, sum);
} else {
printf("Invalid choice\n");
return 1;

83
Mehnaaz Ansari
07701182023

}
return 0;
}

Output:

84
Mehnaaz Ansari
07701182023

EXPERIMENT-6
Aim:
Write a C program to find reverse of any number using recursion.

Source code:
#include <stdio.h>
int reverseNumber(int num, int reversedNum) {
if (num == 0) {
return reversedNum;
}
return reverseNumber(num / 10, reversedNum * 10 + num % 10);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
int reversed = reverseNumber(num, 0);
printf("Reverse of %d is: %d\n", num, reversed);
return 0;
}

Output:

85
Mehnaaz Ansari
07701182023

EXPERIMENT-7
Aim:
Write a C program to check whether a number is palindrome or not using recursion

Source code:
#include <stdio.h>
int reverseNumber(int num, int reversedNum) {
if (num == 0) {
return reversedNum;
}
return reverseNumber(num / 10, reversedNum * 10 + num % 10);
}
int isPalindrome(int num) {
int reversed = reverseNumber(num, 0);
return (num == reversed);}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPalindrome(num)) {
printf("%d is a palindrome.\n", num);
} else {
printf("%d is not a palindrome.\n", num);}
return 0;}

Output:

86
Mehnaaz Ansari
07701182023

EXPERIMENT-8
Aim:
Write a C program to find sum of digits of a given number using recursion

Source code:
#include <stdio.h>
int sumOfDigits(int num) {
if (num == 0) {
return 0;
}
return (num % 10) + sumOfDigits(num / 10);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
int sum = sumOfDigits(num);
printf("Sum of digits of %d is: %d\n", num, sum);
return 0;
}

Output:

87
Mehnaaz Ansari
07701182023

EXPERIMENT-9
Aim:
Write a C program to find factorial of any number using recursion.

Source code:
#include <stdio.h>
unsigned long long factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
return 1;
}
unsigned long long result = factorial(num);
printf("Factorial of %d is: %llu\n", num, result);
return 0;
}

Output:

88
Mehnaaz Ansari
07701182023

EXPERIMENT-10
Aim:
Write a C program to generate nth Fibonacci term using recursion.

Source code:
#include <stdio.h>
unsigned long long fibonacci(int n) {
if (n == 0 || n == 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
if (n < 0) {
printf("Fibonacci term is not defined for negative numbers.\n");
return 1;
}
unsigned long long result = fibonacci(n);
printf("The %dth Fibonacci term is: %llu\n", n, result);
return 0;
}

Output:

89
Mehnaaz Ansari
07701182023

LAB-8
EXPERIMENT-1
Aim:
Write a program in C to store elements in an array and print it

Source code:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
int arr[n];
printf("Enter %d elements:\n",n);
for (int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("Elements in the array: ");
for (int i=0;i<n;i++){
printf(" %d",arr[i]);
}
printf("\n");
return 0;
}

Output:

90
Mehnaaz Ansari
07701182023

EXPERIMENT-2
Aim:
Write a program in C to read n number of values in an array and display it in reverse order

Source code:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
int arr[n];
printf("Enter %d elements:\n",n);
for (int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("Elements in reverse order in the array: ");
for (int i=n-1;i>=0;i--){
printf(" %d",arr[i]);
}
printf("\n");
return 0;
}

Output:

91
Mehnaaz Ansari
07701182023

EXPERIMENT-3
Aim:
Write a program in C to find the sum of all elements of the array

Source code:
#include <stdio.h>
int main() {
int n,sum=0;
printf("Enter the number of elements: ");
scanf("%d",&n);
int arr[n];
printf("Enter %d elements:\n",n);
for (int i=0;i<n;i++){
scanf("%d",&arr[i]);
sum+=arr[i];
}
printf("Sum of elements in the array:%d \n",sum);
return 0;
}

Output:

92
Mehnaaz Ansari
07701182023

EXPERIMENT-4
Aim:
Write a program in C to copy the elements of one array into another array

Source code:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
int arr1[n];
int arr2[n];
printf("Enter %d elements:\n",n);
for (int i=0;i<n;i++){
scanf("%d",&arr1[i]);
}
for (int i=0;i<n;i++){
arr2[i]=arr1[i];
}
printf("Elements in another array: ");
for (int i=0;i<n;i++){
printf(" %d",arr2[i]);
} printf("\n");
return 0;}

Output:

93
Mehnaaz Ansari
07701182023

EXPERIMENT-5
Aim:
Write a program in C to find the maximum and minimum element in an array

Source code:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
int arr[n];
printf("Enter %d elements:\n",n);
for (int i=0;i<n;i++){
scanf("%d",&arr[i]);}
int max = arr[0];
int min = arr[0];
for (int i=1;i<n;i++){
if(arr[i]>max){
max= arr[i];}
if(arr[i]<min){
min= arr[i];}
}printf("maximum element:%d\n",max);
printf("minimum element:%d",min);
return 0;}

Output:

94
Mehnaaz Ansari
07701182023

EXPERIMENT-6
Aim:
Write a program in C to separate odd and even integers in separate arrays

Source code:
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);}
int evenArr[n], oddArr[n];
int evenCount = 0, oddCount = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
evenArr[evenCount++] = arr[i];
} else {
oddArr[oddCount++] = arr[i];}
}
printf("\nEven integers array: ");
for (int i = 0; i < evenCount; i++) {
printf("%d ", evenArr[i]);
}
printf("\nOdd integers array: ");
for (int i = 0; i < oddCount; i++) {
printf("%d ", oddArr[i]);
}
printf("\n");
return 0;
}
Output:

95
Mehnaaz Ansari
07701182023

EXPERIMENT-7
Aim:
Write a program in C to count a total number of duplicate elements in an array

Source code:
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]); }
int duplicateCount = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
duplicateCount++;
break;} } }
printf("Total number of duplicate elements: %d\n", duplicateCount);
return 0;}

Output:

96
Mehnaaz Ansari
07701182023

EXPERIMENT-8
Aim:
Write a program in C to print all unique elements in an array

Source code:
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("Unique elements in the array: ");
for (int i = 0; i < n; i++) {
int isUnique = 1;
for (int j = 0; j < n; j++) {
if (i != j && arr[i] == arr[j]) {
isUnique = 0;
break;
}
}
if (isUnique) {
printf("%d ", arr[i]);
}
}
printf("\n");
return 0;
}
Output:

97
Mehnaaz Ansari
07701182023

EXPERIMENT-9
Aim:
Write a program in C to count the frequency of each element of an array

Source code:
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
int frequency[n];
for (int i = 0; i < n; i++) {
frequency[i] = -1;
}
for (int i = 0; i < n; i++) {
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
frequency[j] = 0;
}
}
if (frequency[i] != 0) {
frequency[i] = count;
}

98
Mehnaaz Ansari
07701182023

}
printf("Frequency of each element:\n");
for (int i = 0; i < n; i++) {
if (frequency[i] != 0) {
printf("%d occurs %d time(s)\n", arr[i], frequency[i]);
}
}
return 0;
}

Output:

99
Mehnaaz Ansari
07701182023

EXPERIMENT-10
Aim:
Write a program in C to find the second largest element in an array

Source code:
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
int firstMax, secondMax;
if (arr[0] > arr[1]) {
firstMax = arr[0];
secondMax = arr[1];
} else {
firstMax = arr[1];
secondMax = arr[0];
}
for (int i = 2; i < n; i++) {
if (arr[i] > firstMax) {
secondMax = firstMax;
firstMax = arr[i];
} else if (arr[i] > secondMax && arr[i] != firstMax) {
secondMax = arr[i]; }
}
if (n < 2) {
printf("Array size is too small to find the second largest element.\n");
} else {
printf("Second largest element is: %d\n", secondMax);
}
return 0;}
Output:

100
Mehnaaz Ansari
07701182023

LAB-9
EXPERIMENT-1
Aim:
Write a program in C to show the basic declaration of pointer

Source code:
#include <stdio.h>
int main() {
int a;
printf("Enter a number: ");
scanf("%d",&a);
int *ptr=&a;
printf("value of the number:%d\n",a);
printf("address of the number:%d\n",&a);
printf("value of the number pointer is pointing to:%d\n",*ptr);
printf("address stored in pointer:%d\n",ptr);
return 0;
}

Output:

101
Mehnaaz Ansari
07701182023

EXPERIMENT-2
Aim:
Write a C program to add two numbers using pointers.

Source code:
#include <stdio.h>
int main() {
int num1,num2,sum;
int *ptr1,*ptr2;
printf("Enter 1st number: ");
scanf("%d",&num1);
printf("Enter 2nd number: ");
scanf("%d",&num2);
ptr1=&num1;
ptr2=&num2;
sum= *ptr1 + *ptr2;
printf("sum is: %d",sum);
return 0;
}

Output:

102
Mehnaaz Ansari
07701182023

EXPERIMENT-3
Aim:
Write a C program to swap two numbers using pointers.

Source code:
#include <stdio.h>
int swap(int *a,int *b){
int k= *a;
*a=*b;
*b=k;
}
int main() {
int num1,num2;
printf("Enter 1st number: ");
scanf("%d",&num1);
printf("Enter 2nd number: ");
scanf("%d",&num2);
printf("Before swapping: num1=%d , num2=%d\n",num1,num2);
swap(&num1,&num2);
printf("After swapping: num1=%d , num2=%d",num1,num2);
return 0;
}

Output:

103
Mehnaaz Ansari
07701182023

EXPERIMENT-4
Aim:
Write a C program to input and print array elements using pointer.

Source code:
#include <stdio.h>
int main() {
int n;
printf("Enter the size of an array: ");
scanf("%d",&n);
int arr[n];
printf("Enter %d elements:\n",n);
for (int *ptr=arr; ptr<arr+n; ++ptr){
scanf("%d",ptr);
}
printf("Array elements: ");
for (int *ptr=arr; ptr<arr+n; ++ptr){
printf("%d",*ptr);
}
return 0;
}

Output:

104
Mehnaaz Ansari
07701182023

EXPERIMENT-5
Aim:
Write a C program to copy one array to another using pointers

Source code:
#include <stdio.h>
int copyarr(int *source,int *destination,int n){
for (int i=0; i<n; ++i){
*(destination+i)=*(source+i);
}
}
int main() {
int n;
printf("Enter the size of an array: ");
scanf("%d",&n);
int sourcearr[n],destinationarr[n];
printf("Enter %d elements for the source array:\n",n);
for (int *ptr=sourcearr; ptr< sourcearr + n; ++ptr){
scanf("%d", ptr);
}
copyarr(sourcearr,destinationarr,n);
printf("Source Array elements: ");
for (int *ptr=sourcearr; ptr< sourcearr + n; ++ptr){
printf("%d",*ptr);
}
printf("\nCopied Array elements: ");
for (int *ptr=destinationarr; ptr< destinationarr + n; ++ptr){
printf("%d",*ptr);
}
return 0;
}

Output:

105
Mehnaaz Ansari
07701182023

EXPERIMENT-6
Aim:
Write a C program to swap two arrays using pointers

Source code:
#include <stdio.h>
void swapArrays(int *arr1, int *arr2, int size) {
for (int i = 0; i < size; i++) {
int temp = *(arr1 + i);
*(arr1 + i) = *(arr2 + i);
*(arr2 + i) = temp;
}
}
void displayArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", *(arr + i));
}
printf("\n");
}
int main() {
int size;
printf("Enter the size of the arrays: ");
scanf("%d", &size);
int arr1[size];
printf("Enter %d elements for the first array:\n", size);
for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr1[i]);
}
int arr2[size];
printf("Enter %d elements for the second array:\n", size);

106
Mehnaaz Ansari
07701182023

for (int i = 0; i < size; i++) {


printf("Element %d: ", i + 1);
scanf("%d", &arr2[i]);
}
printf("\nOriginal arrays:\n");
printf("Array 1: ");
displayArray(arr1, size);
printf("Array 2: ");
displayArray(arr2, size);
swapArrays(arr1, arr2, size);
printf("\nArrays after swapping:\n");
printf("Array 1: ");
displayArray(arr1, size);
printf("Array 2: ");
displayArray(arr2, size);
return 0;
}

Output:

107
Mehnaaz Ansari
07701182023

EXPERIMENT-7
Aim:
Write a C program to reverse an array using pointers

Source code:
#include <stdio.h>
void reverseArray(int *arr, int size) {
int *start = arr;
int *end = arr + size - 1;
while (start < end) {
int temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
void displayArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", *(arr + i));
}
printf("\n");
}
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);

108
Mehnaaz Ansari
07701182023

scanf("%d", &arr[i]);
}
printf("\nOriginal array:\n");
displayArray(arr, size);
reverseArray(arr, size);
printf("\nArray after reversing:\n");
displayArray(arr, size);
return 0;
}

Output:

109
Mehnaaz Ansari
07701182023

EXPERIMENT-8
Aim:
Write a C program to search an element in array using pointers

Source code:
#include <stdio.h>
int searchElement(int *arr, int size, int target) {
for (int i = 0; i < size; i++) {
if (*arr == target) {
return i;
}
arr++;
}
return -1;
}
int main() {
int size, target;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &target);
int result = searchElement(arr, size, target);
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;}
Output:

110
Mehnaaz Ansari
07701182023

EXPERIMENT-9
Aim:
Write a C program to access two-dimensional array using pointers

Source code:
#include <stdio.h>
void displayArray(int *arr, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", *((arr + i * cols) + j));
}
printf("\n");
}
}
int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int arr[rows][cols];
printf("Enter elements for the 2D array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element (%d, %d): ", i + 1, j + 1);
scanf("%d", &arr[i][j]);
}
}
printf("\n2D array elements using pointers:\n");
displayArray(&arr[0][0], rows, cols);
return 0;
}
Output:

111
Mehnaaz Ansari
07701182023

EXPERIMENT-10
Aim:
Write a C program to add two matrix using pointers.

Source code:
#include <stdio.h>
void addMatrices(int *mat1, int *mat2, int *result, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
*((result + i * cols) + j) = *((mat1 + i * cols) + j) + *((mat2 + i * cols) + j);}
}
}
void displayMatrix(int *mat, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", *((mat + i * cols) + j));}
printf("\n");}
}
int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int mat1[rows][cols];
printf("Enter elements for the first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element (%d, %d): ", i + 1, j + 1);
scanf("%d", &mat1[i][j]);}
}

112
Mehnaaz Ansari
07701182023

int mat2[rows][cols];
printf("Enter elements for the second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element (%d, %d): ", i + 1, j + 1);
scanf("%d", &mat2[i][j]);}
}
int result[rows][cols];
addMatrices(&mat1[0][0], &mat2[0][0], &result[0][0], rows, cols);
printf("\nFirst matrix:\n");
displayMatrix(&mat1[0][0], rows, cols);
printf("\nSecond matrix:\n");
displayMatrix(&mat2[0][0], rows, cols);
printf("\nSum of matrices:\n");
displayMatrix(&result[0][0], rows, cols);
return 0;
}

Output:

113
Mehnaaz Ansari
07701182023

LAB-10
EXPERIMENT-1
Aim:
Write a program in C to input a string and print it

Source code:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("Entered string: %s\n", str);
return 0;
}

Output:

114
Mehnaaz Ansari
07701182023

EXPERIMENT-2
Aim:
Write a program in C to find the length of a string without using library function

Source code:
#include <stdio.h>
int stringLength(char *str) {
int length = 0;
while (*str != '\0') {
length++;
str++;
}
return length;
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
int length = stringLength(str);
printf("Length of the entered string: %d\n", length);
return 0;
}

Output:

115
Mehnaaz Ansari
07701182023

EXPERIMENT-3
Aim:
Write a program in C to separate the individual characters from a string

Source code:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("Individual characters from the entered string:\n");
for (int i = 0; str[i] != '\0'; i++) {
printf("%c\n", str[i]);
}
return 0;
}

Output:

116
Mehnaaz Ansari
07701182023

EXPERIMENT-4
Aim:
Write a program in C to print individual characters of string in reverse order

Source code:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("Individual characters in reverse order:\n");
for (int i = 0; str[i] != '\0'; i++) {
printf("%c\n", str[strlen(str) - 1 - i]);
}
return 0;
}

Output:

117
Mehnaaz Ansari
07701182023

EXPERIMENT-5
Aim:
Write a program in C to count the total number of words in a string

Source code:
#include <stdio.h>
int countWords(char *str) {
int wordCount = 0;
int inWord = 0;
while (*str != '\0') {
if (*str == ' ' || *str == '\t' || *str == '\n') {
inWord = 0;}
else if (inWord == 0) {
wordCount++;
inWord = 1;}
str++;
}
return wordCount;}
int main() {
char str[1000];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
int words = countWords(str);
printf("Total number of words in the string: %d\n", words);
return 0;}

Output:

118
Mehnaaz Ansari
07701182023

EXPERIMENT-6
Aim:
Write a program in C to compare two strings without using string library functions

Source code:
#include <stdio.h>
int compareStrings(char *str1, char *str2) {
while (*str1 != '\0' && *str2 != '\0') {
if (*str1 != *str2) {
return 0;
}
str1++;
str2++;
}
if (*str1 == '\0' && *str2 == '\0') {
return 1;
} else {
return 0;
}
}
int main() {
char str1[100];
char str2[100];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
if (compareStrings(str1, str2)) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
Output:

119
Mehnaaz Ansari
07701182023

EXPERIMENT-7
Aim:
Write a program in C to count total number of alphabets, digits and special characters in a string

Source code:
#include <stdio.h>
void countCharacters(char *str, int *alphabets, int *digits, int *specialChars) {
while (*str != '\0') {
if ((*str >= 'a' && *str <= 'z') || (*str >= 'A' && *str <= 'Z')) {
(*alphabets)++;
} else if (*str >= '0' && *str <= '9') {
(*digits)++;
} else {
(*specialChars)++; }
str++;}}
int main() {
char str[1000];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
int alphabets = 0, digits = 0, specialChars = 0;
countCharacters(str, &alphabets, &digits, &specialChars);
printf("Total number of alphabets: %d\n", alphabets);
printf("Total number of digits: %d\n", digits);
printf("Total number of special characters: %d\n", specialChars);
return 0;}

Output:

120
Mehnaaz Ansari
07701182023

EXPERIMENT-8
Aim:
Write a program in C to count total number of vowel or consonant in a string

Source code:
#include <stdio.h>
int isVowel(char ch) {
ch = tolower(ch);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');}
void countVowelsConsonants(char *str, int *vowels, int *consonants) {
while (*str != '\0') {
if ((*str >= 'a' && *str <= 'z') || (*str >= 'A' && *str <= 'Z')) {
if (isVowel(*str)) {
(*vowels)++;
} else {
(*consonants)++; } }
str++; }}
int main() {
char str[1000];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
int vowels = 0, consonants = 0;
countVowelsConsonants(str, &vowels, &consonants);
printf("Total number of vowels: %d\n", vowels);
printf("Total number of consonants: %d\n", consonants);
return 0;}

Output:

121
Mehnaaz Ansari
07701182023

EXPERIMENT-9
Aim:
Write a program in C to find maximum occurring character in a string

Source code:
#include <stdio.h>
char maxOccurringChar(char *str) {
int charCount[256] = {0};
while (*str != '\0') {
charCount[*str]++;
str++; }
char maxChar = '\0';
int maxCount = 0;
for (int i = 0; i < 256; i++) {
if (charCount[i] > maxCount) {
maxCount = charCount[i];
maxChar = (char)i;} }
return maxChar; }
int main() {
char str[1000];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
char maxChar = maxOccurringChar(str);
printf("Maximum occurring character: %c\n", maxChar);
return 0; }

Output:

122
Mehnaaz Ansari
07701182023

EXPERIMENT-10
Aim:
Write a C program to check whether a given substring is present in the given string

Source code:
#include <stdio.h>
int isSubstring(char *str, char *sub) {
while (*str != '\0') {
char *tempStr = str;
char *tempSub = sub;
while (*tempStr == *tempSub && *tempSub != '\0') {
tempStr++;
tempSub++;
}
if (*tempSub == '\0') {
return 1;
}
str++;
}
return 0;
}
int main() {
char str[1000];
char sub[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a substring to check: ");
fgets(sub, sizeof(sub), stdin);
str[strlen(str) - 1] = '\0';
sub[strlen(sub) - 1] = '\0';
if (isSubstring(str, sub)) {
printf("The substring is present in the string.\n");
} else {
printf("The substring is not present in the string.\n");
}
return 0;
}

Output:

123

You might also like