C Record CORRECTED
C Record CORRECTED
AIM:
To write a program for I/O statements, operators, expressions using C.
ALGORITHM:
STEP1:Start the program.
STEP2:Declare the variables totamt,amount,subtot,distant,taxamt,quantity,value,discount,tax.
STEP3:Read the values for the variables.
STEP4:Perform addition, subtraction and multiplication.
STEP5:Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
Void main()
{
float totamt,amount,subtot,distant,taxamt,quantity,value,discount,tax;
printf("\nenter the quantity of item sold:");
scanf("%f",&quantity);
printf("\n enter the value of item:");
scanf("%f",&value);
printf("\n enter the discount percentage:");
scanf("%f",&discount);
printf("\n enter the tax:");
scanf("%f",&tax);
amount=quantity*value;
distant=(amount*discount)/100.0;
subtot=amount-distant;
taxamt=(subtot*tax)/100.0;
totamt=subtot+taxamt;
printf("\n\n\n******bill******");
printf("\n quantity sold:%f",quantity);
printf("\n priceperitem:%f",value);
printf("\n amount %f",amount);
printf("\n discount:%f",distant);
printf("\n discounted total: %f",subtot);
printf("\n tax:+ %f",taxamt);
printf("\n total amount %f",totamt); getch();
}
1
OUTPUT:
Enter the quantity of item sold:10
Enter the value of item:50
Enter the discount percentage:10
Enter the tax:10
Quantity sold:10.00
Price per item:50.00
Amount:500.00
Discount:50.00
Discounted total:450.00
Tax:+45
Total amount:495.00
RESULT:
Thus the program for I/O statements, operators and expressions are executed
successfully.
2
DECISION-MAKING CONSTRUCTS: IF-ELSE, GOTO,
Ex.No: 2
SWITCH-CASE, BREAK-CONTINUE
AIM:
To write a Program to check whether a person is eligible to Vote or Not using IF-ELSE.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char name[50];//or*name
printf("\n Type the name of the candidate:");
gets(name);
printf("\n Enter the age:");
scanf("%d",&age);
if(age>=18)
printf("\n %s is Eligibile for vote",name);
else
printf("\n %s is not Eligibile for vote",name);
getch();
}
OUTPUT:
Type the name of the candidate:
Ramu
Enter the age:
25
Ramu is eligible to vote
RESULT:
Thus the program for if-else using C is executed successfully.
3
Ex.No: 2 GOTO
AIM:
ALGORITHM:
#include <stdio.h>
#include<conio.h>
Void main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table; getch();
}
OUTPUT:
RESULT:
Thus the program for goto is executed successfully.
4
Ex.No: 2 SWITCH-CASE
AIM:
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include<conio.h>
Void main() {
int day ;
printf("Enter the day of week");
scanf("%d",&day);
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break; getch();
}}
5
OUTPUT:
RESULT:
6
Ex.No: 2 BREAK
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
if(i == 5)
break;
printf("%d ",i);
}
printf("came outside of loop i = %d",i);
getch();
}
OUTPUT:
1
2
3
4
came outside of loop i = 5
RESULT:
7
Ex.No: 2 CONTINUE
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main(){
int i=1;
for(i=1;i<=10;i++){
if(i==5){
continue;
}
printf("%d \n",i);
}end of for loop
getch();
}
OUTPUT:
1
2
3
4
6
7
8
9
10
RESULT:
Thus the program for continue is executed successfully.
8
Ex.No: 3 LOOPS: FOR, WHILE, DO-WHILE
AIM:
Write a program in C using FOR loop.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int Number, i, Sum = 0;
printf("\nPlease Enter any Integer Value\n");
scanf("%d", &Number);
for(i = 1; i <= Number; i++)
{
Sum = Sum + i;
}
printf("Sum of Natural Numbers = %d", Sum);
getch();
}
OUTPUT:
Please Enter any Integer Value
5
Sum of Natural Numbers =15
RESULT:
Thus the program to find sum of n numbers using for loop is executed successfully.
9
Ex.No: 3 WHILE
AIM:
Write a program in C using while loop.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
getch();
}
OUTPUT:
Enter the number
407
Armstrong number
RESULT:
Thus the program to find Armstrong number using while loop is executed successfully.
10
Ex.No: 3 DO-WHILE
AIM:
Write a program in C using do-while loop.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n1=0,n2=1,n3,i=2,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
do
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
i=1+1;
} while(i<=number);
getch();
}
OUTPUT:
Enter the number of elements:5
0
1
1
2
3
5
8
RESULT:
Thus the program for Fibonacci series using do-while is executed successfully.
11
ARRAYS: 1D AND 2D, MULTI-DIMENSIONAL
Ex.No: 4
ARRAYS, TRAVERSAL
AIM:
Write a program in C using one dimensional array.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main ()
{
int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n",a[i]);
}
getch();
}
12
OUTPUT:
RESULT:
Thus the program to arrange numbers in ascending order using one dimensional array is executed
successfully.
13
Ex.No: 4 TWO DIMENSIONAL ARRAY
AIM:
Write a program in C using two dimensional array.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, rows, columns, a[10][10], b[10][10];
int Addition[10][10], Subtraction[10][10], Module[10][10];
float Division[10][10];
14
for(columns = 0;columns < j;columns++)
{
Addition[rows][columns] = a[rows][columns] + b[rows][columns];
Subtraction[rows][columns] = a[rows][columns] - b[rows][columns];
Division[rows][columns] = a[rows][columns] / b[rows][columns];
Module[rows][columns] = a[rows][columns] % b[rows][columns];
}
}
printf("\nAdd\t Sub\t Div\t Mod");
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
printf("\n%d \t ", Addition[rows][columns]);
printf("%d \t ", Subtraction[rows][columns]);
printf("%.2f \t ", Division[rows][columns]);
printf("%d \t ", Module[rows][columns]);
}
}
getch();
}
RESULT:
Thus the program for two dimensional array using C is executed successfully.
15
Ex.No: 4 MULTI-DIMENSIONAL ARRAY
AIM:
Write a program in C using multi dimensional array.
ALGORITHM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
int arr[3][3][3]= { { {11, 12, 13}, {14, 15, 16}, {17, 18, 19} }, { {21, 22, 23}, {24, 25, 26}, {27,
28, 29} }, { {31, 32, 33}, {34, 35, 36}, {37, 38, 39} }, };
clrscr();
printf(":::3D Array Elements:::\n\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++) {
for(k=0;k<3;k++) {
printf("%d\t",arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
getch();
}
OUTPUT:
3D Array Elements
{11, 12, 13}, {14, 15, 16}, {17, 18, 19} }, { {21, 22, 23}, {24, 25, 26}, {27, 28, 29} }, { {31, 32,
33}, {34, 35, 36}, {37, 38, 39}
RESULT:
Thus the program for multi dimensional array is executed successfully.
16
Ex.No: 5 STRINGS OPERATIONS
AIM:
Write a program in C for string operations.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main( )
{
char source[ ] = " fresh2refresh" ;
char target[ ]= " C tutorial" ;
printf ( "\nSource string = %s", source ) ;
printf ( "\nTarget string = %s", target ) ;
strcat ( target, source ) ;
printf ( "\nTarget string after strcat( ) = %s", target ) ;
getch();
}
OUTPUT:
Copying
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main( )
{
char source[ ] = "fresh2refresh" ;
char target[20]= "" ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
17
strcpy ( target, source ) ;
printf ( "\ntarget string after strcpy( ) = %s", target ) ;
getch();
}
OUTPUT:
Fresh2refresh
Comparing
#include <stdio.h>
#include <string.h>
#include<conio.h>
int main( )
{
char str1[ ] = "fresh" ;
char str2[ ] = "refresh" ;
int i, j, k ;
i = strcmp ( str1, "fresh" ) ;
j = strcmp ( str1, str2 ) ;
k = strcmp ( str1, "f" ) ;
printf ( "\n%d %d %d", i, j, k ) ;
getch();
}
OUTPUT:
0 -1 1
String Length
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main( )
{
int len;
char array[20]="fresh2refresh.com" ;
len = strlen(array) ;
printf ( "\string length = %d \n" , len ) ;
getch();
}
18
OUTPUT:
RESULT:
19
FUNCTIONS: CALL, RETURN, PASSING
Ex.No: 6 PARAMETERS BY (VALUE, REFERENCE),
PASSING ARRAYS TO FUNCTION
AIM:
Write a program in C for function call and return.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include<conio.h>
int addNumbers(int a, int b); // function prototype
void main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
getch();
}
int addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}
OUTPUT:
RESULT:
Thus the program to add two numbers using function is executed successfully.
20
PASSING PARAMETERS BY (VALUE,
Ex.No: 6
REFERENCE)
AIM:
Write a program in C for function call and return.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include<conio.h>
int sum (int n);
void main()
{
int a = 5;
printf("\n The value of 'a' before the calling function is = %d", a);
a = sum(a);
printf("\n The value of 'a' after calling the function is = %d", a);
getch();
}
int sum (int n)
{
n = n + 20;
printf("\n Value of 'n' in the called function is = %d", n);
return n;
}
OUTPUT:
21
Call by Reference
#include <stdio.h>
#include<conio.h>
int sum (int *n);
void main()
{
int a = 5;
printf("\n The value of 'a' before the calling function is = %d", a);
sum(&a);
printf("\n The value of 'a' after calling the function is = %d", a);
getch();
}
int sum (int *n)
{
*n = *n + 20;
printf("\n value of 'n' in the called function is = %d", n);
}
OUTPUT:
RESULT:
Thus the program for call by value and call by reference using function is executed successfully.
22
PASSING ARRAY TO FUNCTION
Ex.No: 6
AIM:
Write a program in C for passing array to function.
ALGORITHM:
#include<stdio.h>
#include<conio.h>
int minarray(int arr[],int size){
int min=arr[0];
int i=0;
for(i=1;i<size;i++){
if(min>arr[i]){
min=arr[i];
}
}//end of for
return min;
}//end of function
void main(){
int i=0,min=0;
int numbers[]={4,5,7,3,8,9};//declaration of array
min=minarray(numbers,6);//passing array with size
printf("minimum number is %d \n",min);
getch();
}
OUTPUT:
minimum number is 3
RESULT:
Thus the program to find minimum number using passing array to function is executed
successfully.
23
Ex.No: 7 RECURSION
AIM:
Write a program in C for recursion.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
int find_factorial(int);
void main()
{
int num, fact;
//Ask user for the input and store it in num
printf("\nEnter any integer number:");
scanf("%d",&num);
//Calling our user defined function
fact =find_factorial(num);
//Displaying factorial of input number
printf("\nfactorial of %d is: %d",num, fact);
getch();
}
int find_factorial(int n)
{
//Factorial of 0 is 1
if(n==0)
return(1);
//Function calling itself: recursion
return(n*find_factorial(n-1));
}
OUTPUT:
factorial of 5 is: 120
8.Pointer for Function
RESULT:
Thus the program to find factorial of a number using recursion is executed successfully.
24
POINTERS: POINTERS TO FUNCTIONS,
Ex.No: 8
ARRAYS,STRINGS,
POINTERS TO POINTERS, ARRAY OF POINTERS
AIM:
Write a program in C for pointers to function.
ALGORITHM:
#include <stdio.h>
#include<conio.h>
int add(int,int);
void main()
{
int a,b;
int (*ip)(int,int);
int result;
printf("Enter the values of a and b : ");
scanf("%d %d",&a,&b);
ip=add;
result=(*ip)(a,b);
printf("Value after addition is : %d",result);
getch();
}
int add(int a,int b)
{
int c=a+b;
return c;
}
OUTPUT:
Enter the values of a and b : 25 40
Value after addition is : 65
RESULT:
Thus the program for pointer to function using C is executed successfully.
25
Ex.No: 8 POINTERS TO ARRAYS
AIM:
Write a program in C for pointers to arrays.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include<conio.h>
void main () {
/* an array with 5 elements */
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p;
int i;
p = balance;
/* output each array element's value */
printf( "Array values using pointer\n");
for ( i = 0; i < 5; i++ ) {
printf("*(p + %d) : %f\n", i, *(p + i) );
}
printf( "Array values using balance as address\n");
for ( i = 0; i < 5; i++ ) {
printf("*(balance + %d) : %f\n", i, *(balance + i) );
}
getch();
}
OUTPUT:
Array values using pointer
1000.0, 2.0, 3.4, 17.0, 50.0
Array values using balance as address
1000.0, 2.0, 3.4, 17.0, 50.0
RESULT:
26
Ex.No: 8 POINTERS TO STRING
AIM:
Write a program in C for pointers to string.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <String.h>
#include<conio.h>
void main(){
//initializing the pointer string array
char *students[]={"bhanu","ramu","hari","pinky",};
int i,j,a;
printf("The names of students are:\n");
for(i=0 ;i<4 ;i++ )
printf("%s\n",students[i]);
getch();
}
OUTPUT:
Bhanu,ramu,hari,pinky
RESULT:
27
Ex.No: 8 POINTER TO POINTER
AIM:
Write a program in C for pointer to pointer.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include<conio.h>
void main()
{
int num=123;
28
printf("\n Value of num using pr1 is: %d", **pr1);
/*Possible ways to find address of num*/
printf("\n Address of num is: %p", &num);
printf("\n Address of num using pr2 is: %p", pr2);
printf("\n Address of num using pr1 is: %p", *pr1);
RESULT:
29
Ex.No: 9 STRUCTURES: NESTED STRUCTURES, POINTERS
TO STRUCTURES, ARRAYS OF STRUCTURES AND UNIONS
AIM:
Write a program in C for nested structures, pointers to structures, arrays of structures and unions.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <string.h>
#include<conio.h>
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}e1;
void main( )
{
//storing employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;
30
//printing first employee information
printf( "employee id : %d\n", e1.id);
printf( "employee name : %s\n", e1.name);
printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.y
yyy);
getch();
}
OUTPUT:
Sonoo jaisol
6/6/2004
RESULT:
Thus the program for structure is executed successfully.
31
Ex.No: 9 POINTERS TO STRUCTURES
AIM:
Write a program in C for pointers to structures.
ALGORITHM:
OUTPUT:
Enter age: 25
Enterweight:50.3
Age 25
Weight 50.3
RESULT:
Thus the program for pointers to structure is executed successfully.
32
Ex.No: 9 ARRAY OF STRUCTURES
AIM:
Write a program in C for array of structures.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
void main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
getch();
}
33
OUTPUT:
Student information list
1 rosy
2 john
3 raju
4 jessy
5 amutha
RESULT:
Thus the program for array of structures is executed successfully.
34
Ex.No: 9 ARRAY OF UNION
AIM:
Write a program in C for array of UNION.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
//declaration of student array.
union student s[3];
35
OUTPUT:
Enternameofstudent: Roy
Enterthestudentrollno: 1
studentdivision: A
Enternameofstudent: Jason
Enterthestudentrollno: 2
studentdivision: B
Enternameofstudent: Tom
Enterthestudentrollno: 3
student division: A
RESULT:
Thus the program for array of union is executed successfully.
36
Ex.No: 10 FILES: READING AND WRITING, FILE POINTERS, FILE
OPERATIONS, RANDOM ACCESS, PROCESSOR DIRECTIVES
AIM:
Write a program in C for files reading and writing, file pointers, file operations, random access,
processor directives.
Reading a file
fgetc(file_pointer): It returns the next character from the file pointed to by the file pointer.
When the end of the file has been reached, the EOF is sent back.
fgets(buffer, n, file_pointer): It reads n-1 characters from the file and stores the string in a
buffer in which the NULL character ‘\0’ is appended as the last character.
PROGRAM:
#include <stdio.h>
int main()
{
FILE * file_pointer;
char buffer[30], c;
file_pointer = fopen("fprintf_test.txt", "r");
printf("----read a line----\n");
fgets(buffer, 50, file_pointer);
printf("%s\n", buffer);
printf("----read and parse data----\n");
file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer
char str1[10], str2[2], str3[20], str4[2];
fscanf(file_pointer, "%s %s %s %s", str1, str2, str3, str4);
printf("Read String1 |%s|\n", str1);
printf("Read String2 |%s|\n", str2);
printf("Read String3 |%s|\n", str3);
printf("Read String4 |%s|\n", str4);
printf("----read the entire file----\n");
file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer
while ((c = getc(file_pointer)) != EOF) printf("%c", c);
37
fclose(file_pointer);
return 0;
}
Writing to file
The above program writes a single character into the fputc_test.txt file until it reaches the next
line symbol “\n” which indicates that the sentence was successfully written. The process is to
take each character of the array and write it into the file.
#include <stdio.h>
int main() {
int i;
FILE * fptr;
char fn[50];
char str[] = "Guru99 Rocks\n";
fptr = fopen("fputc_test.txt", "w"); // "w" defines "writing mode"
for (i = 0; str[i] != '\n'; i++) {
/* write to file using fputc() function */
fputc(str[i], fptr);
}
fclose(fptr);
return 0;
}
Random Access in File
fseek() function is used to take file pointer to a particular position data file.
Syntax: fseek(Fpointer, Position, Initial);
Fpointer is name of file pointer variable.
Initial specifies the position from where file pointer should be jumped. It can three
values :
0: From the beginning of file.
1: Current position.
2: End of file
#include<stdio.h>
int main()
{
FILE *f1;
char line[80] , s;
int n;
f1=fopen(“data.txt”,”r”);
fgets(line,80,f1);
printf(“%s”,line);
fseek(f1,2,0);
/*File pointer jumped to 3rd character from beginning*/
38
s=getc(f); /*5th Character read from file*/
printf(“\n%c”,s);
fseek(f1,2,1);
/*File pointer jumped to 3rd character from current
position*/
s=getc(f);
printf(“\n%c”,s);
fseek(f1,0,2);
/*File pointer jumped end of file* /
s=getc(f);
printf(“\n%c”,s);
fclose(f);
return(0);
}
Preprocessor Directive
The #error preprocessor directive indicates error. The compiler gives fatal error
if #error directive is found and skips further compilation process.
#include<stdio.h>
#ifndef __MATH_H
#error First include then compile
#else
void main(){
float a;
a=sqrt(7);
printf("%f",a);
}
#endif
RESULT:
Thus the program in C for files reading and writing, file pointers, file operations, random access,
processor directives is executed successfully.
39
40