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

C_record_work 1

Uploaded by

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

C_record_work 1

Uploaded by

esther.jeba07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

REG NO: 212224060310

INDEX.
PAGE.
S.NO DATE LIST OF EXPERINMENTS. MARKS SIGN
NO.
1(A) Datatypes & Operators
1(B) Conditional Statements
1(C) Operators & Expressions
1(D) Conditional Statements(L2)
2(A) Looping Statements
2(B) Nested Looping Statements
2(C) Functions
2(D) Looping Statements
3(A) Functions
3(B) Looping
3(C) Array
3(D) Array
4(A) Datatypes & Operators-Assignment
4(B) Unconditional Statements
4(C) Strings-Assignment
4(D) Strings
5(A) Pointers
5(B) Pointers
5(C) Array & Operators
5(D)
Strings

1
REG NO: 212224060310

6(A) Pointers L2
6(B) Dynamic Memory Allocation-L3
6(C) User Defined Datatypes

6(D) User Defined Datatypes

2
REG NO: 212224060310

EX.NO : 1(A)
Datatypes & Operators
DATE:

PROGRAM STATEMENT:

Write a program to print welcome to India.

ALGORITHM:

1. Start
2. Include Header File
3. Define the Main Function
4. Print the Message
5. End Program
6. Stop

PROGRAM :

#include <stdio.h>
int main()
{
printf("welcome to india");
}

3
REG NO: 212224060310

OUTPUT :

RESULT :

Thus, the given program executed successfully and produced the output as expected..

4
REG NO: 212224060310

EX.NO : 1(B)
Conditional Statements
DATE :

PROGRAM STATEMENT :

Write a C program to check whether the input value is 0 using simple if Statement

ALGORITHM :

1. Start
2. Include Header File
3. Define the Main Function
4. Declare a Variable
5. Input
6. Check Condition (n==0)
7. End Program

PROGRAM :

#include <stdio.h>
int main()
{
int value;
scanf("%d", &value);
if (value == 0) {
printf("FALSE \n");
} else {
printf("\n");
}

return 0;
}

5
REG NO: 212224060310

OUTPUT :

RESULT :

Thus, the given program executed successfully and produced the output as expected.

6
REG NO: 212224060310

EX.NO : 1(C)
Operators & Expressions
DATE :

PROGRAM STATEMENT :

Write a C program to find principle amount based on compound interest, time & rate of interest.

ALGORITHM :

1. Start
2. Include Header File
3. Define the Main Function
4. Declare Variables
5. Input the Data
6. Calculate the principle
7. output
8. End Program

PROGRAM :

#include <stdio.h>
#include <math.h>

int main() {
double A, r, t, P;
scanf("%lf %lf %lf", &A, &t, &r);
P = A / pow(1 + (r / 100), t);
printf("Principle Amount is = %.2f\n", P);

return 0;
}

7
REG NO: 212224060310

OUTPUT :

RESULT :

Thus, the given program executed successfully and produced the output as expected.

8
REG NO: 212224060310

EX.NO : 1(D)
Conditional Statements(L2)
DATE :

PROGRAM STATEMENT :

Write a C program to read a, b values and check whether a less than b.

ALGORITHM :

1. Start
2. Include Header File
3. Define the Main Function
4. Declare Variables
5. Read Inputs
6. Compare a and b
7. Print Output or Skip
8. End Program
9.
PROGRAM :

#include <stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
if (a<b)
{
printf("a is less than b");
}
else if (a>b)
{
printf(" ");
}
return 0;
}
9
REG NO: 212224060310

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected.
10
REG NO: 212224060310

EX.NO : 2(A)

DATE : Looping Statements

PROGRAM STATEMENT :

Write a C Program to print the string "HELLO WORLD" n number of times.

ALGORITHM :

1. Start
2. Include Header File
3. Define the Main Function
4. Declare Variable
5. Read Input
6. For Loop - Print "HELLO WORLD"
7. End Program

PROGRAM :

#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("HELLO WORLD\n");
}
}

11
REG NO: 212224060310

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected.

12
REG NO: 212224060310

EX.NO : 2(B)

DATE : Nested Looping Statements

PROGRAM STATEMENT :

Write a C program to print the given triangular alphabetic pattern using loop.

ALGORITHM :

