You are on page 1of 17

M.S.

Ramaiah Institute of Technology, Bangalore-54


(Autonomous Institute, Affiliated to VTU)
Department of Computer Science and Engineering
TERM: JAN-MAY 2015

CSL201 / FUNDAMENTALS OF COMPUTING LAB LIST OF PROGRAMS


PART A
1. Write and execute C programs
i.
To check whether the entered year is leap year or not using if-else statement.
ii.
To check whether a given number is positive, negative or zero using ifelse
statement.
i.

Leap year

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a Year");
scanf("%d",&year);
if(((year%4 == 0)&& (year%100 != 0)) || (year%400 == 0))
printf("\n%d is a Leap Year", year);
else
printf("\n%d is not a Leap Year", year);
getch();
}
ii.

Positive, Negative or zero

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
if(num==0)
printf( the number is zero);

else if(num>0)
printf(the number is positive);
else
printf( the number is negative);
getch();
}
2. Write and execute C programs
i.
To check whether the triangle is isosceles, equilateral or scalene using if-else
statement.
ii.
To find the greatest of three numbers using nested if statement.
i.

Isoceles, Equilateral or scalene

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
clrscr();
printf("Enter 3 sides of a triangle");
scanf("%d%d%d", &a,&b,&c);
if(a==b && b==c)
printf("\nTriangle is Equilateral Triangle");
else if((a==b || a==c) || (b==a || b==c) || (c==a || c==b))
printf("\nTriangle is Isosceles Triangle");
else
printf("\nTriangle is Scalene Triangle");
getch();
}
ii.

Greatest of 3 numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
clrscr();
printf("Enter 3 numbers");
scanf("%d%d%d", &a,&b,&c);
if(a>b)

{
if(a>c)
printf("\n%d is largest", a);
else
printf("\n%d is largest", c);
}
else
{
if(b>c)
printf("\n%d is largest", b);
else
printf("\n%d is largest", c);
}
getch();
}
3. Write and execute a C program to find the roots of quadratic equation using if-else
statement.
#include <stdio.h>
#include <math.h>
void main( )
{
float a, b, c;
float disc, root1, root2;
float real, imag;
clrscr();
printf("\n Enter the Coefficients of the Equation : ax2+bx+c=O ? : ");
scanf("%f%f%f",&a, &b, &c);
if( a == 0 && b == 0 && c == 0) /* Check for non-zero coefficients */
printf("\n\n Error: Coefficients value must be Non-Zero...");
else
{
disc = b * b - 4 * a * c;
if(disc < 0 )
{ /* Case: imaginary roots */
printf("\n\n Roots are Imaginary...");
real = -b / (2 * a);
imag = sqrt(fabs(disc)) / (2 * a);
printf("\n\n Complex Root1 = %7.4f +i %7.4f",real, imag);
printf("\n Complex Root2 = %7.4f -i %7.4f",real, imag);
}
else
if( disc == 0 )
{ /* Case: real and identical roots */
printf("\n\n Roots are Real & Identical...");

root1 = -b / (2 * a);
root2 = root1;
printf("\n\n Root1 = %7.4f",root1);
printf("\n Root2 = %7.4f",root2);
}
else
{
/* Case: real and distinct roots */
printf("\n\n Roots are Real & Distinct...");
root1 = (-b + sqrt(disc)) / (2 * a);
root2 = (-b - sqrt(disc)) / (2 * a);
printf("\n\n Root1 = %7.4f",root1);
printf("\n Root2 = %7.4f",root2);
}
}
getch();
}

