Programming in c Laboratory (2)
Programming in c Laboratory (2)
Name :
Register no :
Branch :
ANNA UNIVERSITY
UNIVERSITY COLLEGE OF ENGINEERING - DINDIGUL
DINDIGUL - 624622
BONAFIDE CERTIFICATE
Mr./Ms. _______________________________________________________________________in
University Registration
No:
5 Strings:operation.
7 Recursion.
DATE :
AIM
1. Start
2. Declare variables and initializations
3. Read the Input variable.
4. Using I/O statements and expressions for computational processing.
5. Display the output of the calculations.
6. Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
{
int sumOdd = 0;
int sumEven = 0;
int upperbound;
int absDiff;
int number = 1;
printf("Enter the upper bound: ");
scanf("%d", &upperbound);
while (number <= upperbound)
{
if (number % 2 == 0)
{
sumEven += number;
}
else
{
sumOdd += number;
}
++number;
}
RESULT:
Thus a C Program using i/o statements and expressions was executed and the output was obtained.
EX.NO.: 1(b) ARITHMETIC OPERATION
DATE:
AIM
To write a C Program to Design a calculator to perform the operations, namely, addition, subtraction,
multiplication, division and square of a number.
ALGORITHM
1. Start
2. Declare variables
3. Read the Inputs .
4. Calculate Arithmetic operations(+,-,*,/,pow) for the input of two numbers.
5. Display the output of the calculations .
6. Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
int main()
{
int firstNumber, secondNumber; int
sum, difference, product; long square;
float quotient;
printf("Enter First Number: ");
scanf("%d", &firstNumber); printf("Enter
Second Number: "); scanf("%d",
&secondNumber);
sum = firstNumber + secondNumber
difference = firstNumber - secondNumber;
product = firstNumber * secondNumber;
quotient = (float)firstNumber / secondNumber;
square = firstNumber *firstNumber;
printf("\nSum = %d", sum);
printf("\nDifference = %d", difference);
printf("\nMultiplication = %d", product);
printf("\nDivision = %.3f", quotient);
printf("\n Square= %ld", square);
getch();
}
RESULT
Thus a C Program for Arithmetic operations was executed and the output was obtained
Ex.No. :2(a) Decision Making Construct : If else
Date:
AIM
To write a c program to find the roots of the quadratic equation using if…else statement
ALGORITHM
D=b*b-4*a*c
Step 4:if D is greater than or equal to zero then find the two roots as
root1=(-b+sqrt(d))/(2*a)
root2=(+b+sqrt(d))/(2*a)
Step 5:if the D is not greater than or equal to zero then print the statement the roots are imaginary.
Step 6:stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
Void main()
int a,b,c,d;
float root1,root2;
scanf(“%d %d %d”,&a,&b,&c);
d=b*b-4*a*c;
if(d>=0)
{
root1=(-b+sqrt(d))/(2*a);
root2=(+b+sqrt(d))/(2*a);
Else
RESULT
Thus the c program for if else statement was executed and the Output is verified.
EX.NO.:2(b) GOTO STATEMENT.
DATE:
AIM
ALGORITHM
Goto step 5
Step 4:If a is not equivalent to b print the statement A and B are not equal
Step 6: Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
Void main()
Int a,b;
Scanf(“%d %d”,&a&b);
If(a==b)
Goto equal;
Else
Equal
RESULT
Thus the c program for goto statement was executed and the output was verified
EX.NO.:2(c) SWITCH STATEMENT
DATE:
AIM
Switch statement.
ALGORITHM
Step 1: start.
C=a+b
Print c
Goto step 7
C=a-b
Print c
Goto step 7
C=a*b
Print c
Goto step 7
C=a/b
Print c
Goto step 7
Exit
code”.
Step 7: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
Void main()
Int a,b,c,n;
Clrscr();
Printf(“--MENU--\n”);
Printf(“1 addition\n”);
Printf(“2 subraction\n”);
Printf(“3 multiplication\n”);
Printf(“4 division\n”);
Printf(“0 EXIT\n”);
Scanf(“%d”,&n);
If(n<=4&n.0)
Scanf(“%d %d”,&a,&b);
Switch(n)
{
Case 1:
C=a+b;
Printf(“addition: %d\n”);
Break;
Case 2:
C=a-b;
Printf(“subraction: %d\n”);
Break;
Case 3:
C=a*b;
Printf(“multiplication: %d\n”);
Break;
Case 4:
C=a/b;
Printf(“division: %d\n”);
Break;
Case 0:
Exit(0);
Break;
Default:
Getch();
RESULT
Thus the c program for switch statement was executed and the output was verified
EX.NO.: 3(a) LOOPS: FOR LOOP
DATE:
AIM
ALGORITHM
Step 3: set a loop to find the factorial of the given number using the formula
Fact=fact*I
Step 5: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
Void main()
Int fact=1,i,num;
Scanf(“%d”,num);
For(i=1;i<=num;i++)
Fact=fact*i;
RESULT
Thus the c program for FOR loop was executed and the output was verified
EX.NO.:3(b) WHILE LOOP
DATE:
AIM
To write a c program to find sum of digits and to check the given number is palindrome or not
ALGORITHM
Step 8: after the end of the loop print the sum and reverse number of the digit
Step 9: find whether the reverse number is equal to the given number or not.If equal the number is not
palindrome.
PROGRAM
#include<stdio.h>
#include<conio.h>
Void main()
Scanf(“%ld”,&num);
a=num;
While(num!=0)
{
Rem=num%10;
Sum=sum+rem;
Rnum=rnum*10+rem;
Num=num/10;
If(a==num)
Else
RESULT
Thus the c program for while loop was executed and the output was verfied
EX.NO.: 3(c) DO……WHILE
DATE:
AIM
To write a c program to add numbers until the user enter zero using do…while
ALGORITHM
Step 7: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
Void main()
Clrscr();
Do
Printf(“enter a number:”);
Scanf(“%lf”,&number);
Sum+=number;
While(number!=0.0)
Printf(“sum=%.2lf”,sum);
Getch();
}
RESULT
Thus the c program for do…while loop was executed sucessfully and the output was verified
EX.NO.: 4(a) ARRAYS: 1D
DATE:
AIM
ALGORITHM
Step 3: using for loop give the condition to square the values
Step 5: stop
PROGRAM
#included<stdio.h>
#include<conio.h>
Void main()
int values[10]={1,2,3,4,5,};
int i;
for(i=5;i<10;++i)
values[i]=i*i;
for(i=0;i<10;++i)
getch();
RESULT
Thus the c prograam for one dimensional array was executed sucessfully and the output
was verified.
EX.NO.:4(b) ARRAY:2D
DATE:
AIM
ALGORITHM
For(i=0;i<=3;i++)
Step 4: if the condition is true print the statement inside the for loo[p
Step 5: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
int stud[4][2];
int i;
for(i=0;i<=3;i++)
scanf("%d %d",&stud[i][0],&stud[i][1]);
for(i=0;i<=3;i++)
getch();
}
RESULT
Thus the c program for two dimensional array was executed sucessfully and the output
were verified
EX.NO.:4(c) MULTIDIMENSIONAL ARRAY
DATE:
AIM
ALGORITHM
Step 11: multiply the A and B matrix and store the elements in the C matrix
PROGGRAM
#include<stdio.h>
#include<conio.h>
void main()
int a[25][25],b[25][25],c[25][25],i,j,k,r,s;
int m,n;
scanf("%d %d",&m,&n);
if(m!=r)
else
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("\t %d",&a[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("\t %d",&b[i][j]);
for(i=0;i<m;i++)
printf("\n");
for(j=0;j<n;j++)
printf("\t %d",a[i][j]);
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t %d",b[i][j]);
for(i=0;i<m;i++)
printf("\n");
for(j=0;j<n;j++)
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][j]*b[i][j];
for(i=0;i<m;i++)
printf("\n");
for(j=0;j<n;j++)
printf("\t %d",c[i][j]);
RESULT
Thus the c program for multidimensional array was executed sucessfully and the output was
Verified
EX.NO.:4(d) TRAVERSAL ARRAY
DATE:
AIM
ALGORITHM
Step 6: compare every character above the middle character with the below character of the
Middle character
Step 8: if the character is not equal then print the given string is not a palindrome
Step 9: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
int len=0,i,j;
char name[25];
scanf("%s",name);
while(name[len]!='\0')
len++;
printf("\n %d",len);
for(i=0,j=len-1;i<len/2;i++,j--)
if(name[i]!=name[j])
exit(0);
RESULT
Thus the c program or traversal array was executed sucessfully and the output was verified
EX.NO.:5(a) STRING OPERATION: STRLEN
DATE:
AIM
ALGORITHM
Step 1: Start
Step 5: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
char str[50];
int len;
scanf("%[^\n]",str);
for(len=0;str[len]!='\0';len++);
RESULT
Thus the c program for finding the length of the string was executed sucessfully and the
Was verified
EX.NO.:5(b) STRCMP
DATE:
AIM
ALGORITHM
Step 1: start
Step 5: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
int i;
char str1[20],str2[20];
scanf("%s %s",str1,str2);
for(i=1;i<2;i++)
if(str1[i]==str2[i])
else
}
}
RESULT
Thus the c program to compare the given strings was executed sucessfully and the output
Was verified
EX.NO.:5(c) STRING CONCATENATION
DATE:
AIM
ALGORITHM
Step 1: start
Step 6: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
char str1[10],str2[10],str3[40];
int i,j;
scanf("%s %s",str1,str2);
for(i=0;str1[i]!='\0';i++)
str3[i]=str1[i];
for(j=0;str2[j]!='\0';j++)
str3[i]=str2[j];
i++;
}
str3[i]='\0';
RESULT
Thus the c program to concatenate the given string was executed sucessfully and the output
Was verified
EX.NO.:6(a) FUNCTIONS: CALL AND RETURN
DATE:
AIM
ALGORITHM
Step 5: return the addition values to the called function from the calling function
Step 7: end
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
scanf("%d %d",&a,&b);
c=add(a,b);
add(int x,int y)
int z;
z=x+y;
return(z);
}
RESULT
Thus the c program to call and return the was executed sucessfully and the output
Was verified
EX.NO.:6(b) FUNCTIONS:PASSING PARAMETER BY VALUES
DATE:
AIM
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
int x, y;
scanf("%d%d",&x,&y);
swap(x, y);
return 0;
}
int temp;
temp = b;
b = a;
a = temp;
RESULT
Thus the c program to swap the two numbers using call by value was executed sucessfully and the
DATE:
AIM
RESULT
Thus a C Program Sorting using pass by reference was executed and the output was obtained.
EX.NO.:6(d) FUNCTIONS:PASSING ARRAY TO FUNCTIONS
DATE:
AIM
To write a c program to illustrate the method of passing of a two dimensional array to a function
ALGORITHM
Step 2: Start at index zero, compare the element with the next one (a[0] & a[1] (a is the name of the array)),
and swap if a[0] > a[1]. Now compare a[1] & a[2] and swap if a[1] > a[2]. Repeat this process until the end of
the array. After doing this, the largest element is present at the end. This whole thing is known as a pass. In the
first pass, we process array elements from [0,n-1].
Step 3: Repeat step one but process array elements [0, n-2] because the last one, i.e., a[n-1], is present at its
correct position. After this step, the largest two elements are present at the end.
Step 5: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
scanf("%ld", &n);
scanf("%ld", &array[c]);
bubble_sort(array, n);
return 0;
long c, d, t;
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
RESULT
Thus the c program to sort the elements using bubble sort for passing array to function was
DATE:
AIM
ALGORITHM
RECURSIVE FUNCTION
Step 2: declare mid as ‘static int’ of local declaration and ‘i’ as ‘int’ as ‘int’ data type
Step 3: mid=(low+high)
Step 4: else
PROGRAM
#include<stdio.h>
#include<conio.h>
int key;
void main()
int a[50],i,n,loc;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&key);
loc=bin(a,0,n);
if(loc==0)
else
{
printf("sucessful search.\n");
int i;
if(low<=high)
mid=(low+high)/2;
if(key<b[mid])
high=mid-1;
bin(b,low,high);
else if(key>b[mid])
low=mid+1;
bin(b,low,high);
else if(key==b[mid])
return(mid+1);
else
return(0);
}
RESULT
Thus the c program recursion process was executed sucessfully and the output was verified
EX.NO.:8(a) POINTERS:POINTERS TO FUNCTIONS
DATE:
AIM
To write a c program for printing the sum of the salary and bonus using pointers to functions
ALGORITHM:
Step 6: stop
PROGRAM
#include <stdio.h>
*var = *var+b;
void main()
scanf("%d", &salary);
printf("Enter bonus:");
scanf("%d", &bonus);
salaryhike(&salary, bonus);
}
RESULT
Thus the c program for printing the sum of salary and bonus using pointer to functions
DATE:
AIM
ALGORITHM
Step 5: using for loop condition for(i=0; i< ARRAY_SIZE ; ++i), assign the array values
Step7: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#define ARRAY_SIZE 5
void main()
int i = 0;
int (*ptr)[ARRAY_SIZE];
ptr = &arr;
}
RESULT
Thus the c program using pointer to array was executed sucessfully and the output was verified.
EX.NO.:8(c) POINTERS: POINTERS TO STRING
DATE:
AIM
ALGORITHM
Step 6: stop
PROGRAM
#include <stdio.h>
#include<conio.h>
void main()
while(*ptr != '\0')
printf("%c", *ptr);
ptr++;
RESULT
Thus the c program for displaying the string using pointer to strings
EX.NO.:8(d) POINTERS: POINTERS TO POINTERS
DATE:
AIM
ALGORITHM
Step 5: the pointer ‘p’ is pointing to the address of ‘a’ the double pointer ‘pp’ is pointing to the address ‘p’
Step 8: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main ()
int a = 10;
int *p;
int **pp;
p = &a;
pp = &p;
printf("address of a: %x\n",p);
printf("address of p: %x\n",pp);
}
RESULT
Thus the c program for pointer to pointer was executed sucessfully and the output was vereifed
EX.NO.:8(e) POINTERS: ARRAY OF POINTER
DATE:
AIM
ALGORITHM
Step 4: using the for loop ,print the values using pointers
Step 5: stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
int a = 10;
int b = 20;
int c = 30;
int i = 0;
RESULT
Thus the c program for pointers to pointers was executed sucessfully amd the output was verified
EX.NO.:9(a) STRUCTURES: NESTED STRUCTURES
DATE:
AIM
ALGORITHM
a=b;
b=c;
PROGRAM
#include<stdio.h>
#include<conio.h>
Void main()
scanf("%d",&num);
if(num==0)
printf("0");
else
{
for(i=0;i<num;i++)
fib=fib+a;
a=b;
b=fib;
printf("%d\t",fib);
RESULT
Thus the c program to generate the fibbonaci series was executed sucessfully and the output
Was verified
EX.NO.:9(b) STRUCTURES: POINTERS TO STRUCTURES
DATE:
AIM
To write a c program for displaying the students records using pointers to structures
ALGORITHM
Step 6: stop
PROGRAM
#include <stdio.h>
#include <string.h>
struct student
int id;
char name[30];
float percentage;
};
void main()
int i;
ptr = &record1;
RESULT
Thus the c program for pointers to structures was executed sucessfully and output was verified
EX.NO.:9(c) STRUCTURES: ARRAY OF STRUCTURES
DATE:
AIM
To write a c program for displaying three students name, id, marks using array of structures
ALGORITHM
Step 5: give the data for name ,id ,and the marks of three students
Step 7: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
struct student
char name[20];
int id;
float marks;
};
void main()
int dummy;
scanf("%s %d %f",s1.name,&s1.id,&s1.marks);
scanf("%c",&dummy);
printf("Enter the name, id, and marks of student 2 ");
scanf("%s %d %f",s2.name,&s2.id,&s2.marks);
scanf("%c",&dummy);
scanf("%s %d %f",s3.name,&s3.id,&s3.marks);
scanf("%c",&dummy);
printf("%s %d %f\n",s1.name,s1.id,s1.marks);
printf("%s %d %f\n",s2.name,s2.id,s2.marks);
printf("%s %d %f\n",s3.name,s3.id,s3.marks);
RESULT
Thus the c program for array of structure was executed sucessfully and the output was verified
EX.NO.:9(d) STRUCTURES: ARRAY OF UNION
DATE:
AIM
ALGORITHM
Step 4: fuctions are called first and process to be done. After this control pass to the main function
Step 9: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
int i,size1,size2,size,j=0,k;
scanf("%d",&size1);
scanf("%d",&size2);
int a[size1],b[size2],uni[size1+size2];
scanf("%d",&a[i]);
for(i=0;i<size2;i++)
scanf("%d",&b[i]);
for(i=0;i<size1;i++)
uni[j]=a[i];
j++;
for(i=0;i<size2;i++)
uni[j]=b[i];
j++;
sort(size1+size2,uni);
size=removerepeated(size1+size2,uni);
for(i=0;i<size;i++)
printf("%d\n",uni[i]);
int i,j,k;
for(i=0;i<size;i++)
for(j=i+1;j<size;)
if(a[i]==a[j])
for(k=j;k<size;k++)
a[k]=a[k+1];
size--;
Else
j++;
return(size);
int i,j,temp;
for(i=0;i<size;i++){
for(j=i+1;j<size;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
RESULT
Thus the c program for union of values of two array using array of union was executed sucessfully
DATE:
AIM
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
FILE *fp;
char fName[20];
scanf("%s",fName);
fp=fopen(fName,"w");
if(fp==NULL)
{
exit(0);
putc('A',fp);
putc('B',fp);
putc('C',fp);
fclose(fp);
fp=fopen(fName,"r");
if(fp==NULL)
exit(0);
printf("%c",getc(fp));
printf("%c",getc(fp));
printf("%c",getc(fp));
fclose(fp);
RESULT
Thus the c program for writing and reading the file was executed sucessfully and the output
Was verified
EX.NO.:10(b) FILE: FILE POINTER
DATE:
AIM
ALGORITHM
Step 5: using the while statements and if statement we are countig the number of character in the file
Step 8: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
FILE *fp;
char ch;
int noc = 0;
fp = fopen("ok.txt", "r");
while(1)
ch = fgetc(fp);
if(ch == EOF)
break;
}
noc++;
fclose(fp);
RESULT
Thus the c program for count the number of character in a text file was executed sucessfully
DATE:
AIM
To write a c program for file operation using the fprrintf() and scanf() statement
ALGORITHM
Step 6: file is opening for second time for reading the file
Step 9: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
FILE *fp;
fp = fopen("ok.txt", "w");
fclose(fp);
char buff[255];
fp = fopen("ok.txt", "r");
fclose(fp);
RESULT
Thus the c program for file operation using fprintf() and fscanf() statement was executed
DATE:
AIM
To write a c program to finding and displaying the current position of the pointer using random
access file
ALGORITHM
Step 3: using fopen() we are opening the file both in write and read mode
Step 4: using random access file command the current position of file is found
Step 8: stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void main ()
FILE *fp;
int c;
fp = fopen("ok.txt","w+");
rewind(fp);
c = fgetc(fp);
if( feof(fp) )
break;
printf("%c", c);
fclose(fp);
RESULT
Thus the c program for printing the current position of the file using random access file was
DATE:
AIM
To write a c program for printing some values like height, number, letter, letter sequence,
ALGORITHM
Step 2: defining and declaring the values for the variables like number, letter, letter sequence,
Step 4: give the print statements to print the height, number, letter,letter sequence and backslash
Character
Step 5: stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
}
RESULT
Thus the c program for printing stuffs by processor directives was exected sucessfully and output was
Verified