1. Start
2. Include Header File
3. Define the Main Function
4. Declare Variable
5. Read Input
6. Outer For Loop (i)
7. Inner For Loop (j)
8. Print 'a' Characters
9. Print Newline
10. End Program

PROGRAM :

#include<stdio.h>
int main ()
{
int n;
scanf("%d",&n);
for (int i=n;i>0;i--)
{
for(int j=0;j<i;j++)
{
printf("a");
}
printf("\n");
}
}

13
REG NO: 212224060310

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected.

14
REG NO: 212224060310

EX.NO : 2(C)
FUNCTIONS
DATE :

PROGRAM STATEMENT :

Write a C function/sub program to display the values according to given format using static storage
class without return type & with arguments.

ALGORITHM :

1. Start
2. Include Header File
3. Declare Static Variable
4. Define the Main Function
5. Read Input
6. Call Display Function
7. Define Display Function
8. Update Static Variable
9. Print Updated Value
10. End Program

PROGRAM :

#include <stdio.h>
void display(int a);
static int c = 0;
int main()
{
int a;
scanf("%d",&a);
display(a);
display(a);
display(a);
display(a);
display(a);

}
void display(int a)
{
15
REG NO: 212224060310

c += a;
printf("%d ",c);

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected.

16
REG NO: 212224060310

EX.NO : 2(D)

DATE : Looping Statements

PROGRAM STATEMENT :

Write a c program to find the sum of even digits using while loop

ALGORITHM :

1. Start
2. Include Header File
3. Define the Main Function
4. Declare Variables
5. Read Input
6. Initialize Sum
7. For Loop (Iterate through numbers)
8. Check Even Number
9. Add Even Numbers to Sum
10. Print Result
11. End Program

PROGRAM :

#include<stdio.h>
int main()
{
int num,sum=0;
scanf("%d",&num);
for (int i=1;i<=num;i++)
{
if(i%2==0)
{
sum+=i;
}
}
printf("%d",sum);
}

17
REG NO: 212224060310

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected.
18
REG NO: 212224060310

EX.NO : 3(A)

DATE : Function

PROGRAM STATEMENT :

Write a program to calculate age using function without return type without arguments.

ALGORITHM :

1. Start
2. Include Header Files
3. Define age Function
4. Use Month Array
5. Adjust Day and Month Differences
6. Compute Age Differences
7. Print Age in Years, Months, and Days
8. End Program

PROGRAM :

#include <stdio.h>
#include <stdlib.h>
void age(int pd, int pm, int py, int bd, int bm, int by)
{
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
{
if (bd>pd)
{
pd=pd+month[bm-1];
pm=pm-1;
}
if(bm>pm)
{
py=py-1;
pm=pm+12;
}
int fd=pd-bd,fm=pm-bm,fy=py-by;
19
REG NO: 212224060310

printf("Present Age Years: %d Months: %d Days: %d",fy,fm,fd);


}
}
int main()
{
int pd,pm,py,bd,bm,by;
scanf("%d%d%d%d%d%d",&pd,&pm,&py,&bd,&bm,&by);
age(pd,pm,py,bd,bm,by);
}

OUTPUT :

20
REG NO: 212224060310

RESULT :
.
Thus, the given program executed successfully and produced the output as expected.

21
REG NO: 212224060310

EX.NO : 3(B)

DATE : Looping

PROGRAM STATEMENT :

Create a C program to check whether 71 is prime or not

ALGORITHM :

1. Start
2. Initialize Variables
3. Check Divisibility
4. Print Result
5. End

PROGRAM :

#include <stdio.h>

int main() {
int num = 71, i, isPrime = 1;

if (num <= 1) {
isPrime = 0;
} else {
for (i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}

if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
22
REG NO: 212224060310

return 0;
}

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected

23
REG NO: 212224060310

EX.NO : 3(C)

DATE : ARRAY

PROGRAM STATEMENT :

Create a C program to read n elements as input and print the elements of an array present on even
position

ALGORITHM :

1. Start
2. Include Header File
3. Define the Main Function
4. Declare Variables
5. Read Input
6. Input Array Elements
7. Print Elements at Odd Indices
8. End Program

PROGRAM :

#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for (int i =0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(int i =0;i<n;i++)
{
if(i%2==1)
{
printf("%d ",arr[i]);
}

24
REG NO: 212224060310

}
printf("\n");
}

OUTPUT :

RESULT :

Thus, the given program executed successfully and produced the output as expected

25
REG NO: 212224060310

EX.NO : 3(D)

DATE : ARRAY

PROGRAM STATEMENT :

Write a C program to delete last element in an array

ALGORITHM :

1. Start
2. Input Array Size and Elements
3. Check if Array is Empty
4. Delete the Last Element
5. Display Updated Array
6. End

PROGRAM :

#include <stdio.h>
int main() {
int n, i;
scanf("%d", &n);

if (n <= 0) {
printf("Invalid input: Number of elements should be positive.\n");
return 1;
}

int arr[n];
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

if (n == 0) {
printf("Array is empty.\n");
} else {
for (i = 0; i < n - 1; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

return 0;
}

26
REG NO: 212224060310

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected

27
REG NO: 212224060310

EX.NO : 4(A)

DATE : Datatypes & Operators-Assignment

PROGRAM STATEMENT :

Write a C program to read the amount(integer) and break the amount into smallest possible number of
bank notes.

ALGORITHM :

1. Start
2. Include Header File
3. Define the Main Function
4. Declare Variables
5. Read Input
6. Calculate Notes for 100
7. Calculate Notes for 50
8. Calculate Notes for 20
9. Calculate Notes for 10
10. Calculate Notes for 5
11. Calculate Notes for 2
12. Calculate Notes for 1
13. Print Results
14. End Program

PROGRAM :

#include <stdio.h>
int main()
{
int amt, total;
scanf("%d",&amt);
total = (amt)/100;
printf("There are: ");
printf("\n%d Note(s) of 100.00\n", total);
amt = amt-(total*100);
total=amt/50;
printf("%d Note(s) of 50.00\n", total);
amt = amt-(total*50);
total = amt/20;
printf("%d Note(s) of 20.00\n", total);
28
REG NO: 212224060310

amt = amt-(total*20);
total = amt/10;
printf("%d Note(s) of 10.00\n", total);
amt = amt-(total*10);
total = amt/5;
printf("%d Note(s) of 5.00\n", total);
amt=amt-(total*5);
total = amt/2;
printf("%d Note(s) of 2.00\n", total);
amt = amt-(total*2);
total = amt/1;
printf("%d Note(s) of 1.00\n", total);
return 0;
}

29
REG NO: 212224060310

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected
30
REG NO: 212224060310

EX.NO : 4(B)

DATE : Unconditional Statements

PROGRAM STATEMENT :

Write a C program to check positive negative or zero using simple if statement

ALGORITHM :

1. Start
2. Include Header File
3. Define the Main Function
4. Declare Variables
5. Read Input
6. Check Positive Number
7. Check Negative Number
8. Check Zero
9. Print Result
10. End Program
11. Stop

PROGRAM :

#include <stdio.h>
int main()
{
int num;
scanf("%d",&num);
if(num > 0)
{
printf("Number is POSITIVE");
}
if(num < 0 )
{
printf("Number is NEGATIVE");
}
if(num == 0)
{
printf("Number is ZERO");
}
31
REG NO: 212224060310

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected

32
REG NO: 212224060310

EX.NO : 4(C)

DATE : Strings-Assignment

PROGRAM STATEMENT :

To convert the string 'JAVA' into lowercase

ALGORITHM :

1. Start
2. Include Header Files
3. Define the Main Function
4. Declare Variables
5. Read Input
6. Convert Characters to Lowercase
7. Print Lowercase String
8. End Program

PROGRAM :

#include<stdio.h>
#include<ctype.h>
int main()
{
char str[100];
fgets(str,100,stdin);
for(int i=0;str[i]!='\0';i++)
{
str[i]=tolower(str[i]);
}
printf("Lower case String is:%s",str);
}

33
REG NO: 212224060310

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected

34
REG NO: 212224060310

EX.NO : 4(D)

DATE : Strings

PROGRAM STATEMENT :

Complete the C program to copy a string into another string without using strcpy() using for loop.

ALGORITHM :

1. Start
2. Include Header Files
3. Define the Main Function
4. Declare Variables
5. Read Input
6. Print the String
7. End Program
PROGRAM :

#include<stdio.h>
#include<string.h>
int main()
{
char i[100];
scanf("%[^\n]",i);
printf("String s2: %s",i);
}

35
REG NO: 212224060310

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected

36
REG NO: 212224060310

EX.NO : 5(A)

DATE : Pointers

PROGRAM STATEMENT :

Write a C program to print string 'helloworld' using pointer

ALGORITHM :

1. Start
2. Include Header Files
3. Define the Main Function
4. Declare Variables
5. Read Input
6. Check the condition
7. End Program
PROGRAM :

#include <stdio.h>
int main()
{
char str[100];
char *ptr;

scanf("%s",str);

//assign address of str to ptr


ptr=str;

printf("The entered string is :: ");

while(*ptr!='\0')
printf("%c",*ptr++);

return 0;

37
REG NO: 212224060310

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected.

38
REG NO: 212224060310

EX.NO : 5(B)

DATE : Functions & Storage Classes

PROGRAM STATEMENT :

Write a C program to swap any two values function pointers

ALGORITHM :

1. Start
2. Include Header Files
3. Define the Main Function
4. Declare Variables
5. Initialize the Pointer
6. Print the string
7. End Program

PROGRAM :

#include<stdio.h>
int main()
{
int n,m,temp=0;
scanf("%d%d",&n,&m);
printf("Before Swapping a=%d b=%d",n,m);
temp=n;
n=m;
m=temp;
printf("\nAfter Swapping a=%d b=%d",n,m);
}

39
REG NO: 212224060310

OUTPUT :

RESULT :
.
Thus, the given program executed successfully and produced the output as expected.

40
REG NO: 212224060310

EX.NO : 5(C)
Arrays & Operations
DATE :

PROGRAM STATEMENT :

Write a C Program to print the sum of elements in the Zigzag sequence in a given matrix.

ALGORITHM :

1. Start
2. Include Header Files
3. Define the Main Function
4. Declare Variables
5. Read Input
6. Matrix Input
7. Sum Row-wise Zig-Zag Pattern
8. Sum Column-wise Zig-Zag Pattern
9. Sum Diagonal Zig-Zag Pattern
10. Adjust for Overlap
11. Final Sum Calculation
12. Print Result
13. End Program

PROGRAM :

#include<stdio.h>
#include<stdlib.h>
int main()
{
int m, n, row1 = 0, col_n = 0, diag = 0;
scanf("%d %d",&m,&n);
int i, j;
int mat[m][n];
for(i=0;i<m;i++)
{
for(j=0; j<n;j++)
scanf("%d",&mat[i][j]);
}
for(i=0; i<=0; i++)
{
for(j=0; j<n-1;j++)
{
row1=row1+mat[i][j];
41
REG NO: 212224060310

}
}
for(j=n-1; j==n-1;j--)
{
for(i=0;i<m;i++)
{
col_n=col_n+mat[j][i];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if ((i+j)==(m-1))
{
diag+=mat[i][j];
}
if(j==0 && i==m-1)
{
col_n=col_n-mat[i][j];
}
}
}
printf("Sum of Zig-Zag pattern is %d ", diag + row1 + col_n);
}

42
REG NO: 212224060310

OUTPUT :

RESULT :

Thus, the given program executed successfully and produced the output as expected.

43
REG NO: 212224060310

EX.NO : 5(D)
Strings
DATE :

PROGRAM STATEMENT :

Write a program in C to count total number of alphabets, digits and special characters in a string
using built-in functions.

ALGORITHM :

1. Start
2. Include Header Files
3. Define the Main Function
4. Declare Variables
5. Initialize Counters
6. Read Input
7. Count Characters
8. Print Results
9. End Program

PROGRAM :

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char str[100];
int alp, digit, splch, i;
alp = digit = splch = i = 0;
scanf("%[^\n]%*c",str);
while(str[i]!='\0')
{
if(isalpha(str[i]))
{
alp++;
}
else if(isdigit(str[i]))
{
digit++;
}
else
{
splch++;
44
REG NO: 212224060310

i++;
}

printf("Number of Alphabets in the string is : %d\n", alp);


printf("Number of Digits in the string is : %d\n", digit);
printf("Number of Special characters in the string is : %d\n\n", splch);
}

45
REG NO: 212224060310

OUTPUT :

RESULT :

Thus, the given program executed successfully and produced the output as expected.

46
REG NO: 212224060310

EX.NO : 6(A)
Pointers L2
DATE :

PROGRAM STATEMENT :

Write a C program to calculate the area of a triangle using pointer

ALGORITHM :

1. Start
2. Include Header Files
3. Define the Main Function
4. Declare Variables
5. Read Input
6. Calculate Area
7. Print the Result
8. End Program

PROGRAM :

#include<stdio.h>
#include<math.h>
int main()
{
float n,m,o;
scanf("%f%f",&n,&m);
o=(n*m)/2;
printf("area of the triangle with base %.6f and height%.6f=%.6f",n,m,o);
}

47
REG NO: 212224060310

OUTPUT :

RESULT :

Thus, the given program executed successfully and produced the output as expected.

48
REG NO: 212224060310

EX.NO : 6(B)
Dynamic Memory Allocation -L3
DATE :

PROGRAM STATEMENT :

Write a C program to get and print array of characters using malloc() and free()

ALGORITHM :

1. Start
2. Include Header Files
3. Define the Main Function
4. Input the number of characters
5. Dynamically allocated memory
6. Input the characters
7. Print the array of characters
8. Free the allocated memory
9. End Program

PROGRAM :

#include <stdio.h>
#include <stdlib.h>

int main() {
int size;
scanf("%d", &size);
char *arr = (char *)malloc(size * sizeof(char));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}

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


scanf(" %c", &arr[i]); // Notice the space before %c to consume any newline characters
}
for (int i = 0; i < size; i++) {
printf("%c", arr[i]);
}

// Free the allocated memory


free(arr);

return 0;
}

49
REG NO: 212224060310

OUTPUT :

RESULT :

Thus, the given program executed successfully and produced the output as expected.

50
REG NO: 212224060310

EX.NO : 6(C)
User defined data types
DATE :

PROGRAM STATEMENT :

Write a program to store information of Employee (name , age ,qualification,no_of_years_exp,)


using function with structure.
struct employee
{
char name[20];
int age;
char Qualification[10];
int no_of_years_exp;
};

ALGORITHM :

1. Start
2. Define Structure for Employee
3. Input Employee Data
4. Display Employee Data
5. Main Function
6. End Program

PROGRAM :

#include <stdio.h>
#include<string.h>
struct employee
{
char name[20];
int age;
char quli[10];
int year;
};
void inputemployee(struct employee *emp)
{
fgets(emp->name,sizeof(emp->name),stdin);
if(emp->name[strlen(emp->name) - 1] == '\n')
{
emp->name[strlen(emp->name) - 1] = '\0';
}
scanf("%d",&emp->age);
getchar();
fgets(emp->quli,sizeof(emp->quli),stdin);
51
REG NO: 212224060310

if(emp->quli[strlen(emp->quli) - 1] == '\n')
{
emp->quli[strlen(emp->quli) - 1] = '\0';
}
scanf("%d",&emp->year);
}
void displayemployee(const struct employee *emp)
{
printf("Name is: %s\n",emp->name);
printf("Age is: %d\n",emp->age);
printf("Qualification is:%s\n",emp->quli);
printf("Experience is: %d years\n",emp->year);
}
int main()
{
struct employee emp;
inputemployee(&emp);
displayemployee(&emp);
}

52
REG NO: 212224060310

OUTPUT :

RESULT :

Thus, the given program executed successfully and produced the output as expected.
53
REG NO: 212224060310

EX.NO : 6(D)
User Defined Data types(L2)
DATE :

PROGRAM STATEMENT :

Write a ‘C’ program to store the data’s of different type & retrieve them using union.

ALGORITHM :

1. Start
2. Include Header File
3. Define a union
4. Define the Main Function
5. Store the data in the union
6. Retrieve and print data
7. End Program

PROGRAM :

#include<stdio.h>
#include<string.h>
union data
{
int i;
float f;
char str[20];
};
int main()
{
union data data;
scanf ("%d", &data.i);
printf ("i=%d\n", data.i);
scanf ("%f", &data.f);
printf("f=%.2f\n", data.f);
scanf ("%s", data.str);
printf("String=%s", data.str);
}

54
REG NO: 212224060310

OUTPUT :

RESULT :

Thus, the given program executed successfully and produced the output as expected.
55
REG NO: 212224060310

56

You might also like