4. Write and execute a C program to find grade of a student based on marks range using elseif ladder
Marks
Grade
90-100
S
75 - 90
A
60-74
B
50-59
C
40-49
D
0-39
F
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
long int m1=0, m2=0, m3=0, m4=0, m5=0, m6=0;
long int perce=0;
clrscr();
printf("Enter the marks for 6 subjects");
scanf("%ld%ld%ld%ld%ld%ld",&m1,&m2,&m3,&m4,&m5,&m6);
perce = ((m1+m2+m3+m4+m5+m6)*100/600);
if( (m1<40) || (m2<40) || (m3<40) || (m4<40) || (m5<40) || (m6<40))
{
printf("Fail");
getch();
exit(0);

}
printf("\nPercentage : %ld\n",perce);
if(perce >= 90)
printf("\n S grade");
else if(perce >= 80)
printf("\n A grade");
else if(perce >= 70)
printf("\n B grade");
else if(perce >= 60)
printf("\n C grade");
else if(perce >= 50)
printf("\n D grade");
else
printf("\n Fail");
getch();
}
5. Write and execute a C program to perform a desired arithmetic operation using switch
statement, by declaring choice as char data type. For division operation check whether
the divisor is zero, if zero print Divide by Zero error.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
float a=0, b=0, result=0;
char opcode;
clrscr();
printf("Enter 2 Numbers");
scanf("%f%f",&a,&b);
fflush(stdin);
printf("\nEnter + to add\n - to subtract\n * to multiply\n / to divide");
scanf("%c",&opcode);
switch(opcode)
{
case '+':
result = a+b;
printf("\nSum of %f & %f is : %f", a,b,result);
break;
case '-':
result = a-b;
printf("\nSubtraction of %f & %f is : %f", a,b,result);
break;
case '*':
result = a*b;
printf("\nMultiplication of %f & %f is : %f", a,b,result);

break;
case '/':
if(b==0)
{
printf("Divide By Zero Error\n");
exit(0);
}
else{
result = a/b;
printf("\nDivision of %f & %f is : %f", a,b,result);
}
break;
default: printf("\nInvalid Operator");
}
getch();
}
6. Write and execute a C program to find area of a triangle, square, circle and rectangle
using switch statement.
#include<stdio.h>
#include<conio.h>
#define PI 3.147
void main()
{
float radius = 0, length = 0, breadth = 0;
float base = 0, height = 0, area = 0;
int choice = 0;
clrscr();
printf("Enter \n1 to find area of triangle\n2 to find area of Square");
printf("\n3 to find area of circle\n4 to find area of rectangle\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter base & height of a triangle");
scanf("%f%f",&base,&height);
area = (1.0/2)*base*height;
printf("\nArea of Triangle : %f", area);
break;
case 2:
printf("\nEnter length of a Square");
scanf("%f",&length);
area = length * length;
printf("\nArea of Square : %f", area);
break;
case 3:

printf("\nEnter the radius of a Circle");


scanf("%f",&radius);
area = PI * radius * radius;
printf("\nArea of Circle : %f", area);
break;
case 4:
printf("\nEnter the length & breadth of a Rectangle");
scanf("%f%f",&length,&breadth);
area = length * breadth;
printf("\nArea of Rectangle : %f", area);
break;
default:
printf("\nInvalid Choice");
}
getch();
}
7. Write a C program to find the sum of first n natural numbers using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0,n;
scanf(%d,&n);
for(i=0;i<n;i++)
sum+=i;
printf( the sum of 1st n natural nos is %d, sum);
}
8. Write a C program to check whether a given number is palindrome or not using while
loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int rem=0,temp=0;
int num1=0, num2=0;
clrscr();
printf("Enter 4-Digit Number\n");
scanf("%d",&num1);
temp = num1;
while(temp)

{
rem = temp % 10;
num2 = (num2 * 10) + rem;
temp = temp / 10;
}
if(num1 == num2)
printf("\n%d is a Palindrome Number",num1);
else
printf("\n%d is not a Palindrome Number",num1);
getch();
}
9. Write a C program to add two matrices.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10],c[10][10];
int i=0, j=0;
int rows=0, cols=0;
int rows1=0, cols1=0;
clrscr();
printf("Enter the number of rows & columns for the first matrix\n");
scanf("%d%d", &rows,&cols);
printf("\n Enter the elements for the First Matrix:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the number of rows & columns for the second matrix\n");
scanf("%d%d",&rows1,&cols1);
printf("\n Enter the elements for the Second Matrix:\n");
for(i=0;i<rows1;i++)
{
for(j=0;j<cols1;j++)
{
scanf("%d",&b[i][j]);
}
}
if(rows != rows1 || cols != cols1)
{
printf("\n Addition can not be performed\n");

exit(0);
}
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf(\n The first matrix:);
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf(\n The second matrix:);
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n Resultant Matrix is :\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
10.Write a C program to find factorial of a given number using functions with arguments,
with return type.
#include<stdio.h>
#include<conio.h>
int fact(int num);
void main( )

{
int n,factorial;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
if(n==0)
{
printf("Factorial of %d is : 1",n);
}
else
{
factorial=fact(n);
printf("Factorial of %d is : %d",n,factorial);
}
getch();
}
int fact(int num)
{
int i,f=1;
for(i=1;i<=num;i++)
f=f*i;
return(f);
}

PART B
1. Write and execute a C program to print prime numbers within given range using for
or while or do while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
int m=0, n=0;
clrscr();
printf("\nEnter the range :(m<n)\n");
scanf("%d%d", &m,&n);
printf("\nPrime Numbers between %d & %d :\n",m,n);
while(m<=n)
{
for(i=2;i<=m;i++)
{
if(m%i==0)
break;

}
if(i==m)
printf("%d\t",m);
m++;
}
getch();
}
2. Write and execute a C program to search for an element in an array using binary search
technique.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20];
int i=0, size=0;
int key=0, flag=0;
int high=0, low=0, mid=0;
clrscr();
printf("Enter the size of the array");
scanf("%d",&size);
printf("\nEnter %d elements into Array in ascending order",size);
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("\nArray Elements are :\n");
for(i=0;i<size;i++)
printf("%d\t",arr[i]);
printf("\nEnter the element to search in the array\n");
scanf("%d", &key);
high = size - 1;
low = 0;
while(low<=high)
{
mid = (high + low)/2;
if(key > arr[mid])
{
low = mid + 1;
}
if(key < arr[mid])
{
high = mid - 1;
}
if(key == arr[mid])
{
flag = 1;

break;
}
}
if(flag == 1)
printf("\n%d is found at position %d",key,mid+1);
else
printf("\nKey not found");
getch();
}
3. Write and execute a C program to sort elements in ascending order using bubble
Sort technique.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,temp,j,arr[10];
clrscr();
printf("\n Enter the number of elements in the array");
scanf("%d",&n);
printf("\n Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n the array sorted in ascending order is:\n");
for(i=0;i<n;i++)
printf("\t %d ",arr[i]);
getch();
return 0;
}

