You are on page 1of 12

C PROGRAMMING LAB (2020 SYLLABUS)

C Programming Lab
Subject Code: 20UCA1CC2P/20UIT1CC2P
Class: BCA/BSC IT
 Programs Inside
Develop a program in C
1. Using assignment statements.
2. Using different forms of If Statement.
3. To demonstrate Logical operators
4. Using While, Do-While & For Loop
5. Using Switch
6. To illustrate the use of Functions& Pointers
7. Using Macro definitions to test whether a character is
uppercase or lowercase
8. To make use of arrays.
9. To manipulate Strings.
10. To demonstrate structure.
11. Using console I/O Functions.
12. To copy the contents of one file into another

Prepared by Lt.J.Hajiram Beevi, JMC, Trichy Page 1


C PROGRAMMING LAB (2020 SYLLABUS)

1. Using assignment statements.


#include <stdio.h>
int main()
{
int i=5, j=9, k=5;
int a=6,b=2;
int m=0,n=0;
printf("%d\t%d\n",i,j);
printf("%d\n",m);
m=i++-j--;
printf("%d\n",m);
printf("%d\t%d\n",i,j);
k+=++j+k;;
printf("%d\n",k);
a+=b;
printf("Value of a is after addition %d\n",a);
a-=b;
printf("Value of a is after subtraction %d\n",a);
a*=b;
printf("Value of a is after multiplication %d\n",a);
a/=b;
printf("Value of a is after integer division %d\n",a);
a%=b;
printf("Value of a is after modulo division %d\n",a);
return 1;
}
2.a. Using different forms of If statement (Simple IF)
#include<stdio.h>
int main()
{
int num1,num2;
printf("Enter first integer\n");
scanf("%d",&num1);
printf("Enter second integer\n");
scanf("%d",&num2);
if(num1==num2)
printf("%d is equal to %d\n",num1,num2);
return 0;
}
2.b. Using different forms of If statement (IF…ELSE)
#include<stdio.h>
int main()
{
int n;
printf("Please enter an integer:\n");
scanf("%d",&n);
if(n%2!=0)
printf("%d is odd\n",n);
else
printf("%d is even\n",n);

Prepared by Lt.J.Hajiram Beevi, JMC, Trichy Page 2


C PROGRAMMING LAB (2020 SYLLABUS)

return 0;
}
2.c. Using different forms of If statement (Nested IF)
#include<stdio.h>
int main()
{
int n;
printf("Please enter an integer:");
scanf("%d",&n);
if(n<=15)
{
if(n>=10)
printf("%d is between 10 and 15\n",n);
}
else
printf("%d is not between 10 and 15\n",n);
return 0;
}
3. a.To demonstrate Logical operators (AND)
#include<stdio.h>
int main()
{
int m1,m2,m3,m4,m5,per;
printf("Enter marks in 5 subjects:\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
per=(m1+m2+m3+m4+m5)/5;
if(per>=60)
printf("First Division\n");
if((per>=50)&&(per<60))
printf("Second Division\n");
if((per>=40)&&(per<50))
printf("Third division");
if(per<40)
printf("Fail\n");
}
3. b.To demonstrate Logical operators (OR)
#include<stdio.h>
int main()
{
char ch;
printf("Enter any character");
ch=getchar();
if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
printf("%c is an alphabet\n",ch);
else
if(ch>='0'&&ch<='9')
printf("%c is digit\n",ch);
else
printf("%c is a special character\n",ch);
return 0;

Prepared by Lt.J.Hajiram Beevi, JMC, Trichy Page 3


C PROGRAMMING LAB (2020 SYLLABUS)

}
3. c.To demonstrate Logical operators (NOT)
#include<stdio.h>
int main()
{
int number;
printf("Please enter an integer value:\n");
scanf("%d",&number);
if(!(number>40))
{
printf("Input check greater than 40 - True");
}
else
{
printf("Input check greater than 40 - False");
}
return 0;
}
4. a. Using While, Do-While & For Loop (While Loop)
#include<stdio.h>
int main()
{
int num,original,factorial;
printf("Type a positive integer:\n");
scanf("%d",&num);
original=num;
factorial=1;
while(num>1)
{
factorial*=num;
num--;
}
printf("Factorial of %d=%d\n",original,factorial);
return 0;
}
4. b. Using While, Do-While & For Loop (Do..While Loop)
#include<stdio.h>
int main()
{
int num,rev;
printf("Enter the number to be reversed:\n");
scanf("%d",&num);
printf("Reverse number is:\n");
do
{
rev=num%10;
printf("%d",rev);
num=num/10;
}
while(num!=0);

Prepared by Lt.J.Hajiram Beevi, JMC, Trichy Page 4


C PROGRAMMING LAB (2020 SYLLABUS)

return 0;
}
4.c. Using While, Do-While & For Loop (Simple FOR Loop)
#include<stdio.h>
int main()
{
int i,low,high,sum=0;
printf("Please type the lower bound:\n");
scanf("%d",&low);
printf("Please type the higher bound:\n");
scanf("%d",&high);
for(i=low;i<=high;i++)
sum+=i;
printf("\n The sum of integers from %d to %d is:%d\n",low,high,sum);
return 0;
}
4.d. Using While, Do-While & For Loop (Nested For Loop)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<4;i++)
for(j=1;j<3;j++)
printf("%5d%5d\n",i,j);
return 0;
}
5. Using Switch
#include<stdio.h>
int main()
{
float value1,value2;
char operator;
printf("Type an expression:");
scanf("%f%c%f",&value1,&operator,&value2);
switch(operator)
{
case '+':
printf("%.2f\n",value1+value2);
break;
case '-':
printf("%.2f\n",value1-value2);
break;
case '*':
printf("%.2f\n",value1*value2);
break;
case '/':
if(value2==0)
printf("Divisible by zero\n");
else
printf("%.2f\n",value1/value2);

Prepared by Lt.J.Hajiram Beevi, JMC, Trichy Page 5


C PROGRAMMING LAB (2020 SYLLABUS)

break;
default:
printf("Unknown operator\n");
}
}
6. a. To illustrate the use of Functions& Pointers (Functions)
#include<stdio.h>
int calc_area();
int length,width,area;
int main()
{
int n;
printf("Enter the length of rectangle\n");
scanf("%d",&length);
printf("Enter the width of rectangle\n");
scanf("%d",&width);
n=calc_area();
printf("Area is %d\n",area);
}
int calc_area()
{
area=length*width;
return area;
}
6. b. To illustrate the use of Functions& Pointers (Pointers)
#include<stdio.h>
void no_swap(int x,int y);
void swap(int *p1,int *p2);
int main()
{
int a=1,b=999;
printf("a=%d,b=%d\n",a,b);
no_swap(a,b);
printf("a=%d,b=%d\n",a,b);
swap(&a,&b);
printf("a=%d,b=%d\n",a,b);
}
void no_swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
void swap(int *px,int *py)
{
int temp;
temp=*px;
*px=*py;
*py=temp;

Prepared by Lt.J.Hajiram Beevi, JMC, Trichy Page 6


C PROGRAMMING LAB (2020 SYLLABUS)

}
7. Using Macro definitions to test whether a character is uppercase or lowercase
#include<stdio.h>
#define IS_UPPER(x) (x>='A'&&x<='Z')
#define IS_LOWER(x) (x>='a'&&x<='z')
int main()
{
char ch;
printf("Enter any character:\n");
ch=getchar();
if(IS_UPPER(ch))
printf("'%c' is uppercase\n",ch);
else if(IS_LOWER(ch))
printf("'%c' is lowercase\n",ch);
else
printf("Entered character is not alphabet");
return 0;
}
8. To make use of arrays.
#include <stdio.h>
void getMatrixElements(int matrix[][10], int row, int column)
{
printf("\nEnter elements: \n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j)
{
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}
void multiplyMatrices(int first[][10],int second[][10],int result[][10],int r1, int c1, int r2, int
c2)
{
for (int i = 0; i < r1; ++i)
{
for (int j = 0; j < c2; ++j)
{
result[i][j] = 0;
}
}
for (int i = 0; i < r1; ++i)
{
for (int j = 0; j < c2; ++j)
{
for (int k = 0; k < c1; ++k)
{
result[i][j] += first[i][k] * second[k][j];
}
}

Prepared by Lt.J.Hajiram Beevi, JMC, Trichy Page 7


C PROGRAMMING LAB (2020 SYLLABUS)

}
}
void display(int result[][10], int row, int column)
{
printf("\nOutput Matrix:\n");
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
printf("%d ", result[i][j]);
if (j == column - 1)
printf("\n");
}
}
}
int main()
{
int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);
while (c1 != r2) {
printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d%d", &r2, &c2);
}
getMatrixElements(first, r1, c1);
getMatrixElements(second, r2, c2);
multiplyMatrices(first, second, result, r1, c1, r2, c2);
display(result, r1, c2);
return 0;
}
9. To manipulate Strings.
#include <stdio.h>
#include<string.h>
int main()
{
char words1[]="jamal";
char words2[]="JMC";
char words3[]="Jamal Mohamed College";
char words4[100]="Information";
char words5[100]="Technology";
int length=0,num=0;
//string length
length=strlen(words1);
printf("\n length of the string is %d\n",length);
//string copy

Prepared by Lt.J.Hajiram Beevi, JMC, Trichy Page 8


C PROGRAMMING LAB (2020 SYLLABUS)

printf("\n Before:");
printf("\n %s\n",words2);
strcpy(words2,words3);
printf("\n After:");
printf("\n %s\n",words2);
//string concatenation
printf("\n Before:");
printf("\n %s\n",words4);
strcat(words4,words5);
printf("\n After:");
printf("\n %s\n",words4);
//string comparison
num=strcmp(words5,words4);
printf("\n%d\n",num);
return 0;
}
10. To demonstrate structure.
#include<stdio.h>
#include<string.h>
struct mark_sheet{
char name[20];
long int rollno;
int marks[10];
int total;
float average;
char rem[10];
char cl[20];
}students[100];
int main(){
int a,b,n,flag=1;
char ch;
printf("How many students : \n");
scanf("%d",&n);
for(a=1;a<=n;++a){
printf("\n\nEnter the details of %d students : ", n-a+1);
printf("\n\nEnter student %d Name : ", a);
scanf("%s", students[a].name);
printf("\n\nEnter student %d Roll Number : ", a);
scanf("%ld", &students[a].rollno);
students[a].total=0;
for(b=1;b<=5;++b){
printf("\n\nEnter the mark of subject-%d : ", b);
scanf("%d", &students[a].marks[b]);
students[a].total += students[a].marks[b];
if(students[a].marks[b]<40)
flag=0;
}
students[a].average = (float)(students[a].total)/5.0;
if((students[a].average>=75)&&(flag==1))
strcpy(students[a].cl,"Distinction");

Prepared by Lt.J.Hajiram Beevi, JMC, Trichy Page 9


C PROGRAMMING LAB (2020 SYLLABUS)

else
if((students[a].average>=60)&&(flag==1))
strcpy(students[a].cl,"First Class");
else
if((students[a].average>=50)&&(flag==1))
strcpy(students[a].cl,"Second Class");
else
if((students[a].average>=40)&&(flag==1))
strcpy(students[a].cl,"Third Class");
if(flag==1)
strcpy(students[a].rem,"Pass");
else
strcpy(students[a].rem,"Fail");
flag=1;
}
for(a=1;a<=n;++a){
printf("\n\n\t\t\t\tMark Sheet\n");
printf("\nName of Student : %s", students[a].name);
printf("\t\t\t\t Roll No : %ld", students[a].rollno);
printf("\n------------------------------------------------------------------------");
for(b=1;b<=5;b++){
printf("\n\n\t Subject %d \t\t :\t %d", b, students[a].marks[b]);
}
printf("\n\n------------------------------------------------------------------------\n");
printf("\n\n Totl Marks : %d", students[a].total);
printf("\t\t\t\t Average Marks : %5.2f", students[a].average);
printf("\n\n Class : %s", students[a].cl);
printf("\t\t\t\t\t Status : %s", students[a].rem);
}
return(0);
}
11. a. Using console I/O Functions.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char ch,lower,upper;
puts("\n Enter any one character:");
ch=getchar();
if(ch>='a'&&ch<='z')
{
putchar(toupper(ch));
}
else
{
putchar(tolower(ch));
}
return 0;
}

Prepared by Lt.J.Hajiram Beevi, JMC, Trichy Page 10


C PROGRAMMING LAB (2020 SYLLABUS)

11. b. Using console I/O Functions.


#include<stdio.h>
int main()
{
char c;
int word=1,digit=0,letter=0;
while((c=getchar())!='\n')
{
if(c==''||c=='\t')
word++;
if(c>='0'&&c<='9')
digit++;
if(c>='A'&&c<='Z'||c>='a'&&c<='z')
letter++;
}
printf("\n Total words\tTotal digits\tTotal letters");
printf("\n%d\t\t%d\t\t%d",word,digit,letter);
return 0;
}
12. To copy the contents of one file into another
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);

Prepared by Lt.J.Hajiram Beevi, JMC, Trichy Page 11


C PROGRAMMING LAB (2020 SYLLABUS)

}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}

Prepared by Lt.J.Hajiram Beevi, JMC, Trichy Page 12

You might also like