C Programming Lab M Scheme
C Programming Lab M Scheme
DATE:
AIM:
REQUIREMENTS:
• TURBO C
ALGORITHM:
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr(); // clears the output window
printf("\n Name : David"); // prints the name
printf("\n Address : London"); // prints the address
getch();
}
OUTPUT
RESULT
Thus a c program to print name and address was written, compiled, executed and output was
verified.
1
DATE:
AIM:
REQUIREMENTS:
• TURBO C
ALGORITHM:
• si=p*n*r/100;
• ci= p*pow((1+r/100),n)-p
PROGRAM
#include<stdio.h>
void main()
float p,n,r,si,ci;
clrscr();
scanf("%f%f%f",&p,&n,&r);
si=p*n*r/100;
ci=p*pow((1+r/100),n)-p;
OUTPUT
2
RESULT:
Thus, the program to calculate simple and compound interest was written, compiled,
executed and output was verified.
DATE:
AIM:
3
i) using third variable
REQUIREMENTS:
• TURBO C
#include <stdio.h>
int main()
int x, y, temp;
clrscr();
temp = x;
x = y;
4
y = temp;
getch();
return 0;
OUTPUT
#include <stdio.h>
int main()
int a, b;
5
scanf("%d%d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
getch();
return 0;
OUTPUT
RESULT
Thus a c program to swap two values i) using third variable and ii) without third variable was
written, compiled, executed and output was verified.
DATE:
AIM:
To write a c program to convert a given number of days into months and days using integer
arithmetic operators.
REQUIREMENT:
• TURBO C
6
ALGORITHM:
PROGRAM
#include<stdio.h>
void main()
int months,days;
printf(“Enter days\n”);
scanf(“%d”,&days);
months=days/30;
days=days%30;
OUTPUT
RESULT
Thus a c program to convert a given number of days into months and days was written,
compiled, executed and output was verified.
7
EXP NO : 4 EVALUATION OF VARIABLE EXPRESSION
DATE:
AIM:
REQUIREMENT:
• TURBO C
ALGORITHM:
8
4.Print the variable
PROGRAM
#include<stdio.h>
void main()
float a,b,c,x;
a=9,b=12,c=3;
x=a-b/3+c*2-1;
printf("x=%f\n",x);
Output
RESULT
Thus a c program to use of variables in expression and evaluation was written, compiled,
executed and output was verified.
DATE:
AIM:
REQUIREMENT:
• TURBO C
ALGORITHM:
ALGORITHM
9
4. Print the values
PROGRAM
#include<stdio.h>
#define f 40
int main()
float c;
c=(f-32)/1.8;
return(0);
Output
RESULT
Thus the program temperature in Fahrenheit to Celsius using preprocessor was written,
compiled, executed and output was verified.
10
EXP NO : 6 LARGEST OF GIVEN THREE NUMBERS
DATE:
AIM:
REQUIREMENT:
• TURBO C
ALGORITHM:
3. Using if statement check whether a is greater than b if true then check whether a is greater
than c.
PROGRAM
11
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
printf("largest=%d",a);
else
printf("largest=%d",c);
else
if (b>c)
printf("largest=%d",b);
else
printf("largest=%d",c);
getch();
OUTPUT
12
RESULT
Thus a c program to find the largest of three numbers was written, compiled, executed and
output was verified.
DATE:
AIM:
.REQUIREMENTS:
• TURBO C
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
13
{
int i,n,f;
clrscr();
scanf("%d",&n);
f=1;
for(i=1;i<=n;i++)
f=f*i;
getch();
OUTPUT
RESULT:
Thus, the program to calculate the factorial of a given was written, compiled, executed and
output was verified.
14
EXP NO : 7b. PRIME NUMBERS
DATE:
AIM:
REQUIREMENTS:
• TURBO C
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
int main()
{
int N, i, j, isPrime, n;
printf("To print all prime numbers between 1 to N\n");
printf("Enter the value of N\n");
scanf("%d",&N);
/* For every number between 2 to N, check whether it is prime number or not */
printf("Prime numbers between %d to %d\n", 1, N);
15
for(j = 2; j <= i/2; j++){
/* Check If any number between 2 to i/2 divides I completely If yes i cannot be prime no */
if(i % j == 0){
isPrime = 1;
break;
}
}
OUTPUT
RESULT:
Thus, the program printing prime numbers was written, compiled, executed and output was
verified.
DATE:
16
AIM
REQUIREMENT:
• TURBO C
ALGORITHM
2. Declare a structure student with variables name, roll no, mark, tot.
5. Get the student roll no, name, mark using for loop.
7. Using for loop display name, roll no and total for each student.
PROGRAM
#include<stdio.h>
#include<conio.h>
struct stu
{
int no;
char stuname[100];
int mark[20];
int totalmarks;
};
void main()
{
clrscr();
struct stu s[50];
int n,i,j;
printf("Enter Number of Students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter Number,Student name and 6 Marks one by one\n");
scanf("%d%s",&s[i].no,s[i].stuname);
for(j=0;j<=5;j++)
{
17
scanf("%d",&s[i].mark[j]);
}
}
printf("\n*******************************************");
printf("\nNumber\tNmae\tTotalMarks");
printf("\n*******************************************\n");
for(i=0;i<n;i++)
{
s[i].totalmarks=0;
for(j=0;j<6;j++)
{
s[i].totalmarks=s[i].totalmarks+s[i].mark[j];
}
printf("%d\t%s\t%d\n",s[i].no,s[i].stuname,s[i].totalmarks);
}
getch();
}
OUTPUT
18
RESULT
Thus a c program to display student total marks was written, compiled, executed and output
was verified.
DATE:
AIM
To write a c program using the function power(a,b) to calculate the value of a raised to b.
REQUIREMENT:
• TURBO C
ALGORITHM
19
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
int power(int a,int b);
int main()
{
int a,b,value;
printf("enter the value of a and b: ");
scanf("%d%d",&a,&b);
value=power(a,b);
printf("the value of a = %d raised to b = %d is %d",a,b,value);
getch();
return 0;
}
int power(int a,int b)
{
int i,value;
value=a;
for(i=1;i<b;i++)
{
value=value*a;
}
return value;
}
OUTPUT
20
RESULT
Thus a c program using the function power(a,b) to calculate the value of a raised to b was
written, compiled, executed and output was verified.
DATE:
21
AIM
REQUIREMENT:
• TURBO C
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
int l=0;
char *p;
char name[20];
p=name;
clrscr();
gets(name);
while(*p !=’\0’)
l++;
22
p++;
printf(“\n”);
OUTPUT
RESULT
Thus a c program to find the length of the string using pointers was written, compiled,
executed and output was verified.
DATE: REVERSE
AIM:
23
REQUIREMENTS:
• TURBO C
ALGORITHM:
5. Assign m with n.
6. Using while statement, find the sum of digits and number of digits using divide and
modulo operation.
7. Print the sum of digits and number of digits of the given number.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
int count=0,sum=0,rev;
int r,n,m;
clrscr();
scanf("%d",&n);
m=n;
count=count+1;
r=m%10;
24
printf("%d",r);
sum=sum+r;
m=m/10;
printf("\n");
getch();
OUTPUT
RESULT:
Thus, the program to calculate Sum of all individual digits and also print
the above number in reverse order was written, compiled, executed and output was verified.
DATE:
AIM:
REQUIREMENTS:
• TURBO C
ALGORITHM:
25
1. Start the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
int f1,f2,f3,n,i,sum=0;
f1=-1,f2=1;
scanf(“%d”,&n);
for(i=0;i<n;i++)
f3=f1+f2;
f1=f2;
f2=f3;
sum=sum+f3;
printf(“%d\n”,f3);
getch();
26
OUTPUT
RESULT
Thus, the program to calculate Fibonacci series up to N terms and its sum was written,
compiled, executed and output was verified.
DATE:
AIM:
REQUIREMENTS:
• TURBO C
ALGORITHM:
27
4.Print the Values
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
int year,
printf(“enter a year”);
scanf(“%d”,&year);
if(year%400==0)
elseif (year%100==0)
elseif(year%4==0)
else
OUTPUT
RESULT
Thus, the program to print whether a year or not was written, compiled, executed and
output was verified.
28
EXP NO : 13 PRINTING STRING ARGUMENTS IN REVERSE
DATE:
AIM:
To write a C program to print the string arguments in reverse order using command line
arguments.
.REQUIREMENTS:
• TURBO C
ALGORITHM:
29
7. Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
clrscr();
char str[50];
for(int i=1;i<=argc;i++){
strcat(str,argv[i]);
int l=strlen(str);
for(i=l-1;i>=0;i--)
printf(“%c”,str[i]);
getch();
OUTPUT
30
RESULT:
Thus, the program to print the string arguments in reverse using command line arguments
was written, compiled, executed and output was verified.
DATE:
AIM:
REQUIREMENTS:
• TURBO C
6. Swap the greater with the smaller and continue for next strings.
PROGRAM
#include<string.h>
#include<conio.h>
31
void main()
char name[20][30],temp[30];
int n,v,i,j;
clrscr();
scanf("%d",&n);
for(i=0;i<=n;i++)
gets(name[i]);
for(i=0;i<n;i++)
for(j=i+1;j<=n;j++)
v=strcmp(name[i],name[j]);
if(v>0)
strcpy(temp,name[i]);
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j], temp);
puts(name[i]);
getch();
OUTPUT
32
RESULT:
Thus, the program to arrange the given N names in alphabetical order was written, compiled,
executed and output was verified.
33
EXP NO : 15 COUNT NUMBERS AND CHAR IN STRING
DATE:
AIM:
REQUIREMENTS:
• TURBO C
ALGORITHM:
PROGRAM
#include<stdio.h>
int main()
char line[150];
int i,c,d;
c=d=0;
gets(line);
for(i=0;line[i]!='\0';++i)
34
if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
++c;
else if(line[i]>='0'&&c<='9')
++d;
printf("\nCharacters: %d",c);
printf("\nDigits: %d",d);
return 0;
Output
RESULT:
Thus, the program to count the number of characters and numbers in string was written,
compiled, executed and output was verified.
35
EXP NO : 16 SORTING ARRAYS USING FUNCTION
DATE:
AIM:
REQUIREMENTS:
• TURBO C
ALGORITHM:
6.in sortarray function take the first element and compare with the second element. If it is
small don’t interchange or else interchange the two elements.
7.Then Compare the second element with the third element. If it is small don’t interchange or
else interchange two elements.
8.This process is continued till(n-1)th element is compared with the nth element.
9.At the end of the first scan,the highest element comes to the nth position.
10.repeat steps 3,4,5 till the element is comes to the sorted order.
PROGRAM
#include<stdio.h>
int sortArray(int);
36
int array[100];
void main()
int i,size;
scanf(“%d”,&size);
for(i=0;i<size;i++)
printf(“\n array[%d]=”,i);
scanf(“%d”,&array[i]);
sortArray(size);
for(i=0;i<size; i++)
Printf(“%d”,array[i]);
sortArray(n)
int temp=0,i,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(array[i]>array[j])
temp=array[i];
array[i]=array[j];
37
array[j]=temp;
Output
RESULT:
Thus, the program to sort an array of integers using function was written, compiled, executed
and output was verified.
38
DATE :
AIM :
To write a C program to calculate the subject wise and student wise total and store them as a
part of the structure.
REQUIREMENTS :
ALGORITHM
7.Using the for loop display name, s1,s2,s3,s4 and total for each student
PROGRAM
#include<stdio.h>
#include<conio.h>
struct mark
int s1,s2,s3,s4;
char name[20];
int regno,total;
};
void main()
clrscr();
39
printf(“Give 3 students marks in 3 subjects \n”);
for(i=0;i<3;i++)
scanf(“%d%s%d%d%d”,&s[i].regno,s[i].name,&s[i].s1,&s[i],s2,&s[i].s3);
for(j=0;j<3;j++)
s[i].total=s[i].s1+s[i].s2+s[i].s3;
subtotal.s1=subtotal.s1+s[i].s1;
subtotal.s2=subtotal.s2+s[i].s2;
subtotal.s3=subtotal.s3+s[i].s3;
for(i=0;i<3;i++)
getch();
Output
40
Result
Thus the C program to calculate subject wise and student wise total was written, compiled,
executed and output was verified.
DATE:
AIM: To write a C program to read ten values to an array variable and to locate and
display value using pointers.
REQUIREMENTS:
41
• TURBO C
ALGORITHM:
3. Initialize the pointer variable with the address of first variable in array.
6. Using pointer variable locate,display the values stored in array with for loop.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
int a[10];
int *p;
int i;
clrscr();
p=&a[0];
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
printf("%d\n",*p);p++;
getch();
42
}
OUTPUT:
RESULT:
Thus, the program to read ten values to an array variable and to locate and display value
using pointers was written , compiled, executed and output was verified.
TABLE OF INTEGERS
EXP NO :19
DATE :
AIM :
To write a C program to use a table of integers whose size will be specified interactively at
runtime.
Requirements
ALGORITHM
43
1.Start the Program
PROGRAM
#include<stdio.h>
#include<stdlib.h>
void main()
int m,n,i,j,*a;
scanf(“%d%d”,&m,&n);
a=(int*)malloc(m*n*sizeof(int));
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,a+(n*i+j));
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf(“%d\n”,*(a+(n*i+j)));
Output
44
Result
Thus the c program to use a table of integers whose size will be specified interactively at run
time was written, compiled, executed and output was verified.
DATE :
AIM :
REQUIREMENTS
ALGORITHM
45
3.Dynamically allocate the size using pointer variable a using MALLOC function
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
char *a;
a=(char*)malloc(7);
strcpy(a,”welcome”);
printf(“%s\n”,a);
a=(char*)realloc(a,15);
printf(“%s”,a);
OUTPUT
46
Result
Thus the c program to store a character string in a block of memory space created by
MALLOC and then modify the same to store a larger string was written, compiled, executed and
output was verified.
47