4. Write and execute a C program to find whether a given square matrix is symmetric or
not.
#include <stdio.h>
void main()
{
int a[10][10], i, j, row, col,flag=0;
printf("\nEnter no. of rows & columns: ");
scanf("%d %d", &row, &col);
printf("\nEnter elements of Matrix:\n");
for (i=0; i<row; i++)
for (j=0; j<col; j++)
scanf("%d", &a[i][j]);
printf("\n\nElements of Matrix:\n\n");
for (i=0; i<row; i++)
{
for (j=0; j<col; j++)
printf("\t%d", a[i][j]);
printf("\n\n");
}
for (i=0; i<col; i++)
{
for (j=0; j<row; j++)
if(a[j][i]!=a[i][j])
flag=1;
}
if(flag==1)
printf( not symmetric);
else
printf(symmetric);
}
5. Write and execute a C program to combine two strings without using built-in
functions.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[10];
int i=0,len=0,j;
clrscr() ;
printf("Enter the string1\n");
gets(str1);

printf("Enter the string2\n");


gets(str2);
while(str1[len]!=\0)
len++;
for(j=0,i=len;str2[j]!='\0';j++,i++){
str1[i]=str2[j];
}
str1[i]='\0';
printf("concatenated string is ");
puts(str1);
getch();
}
6. Write and execute a C program to compare two strings without using built-in functions.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[10];
int i,len=0,j,flag=0;
clrscr() ;
printf("Enter the string1\n");
gets(str1);
printf("Enter the string2\n");
gets(str2);
while(str1[len]!=\0)
len++;
for(i=0;i<len;i++)
{
if(str1[i]!=str2[i])
{ flag=1; break; }
}
if(flag==0)
printf("strings are equal");
else
printf(strings are not equal);
getch();
}
7. Write and execute a C program to find GCD and LCM of two numbers using functions
with arguments, without return type.
#include<stdio.h>
#include<conio.h>

void gcd_lcm(int m, int n);


void main( )
{
int x,y;
clrscr( );
printf("Enter two numbers\n");
scanf("%d%d",&x,&y);
gcd_lcm(x,y) ;
getch() ;
}
void gcd_lcm(int m, int n)
{
int p,q,gcd,lcm,r ;
p=m;
q=n;
while(n!=0)
{
r=m%n;
m=n;
n=r;
}
gcd=m;
lcm=p*q/gcd;
printf("GCD of %d and %d is : %d\n",p,q,gcd);
printf("LCM of %d and %d is : %d\n",p,q,lcm);
}
8. Write a C program to illustrate compare and copy structure variables.
#include<stdio.h>
#include<conio.h>
struct class
{
int number;
char name[20];
float marks;
};
void main()
{
int x;
struct class std1 ={11, "PQR", 88.5};
struct class std2 ={12, "STU", 78.5};
struct class std3;
clrscr();

std3 = std2;
x = ((std3.number == std2.number) && (std3.marks == std2.marks)) ? 1:0 ;
if(x==1)
{
printf("\n Student2 & student3 are same\n");
printf("%d\t%s\t%f\n", std3.number, std3.name, std3.marks);
}
else
printf("Student2 & student3 are different");
printf("\nStudent1 Details\n");
printf("%d\t%s\t%f\n", std1.number, std1.name, std1.marks);
getch();
}
9. Write a C program to swap two numbers using pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int *p, *q;
int a=0, b=0;
int temp;
clrscr();
printf("\nEnter two numbers");
scanf("%d%d",&a,&b);
p = &a;
q = &b;
printf("\nBefore Swapping\n");
printf("a : %d\nb : %d\n",a,b);
temp = *p;
*p = *q;
*q = temp;
printf("\nAfter Swapping\n");
printf("a : %d\nb : %d\n",a,b);
getch();
}
10. Write a C program to compute sum of all elements stored in an array using pointers
#include<stdio.h>
#include<conio.h>
void main()

{
int *p;
int arr[20],sum=0;
int i=0,n;
clrscr();
printf("\nEnter the size of the array");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
p = arr;
for(i=0;i<n;i++)
{
sum = sum + (*p);
p++;
}
printf("\nSum : %d",sum);
getch();
}

Note: One program from Part A (17 marks) and One program from Part B
(18 marks) should be executed.
Write-up: 8M
Viva: 7M
Execution: 35M
Total: (8+7+35=50M)