New Lab Record - C
New Lab Record - C
NO :
DATE : NAME :
AIM
ALGORITHM
Step 1: Start
Sumnum1+num2
Step 6: Stop
RESULT
FLOWCHART
RESULT
PSEUDO CODE
Function main():
Input num1
Input num2
Return 0
End Function
RESULT
AIM
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int amount, rate, time, si;
printf("\nEnter Principal Amount : ");
scanf("%d", &amount);
printf("\nEnter Rate of Interest : ");
scanf("%d", &rate);
printf("\nEnter Period of Time : ");
scanf("%d", &time);
si = (amount * rate * time) / 100;
printf("\nSimple Interest : %d", si);
}
INPUT AND OUTPUT
RESULT
Thus, the Program has been executed and the output was verified.
EXNO. : 2B REG. NO :
DATE : NAME :
PROCEDURE
Step1: Start
Step2:get a and b values
Step3: Calculate c=a+b
Step4 :calculate d=a-b
Step 5:calculate e=a*b
Step6:calculate f=a/b
Step 7 : Calculate g=a%b
Step 8 : display all the outputs
Step 9: stop
PROGRAM
#include <stdio.h>
int main()
{
int a,b;
printf(“ Enter A and B values”);
scanf(“%d%d”, &a, &b);
printf(“Addition =%d”,a+b);
printf(“Subtraction =%d”,a-b);
printf(“Multiplication =%d”,a*b);
printf(“Division =%d”,a/b);
printf(“Remainder =%d”,a%b);
}
RESULT
GET ALL THE PRIMARY TYPE DATA AND DISPLAY THE SAME
AIM
To get all the Primary type data and display the same
PROCEDURE
Step1.start
Step2.Get all the primary datatypes
Step 3: display the same
Step4: stop
PROGRAM
#include <stdio.h>
void main()
{
int a;
float b;
double c;
char d;
Enter Character:a
Enter Integer value:7
Enter float value:3.1
Enter double value: 4.000000001
Character:a
Integer:7
Float:3.1
Double:4.000000001
RESULT
To convert float into integer data type for given float value
PROCDURE
Step1:start
Step2:get float value
Step3: convert float into integer
Step4: display the value
Step6: stop
PROGRAM
#include <stdio.h>
void main()
{
int a;
float b;
printf("Enter the float value: ");
scanf("%f", &b);
a= (int)b;
printf("%d\n", a);
}
INPUT AND OUTPUT
RESULT
To check whether the given number is positive or not using Conditional operator
PROCEDURE
Step1:start
Step2:get n value
Step3: n is greater than 0 , display n is positive otherwise display n is negative
Step 4:stop
PROGRAM
#include<stdio.h>
void main()
{
int n;
printf(“enter a value”);
scanf(“%d”,&n);
(n>0)?printf(“ positive”):printf(“negative”)
}
INPUT AND OUTPUT
Enter a value:3
positive
Enter a value:-7
Negative
RESULT
AIM
PROCEDURE
Step1: Start
Step2: get a and b value
Step3: apply bitwise operators and display the result
Step 4: Stop
PROGRAM
#include<stdio.h>
void main()
{
int a,b;
printf(“enter the a and b value”);
scanf(“%d%d”,&a,&b);
printf(“bitwise and : %d”,a&b);
printf(“bitwise or : %d”,a|b);
printf(“bitwise xor : %d”,a^b);
printf(“bitwise complement : %d”,~a);
printf(“bitwise shift left : %d”,a<<1);
printf(“bitwise shift right : %d”,a>>1);
}
INPUT AND OUTPUT
RESULT
AIM
PROCEDURE
Step1: Start
Step 2: get a radius
Step 3: calculate area= 3.14*r*r
Step 4: Display area
Step 5: Stop
PROGRAM
#include<stdio.h>
Void main()
{
Int r;
Float area;
Printf(“enter the radius”);
Scanf(“%d”,&r;);
Area=3.14*r*r;
Printf(“Area of circle :%f”,area);
}
INPUT AND OUTPUT
Enter radius : 4
Area of circle : 50.24
RESULT
AIM
PROCEDURE
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
{
int number;
clrscr();
printf(" Finding positive,Negative and Zero \n");
printf("_____________________________________ \n");
printf("Enter a number\n");
scanf ("%d", &number);
printf("The Given Number is %d \n",number);
if (number > 0)
printf ("%d is a positive number\n", number);
else if (number<0)
printf ("%d is a negative number\n", number);
else
printf ("%d is a Zero \n", number);
AIM
To find the given character is vowel and consonant using switch case
PROCEDURE
PROGRAM
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf(" Finding Vowels Or Consonant \n");
printf(" ___________________________ \n");
printf("\n Enter any character:");
scanf("%c",&ch);
clrscr();
switch(tolower(ch))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("\n The character %c is vowel!",ch);
break;
default :
printf("\n The character %c is consonant!",ch);
}
}
RESULT
AIM
PROCEDURE
1.Get a number N and initialize the counter is zero
2. start the loop I from 1 to N
2.1 check the given number is divisible by i
2.1.1 increase the counter value by one
2.1.2 the loop will be continued until reach N
3.If the value of the counter is 2, the given number is prime ,otherwise the given number is not
prime.
PROGRAM
#include<stdio.h>
void main()
{
int i,count=0,n;
clrscr();
printf(" Checking Prime Or Not \n");
printf(" _____________________ \n");
printf("\nEnter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
printf("\n Given Number %d is prime",n);
else
printf("\nGiven number %d is not a prime",n);
}
RESULT
AIM
PROCEDURE
1. Get N Value
2. Initialize f1=0 and f2=1
3. Display f1 and f2
4. Start the loop I from 3 to n
4.1 calculate f3=f1+f2
4.2 display f3
4.3 assign f1=f2 and f2=f3
4.4 continue the 4 th step until it reaches N.
PROGRAM
#include <stdio.h>
int main()
{
int i ,n, f1=0, f2=1;
clrscr();
printf("Enter number of terms: ");
scanf("%d",&n);
clrscr();
printf(" Fibonacci Series \n");
printf(" ________________ \n");
printf(" The Given Term is : %d\n",n);
printf("Fibonacci Series: \n%d \n%d\n", f1, f2);
for (i=3;i<=n;i++)
{
F3=f1+f2
F1=f2;
F2=f3;
printf("%d\n",f3);
}
}
RESULT
AIM
PROCEDURE
Input size of array and elements in array. Store it in some variable say size and arr.
To select each element from array, run an outer loop from 0 to size - 1. The loop
structure must look like for(i=0; i<size; i++).
Run another inner loop from i + 1 to size - 1 to place currently selected element at
its correct position. The loop structure should look like for(j = i + 1; j<size; j++).
Inside inner loop to compare currently selected element with subsequent element
and swap two array elements if not placed at its correct position.Which is if(arr[i]
> arr[j]) then swap arr[i] with arr[j].
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],j,n,i,temp;
printf(" Enter the size of the array \t");
scanf("%d",&n);
printf(" enter the elements for an array \t");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
clrscr();
printf("\n Sorting Element \n ");
printf(" _______________ \n");
printf(" Size of an Given Array %d \n",n);
printf(" Before Sorting Array are \n");
for(i=1;i<=n;i++)
{
printf("\t %d",ar[i]);
}
for(i=1;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;
}
}
}
RESULT
AIM
To search an element in an array using linear search method.
PROCEDURE
1. Set the value Set j to 1
2. Initially, we need to mention or accept the element to be searched from the user.
3. Then, we create a for loop and start searching for the element in a sequential
fashion.
4. As soon as the compiler encounters a match i.e. array[element] == key value,
return the element along with its position in the array.
5. If no values are found that match the input, it returns -1.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],flag=0,n,i,ele;
printf(" enter the size of the array \t");
scanf("%d",&n);
printf(" enter the elements for an array \t");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf(“enter searching element”);
scanf(“%d”,&ele);
for(i=1;i<=n;i++)
{
if (a[i]==ele)
{
flag=1;
break;
}
}
if (flag ==1)
printf(“element is found”);
else
printf(“element is not found”);
}
12
45
2
7
1
Enter searching element
2
Element is found
RESULT
Thus the program has been completed successfully
EXNO. : 6C REG. NO :
DATE : NAME :
AIM
PROCEDURE
1. Declare the variables j,n,a[20]
2. Get the Max and Min value
3. Get the input using scanf() statement
4. Using if (a[i] > max) maximum value assigned in max variable
5. Using else if (a[i] < min) minimum value assigned in min variable
6. Finally max variable and min variable printed
PROGRAM
#include <stdio.h>
#include<conio.h>
void main ()
{
int i, j, n, a[20], max, min;
clrscr ();
printf ("Enter n value \n");
scanf ("%d", &n);
printf ("Enter the elements for an array\n");
for (i = 1; i <= n; i++)
{
scanf ("%d", &a[i]);
}
max = a[1]; min = a[1];
for (i = 1; i <= n; i++)
{
if (a[i] > max)
max = a[i];
else
min = a[i];
}
printf ("Largest number is %d\n Smallest number is %d", max, min);
}
INPUT AND OUTPUT
Enter n value
5
Enter the elements for an array
5
1
6
8
2
Largest Number is : 8
Smallest Number is :1
RESULT
AIM
To calculate addition of two matrices and subtraction of two matrices.
PROCEDURE
1. Declare the variables i,j,a[10][10],b[10][10],c[10][10],d[10][10],m1,n1,m2,n2;
2. Get the Rows and Columns value
3. Get the input using scanf() statement
4. Using c[i][j] = a[i][j] + b[i][j] ; Addition values assigned in c[i][j] array.
5. Using d[i][j] = a[i][j] - b[i][j];;Subtraction values assigned in d[i][j] array.
6. Finally c[i][j] array and d[i][j] array printed
PROGRAM
#include<stdio.h>
void main()
{
int i,j,a[10][10],b[10][10],c[10][10],d[10][10],m1,n1,m2,n2;
printf("Enter the number of Rows of Mat1 : ");
scanf ("%d",&m1);
printf("Enter the number of Columns of Mat1 : ");
scanf ("%d",&n1);
for(i=1;i<=m1;i++)
for(j=1;j<=n1;j++)
{
printf("Enter the Element a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
printf("Enter the number of Rows of Mat2 : ");
scanf ("%d",&m2);
printf("Enter the number of Columns of Mat2 : ");
scanf ("%d",&n2);
for(i=1;i<=m2;i++)
{
for(j=1;j<=n2;j++)
{
printf(" Enter the Element b[%d][%d] :",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=m1;i++)
{
for(j=1;j<=n1;j++)
{
c[i][j] = a[i][j] + b[i][j] ;
d[i][j] = a[i][j] - b[i][j];
}
}
printf("\nThe Addition of two Matrices \n");
printf(" ____________________________ \n");
for(i=1;i<=m1;i++)
{
for(j=1;j<=n1;j++ )
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
printf("\nThe Subtraction of two Matrices \n");
printf(" ____________________________ \n");
for(i=1;i<=m1;i++)
{
for(j=1;j<=n1;j++ )
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
}
INPUT AND OUTPUT
Enter the number of Rows of Mat1 : 2
Enter the number of Columns of Mat1 : 2
Enter the Element a[0][0] : 4
Enter the Element a[0][1] : 4
Enter the Element a[1][0] : 4
Enter the Element a[1][1] : 4
Enter the number of Rows of Mat2 : 2
Enter the number of Columns of Mat2 : 2
Enter the Element b[0][0] :1
Enter the Element b[0][1] :2
Enter the Element b[1][0] :3
Enter the Element b[1][1] :4
RESULT
MATRIX MULTIPLICATION
AIM
PROCEDURE
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
int a[3][3],b[3][3],c[3][3],i,j;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
scanf("%d",&a[i][j]);
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
scanf("%d",&b[i][j]);
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
for(k=1;k<=3;k++)
c[i][j]=c[i][j] + a[i][k]*b[k][j];
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
printf("%2d",c[i][j]);
printf(“\n”);
}
INPUT AND OUTPUT
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
333
333
333
RESULT
AIM
PROCEDURE
PROGRAM
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char s[10],s1[10];
int i,len,flag=0;
clrscr();
printf("\nENTER A STRING: ");
scanf(“%s”,s);
strcpy(s1,s);
strrev(s);
if (strcmp(s,s1)==0)
printf(“the given string is palindrome”);
else
printf(“the given string is not palindrome”);
}
RESULT
The Program has been executed and the output was verified.
EXNO. : 8B REG. NO :
DATE : NAME :
STRING MANIPULATION
AIM
PROCEDURE
PROGRAM
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
int l1,l2;
printf("Enter the strings\n");
scanf("%s %s",s1,s2);
printf(" String Manipulation \n");
printf(" ___________________ \n");
printf(" First String :%s \n",s1);
printf(" Second String: %s \n\n",s2);
l1=strlen(s1);
l2=strlen(s2);
printf( "Length of First String %d \n",l1);
printf(" Length of Second String %d \n\n",l2);
printf(" Before Concatenation First String is %s\n",s1);
strcat(s1,s2);
printf(" After Concatenation Second String is %s \n",s1);
printf(" Before Copying Second String is %s \n",s2);
strcpy(s2,s1);
printf(" After Copying Second String is %s \n\n",s2);
if(strcmp(s1,s2)==0)
printf("\nBoth Strings are equal\n");
else
printf("\nBoth Strings are not equal");
printf(" Reverse of the string is %s \n\n",s2);
}
RESULT
The Program has been executed and the output was verified.
EXNO. : 8C REG. NO :
DATE : NAME :
AIM
PROCEDURE
_____________________________________________________________
RESULT
The Program has been executed and the output was verified.
EXNO. : 9A REG. NO :
DATE : NAME :
AIM
Program to sum of two numbers using function with arguments and with return value.
PROCEDURE
PROGRAM
#include<stdio.h>
int add(int x,int y);
void main ()
{
int a, b,k;
printf(“Enter the value for A:”);
scanf(“%d”,&a);
printf(“Enter the value for B:”);
scanf(“%d”,&b);
k=add(a,b);
printf(“Result :%d”,k);
}
int add (int x,int y)
{
int z;
z = x+y;
return(z);
}
INPUT AND OUTPUT
Result : 11
RESULT
The Program has been executed and the output was verified.
EXNO. : 9B REG. NO :
DATE : NAME :
AIM
Program to swap two numbers using call by value and call by reference.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void swap1(int,int);
void swap2(int*,int*);
void main()
{
Int a,b;
clrscr();
printf("\n\nEnter the value of A and B=");
scanf("%d%d",&a,&b);
printf("\n\nBefore swapping A=%d,B=%d",a,b);
swap1(a,b);
printf("\n\nAfter call by value,values are A=%d,B=%d",a,b);
swap2(&a,&b);
printf("\n\nAfter call by reference,values are A=%d,B=%d",a,b);
getch();
}
void swap1(inta,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void swap2(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
INPUT AND OUTPUT
RESULT
The Program has been executed and the output was verified.
EXNO. : 9C REG. NO :
DATE : NAME :
AIM
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
int factorial( int n);
void main()
{
int n;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);
clrscr();
printf(" Factorial \n");
printf(" _______________ \n");
printf(" The Given Value is %d \n",n);
printf("Factorial of %d is %d", n, factorial(n));
}
int factorial( int n)
{
if(n==0)
return (1);
else
return (n*factorial(n-1);
}
Factorial
________
Factorial of 5 is 120
RESULT
The Program has been executed and the output was verified.
EXNO. : 10A REG. NO :
DATE : NAME :
PROCEDURE
PROGRAM
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
float *data;
printf("Enter the total number of elements: ");
scanf("%d", &num);
data = (float *)calloc(num, sizeof(float));
if (data == NULL) {
printf("Error!!! memory not allocated.");
exit(0);
}
for (int i = 0; i < num; ++i) {
printf("Enter Number %d: ", i + 1);
scanf("%f", data + i);
}
for (int i = 1; i < num; ++i) {
if (*data < *(data + i))
*data = *(data + i);
}
printf("Largest number = %.2f", *data);
return 0;
}
INPUT AND OUTPUT
RESULT
The Program has been executed and the output was verified.
EXNO. : 10B REG. NO :
DATE : NAME :
PROCEDURE
1.Get two values
2.assign the address of two values into pointer variables
3.calculate c=*a+*b
4.display c
PROGRAM
#include<stdio.h>
Void main()
{
Int c,d,*a,*b,c;
Printf(“enter two values”);
Scanf(“%d%d”,&c,&d);
A=&c;
B=&d;
C=*a+*b;
Printf(“result :%d”,c);
}
INPUT AND OUTPUT
Enter two values
4
5
Result:9
RESULT
AIM
PROGRAM
#include<stdio.h>
Void main()
{
RESULT
AIM
PROCEDURE
1.get no
2.open a file with w+ mode
3.write a no into file
4. move filepointer to first position of the file
5.read a no from file
6.display the no
7.remove the filename from memory
8.close the file
PROGRAM
#include <stdio.h>
void main()
{
FILE *fp;
int no;
char filename[10]=”data.txt”
scanf(“%d”,&no);
fp=fopen(filename,”w+”);
putw(no,fp);
fseek(fp,7,seek_set);
k=getw(fp);
printf(“%d”,k);
remove(filename);
fclose(fp);
}
10
10
RESULT
The Program has been executed and the output was verified.
EXNO. : 13 REG. NO :
DATE : NAME :
AIM
PROCEDURE
1. Define a macro
2. Define a function macro
3. Get radius value to calculate area of circle
4. Pass radius value to macro function and Call the macro function
5. Calculate the area
6. Display the result
Program:
#include <stdio.h>
#define PI 3.1415
#define circleArea(r) (PI*r*r)
void main()
{
float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);
area = circleArea(radius);
printf("Area = %.2f", area);
}
INPUT AND OUTPUT
RESULT
The Program has been executed and the output was verified.