Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Subject: Computational Programming LAB (20CS12L/20CS22L) : List of Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

SUBJECT: COMPUTATIONAL PROGRAMMING

LAB(20CS12L/20CS22L)
LIST OF PROGRAMS
LAB CYCLE-I

1. Accept two numbers and perform basic arithmetic operations


(+,-,*,/,%)
#include<stdio.h>
int main()
{
float a,b,add,dif,product,quotient;
int rem;
printf("enter two numbers\n");
scanf("%f%f",&a,&b);
add=a+b;
dif=a-b;
product=a*b;
quotient=a/b;
rem=(int)a%(int)b;
printf("sum of two numbers=%f\n",add);
printf("diffrence between two numbers=%f\n",dif);
printf("product of two numbers=%f\n",product);
printf("quotient of two numbers=%f\n",quotient);
printf("the remainder of two numbers=%d",rem);
return 0;
}

2. Program to find area/volume of geometrical shapes(Circle,Triangle-given three


sides,given base and height,….)
a).to find area of circle and area of triangle.
#include<stdio.h>
#include<math.h>
int main()
{
float radius,base,height,area_of_circle,area_of_triangle;
printf("enter radius,base,height\n");
scanf("%f%f%f",&radius,&base,&height);
area_of_circle=3.14*radius*radius;
area_of_triangle=0.5*base*height;
printf("area_of_circle=%f\n",area_of_circle);
printf("area_of_triangle=%f",area_of_triangle);
return 0;
}

b) To find the area of triangle if three sides are given.


#include<stdio.h>
#include<math.h>
int main()
{
float s1,s2,s3,s,area_of_triangle;
printf("enter three sides\n");
scanf("%f%f%f",&s1,&s2,&s3);
s=(s1+s2+s3)/2;
area_of_triangle=sqrt(s*(s-s1)*(s-s2)*(s-s3));
printf("area of traingle=%f",area_of_triangle);
return 0;
}
3. Given the values of x,y and z,write a program to rotate their values such that x
has the value of y, y has the value of z, and z has the value of x.
#include<stdio.h>
int main( )
{
int x,y,z,temp;
printf("enter three numbers\n");
scanf("%d%d%d",&x,&y,&z);
printf("before interchange the value of x=%d\t y=%d\t z=%d\n",x,y,z);
temp=x;
x=y;
y=z;
z=temp;
printf("after interchange the value of x=%d\t y=%d\t z=%d\n",x,y,z);
return 0;
}

4. Write a program that reads floating-point number and then displays the right-
most digit of the integral part of the number.
#include<stdio.h>
int main()
{
float x;
int y,z;
printf("enter the floating point number\n");
scanf("%f",&x);
y=(int)x;
z=y%10;
printf("the right most digit of integral part=%d",z);
return 0;
}

5. Write a program that reads floating-point number, separate and displays the
integral and decimal part of the given.
#include<stdio.h>
int main()
{
float x,z;
int y;
printf("enter the floating point number\n");
scanf("%f",&x);
y=(int)x;
z=x-y;
printf("the integral part of the number=%d",y);
printf("the decimal part of the number=%f",z);
return 0;
}

LAB CYCLE-II
1. Program to perform the following using ternary operator
a. I)Check if given number is positive or negative.
#include<stdio.h>
int main()
{
float n;
printf("enter the number\n");
scanf("%f",&n);
(n>0)?printf("%f is positive number",n):printf("%f is negative number",n);
return 0;
}

ii)Check if the given number is even/odd.


#include<stdio.h>
int main()
{
int n;
printf("enter the number\n");
scanf("%d",&n);
(n%2==0)?printf(
"%d is even number",n):printf("%d is odd number",n);
return 0;
}
b) Find the largest/smallest of two /three numbers
i)
#include<stdio.h>
int main()
{
int a,b;
printf("enter two numbers\n");
scanf("%d%d",&a,&b);
(a>b)?printf("%d\t is greater\n",a):printf("%d is greater\n",b);
(a<b)?printf("%d is smaller\n",a):printf("%d is smaller\n",b);
}
ii)
#include<stdio.h>
int main()
{
int a,b,c,big;
printf("enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
(a>b)?(a>c)?printf("%d is greater",a):printf("%d is greater",c):(b>c)?printf("%d is
greater",b):printf("%d is greater",c);
}
2. Write a program to check if given number is even or odd using bitwise ‘&’ operator.

#include<stdio.h>
int main()
{
int n;
printf("enter the number\n");
scanf("%d",&n);
(n&1==1)?printf("%d is odd",n):printf("%d is even",n);
}
3. Write a program to perform the following using bitwise operators:
c=a&b; d=a|b; e=~a
f=a>>n; g=a<<n; h=a^b;

#include<stdio.h>
int main()
{
int a,b,c,d,e,f,g,h,n;
printf("enter the value of a and b");
scanf("%d%d",&a,&b);
c=a&b;
printf("the result of a&b is %d\n",c);
d=a|b;
printf("the result of a|b is %d\n",d);
e=~a;
printf("the result of ~a is %d\n",e);
printf("enter the value of n to move the number of bits to left or right");
scanf("%d",&n);
f=a>>n;
printf("the result of leftshift is %d\n",f);
g=a<<n;
printf("the result of rightshift is %d\n",g);
h=a^b;
printf("the result of xor(a^b) is %d\n",h);
}

4. Write a program to illustrate the use of postfix/prefix increment/decrement operators.

#include<stdio.h>
int main()
{
int a,b,c,d,e;
printf("enter the value of a");
scanf("%d",&a);
b=++a;
printf("the value of a after preincrement is %d\n",a);
printf("the value of b=%d\n",b);
c=a++;
printf("the value of a after postincrement is %d\n",a);
printf("the value of c=%d\n",c);
d=--a;
printf("the value of a after predecremet is %d\n",a);
printf("the value of d=%d\n",d);
e=a--;
printf("the value of a after postdecrement is %d\n",a);
printf("the value of e=%d\n",e);
}

5. Write a program to print the size of various datatypes in c using ‘sizeof’ operator.

include <stdio.h>
int main() {
int a = 16;
printf("Size of variable a : %d\n",sizeof(a));
printf("Size of int data type : %d\n",sizeof(int));
printf("Size of char data type : %d\n",sizeof(char));
printf("Size of float data type : %d\n",sizeof(float));
printf("Size of double data type : %d\n",sizeof(double));
return 0;
}

LAB CYCLE III


‘IF AND SWITCH STATEMENTS
1. Write a program to determine whether a given number is odd or even and print the
message number is even or number is odd with and without using else option

With using else option


#include<stdio.h>
int main()
{
int n;
printf("enter the number\n");
scanf("%d",&n);
if(n%2==0)
printf("number is even");
else
printf("number is odd");
}

ii. without using else option


#include<stdio.h>
int main()
{
int n;
printf("enter the number\n");
scanf("%d",&n);
if(((n%2==0)&&
printf("number is even"))||
printf("number is odd"));
}
2. Design, develop and execute a program to find and output all the roots of a given
quadratic equation, for non-zero coefficients.
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c,d;
double root1,root2;
printf("enter the valule of a b and c\n");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
if (d < 0)
{
printf("First root = %.2lf + i%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
printf("Second root = %.2lf - i%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
}
else
{
root1 = (-b+sqrt(d))/(2*a);
root2 = (-b-sqrt(d))/(2*a);
printf("First root = %.2lf\n", root1);
printf("Second root = %.2lf\n", root2);
}
return 0;
}

3. Genetrate electricity bill depending on the units consumed and varying rates for units
consumed.
Consumption Units Rate of Charge
0-200 Rs.0.50 per unit
201-400 Rs.100plus Rs.0.65 per unit excess of 200
401-600 Rs.230 plus Rs.0.80 per unit excess of 400
601 and above Rs.390 plus Rs.1.00 per unit excess of 600

#include<stdio.h>
int main()
{
int units,custnum;
float charges;
printf("enter customer no and units consumed\n");
scanf("%d%d",&custnum,&units);
if(units<=200)
charges=0.5*units;
else if(units<=400)
charges=100+0.65*(units-200);
else if(units<=600)
charges=230+0.8*(units-400);
else
charges=390+(units-600);
printf("\n customer no:%d:charges=%.2f\n",custnum,charges);
}
4. Write a program to input month number and display its respective month in words.

#include <stdio.h>
#include<stdlib.h>
void main()
{
int monno;
printf("Input Month No : ");
scanf("%d",&monno);
switch(monno)
{
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("invalid Month number. \nPlease try again ....\n");
break;
}
}
5. Write a program to simulate simple calculator.

#include<stdio.h>
int main()
{
int num1,num2,opt;
printf("Enter the first Integer:\n");
scanf("%d",&num1);
printf("Enter the second Integer:\n");
scanf("%d",&num2);
printf("\n\n\n\nEnter the your option:\n");
printf("1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\n");
scanf("%d",&opt);
switch(opt)
{
case 1:printf("\nAddition of %d and %d is: %d",num1,num2,num1+num2);
break;
case 2:printf("\nSubstraction of %d and %d is: %d",num1,num2,num1-num2);
break;
case3:printf("\nMultiplication of %d and %d is: %d",num1,num2,num1*num2);
break;
case 4:
if(num2==0)
{
printf("OOps Devide by zero\n");
}
else
{
printf("\n Division of %d and %d is: %d",num1,num2,num1/num2);
}
break;
case 5: return 0;
break;
default:printf("\n Enter correct option\n");
break;
}
}

LAB CYCLE –IV


LOOP STATEMENTS
1. Write a program to sum natural,odd and even numbers upto ‘n’

#include<stdio.h>
void main()
{
int n,sum=0,even_sum=0,odd_sum=0;
printf("enter the number\n");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
sum=sum+i;
if(i%2==0)
even_sum=even_sum+i;
else
odd_sum=odd_sum+i;
}
printf("the sum of all natural numbers upto %d is %d\n",n,sum);
printf("the sum of all even numbers upto %d is %d\n",n,even_sum);
printf("the sum of all odd_numbers upto %d is %d\n",n,odd_sum);
}

2. Write a program to generate and print first ‘n’ fibobacci numbers


#iclude<stdio.h>
void main()
{
int n,first,second,third,term;
printf("enter the limit\n");
scanf("%d",&n);
first=0;
second=1;
printf("the fibonacci series are\n");
printf("%d\t%d\t",first,second);
third=first+second;
term=2;
while(term<n)
{
printf("%d\t",third);
first=second;
second=third;
third=first+second;
term++;
}
}
3. Write s program to find the sum of digits of a number reducing into single digit.

#include<stdio.h>
void main()
{
int n,sum,rem;
printf("enter the number\n");
scanf("%d",&n);

while((n/10)!=0)
{
sum=0;
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
n=sum;
}
printf("the sum of digits=%d",sum);
}

4. Write a program to reverse a given four digit integer number and check whether it is a
palindrome or not. output the given number with suitable message.

#include<stdio.h>
void main()
{
int n,rem,revno,orno;
printf("enter the number\n");
scanf("%d",&n);
orno=n;
revno=0;
while(n!=0)
{
rem=n%10;
revno=(revno*10)+rem;
n=n/10;
}
if(orno==revno)
printf("%d is a palindrome number",orno);
else
printf("%d is not a palindrome number",orno);
}
5. Write a program to determine whether a given number is prime or not.

#include<stdio.h>
void main()
{
int n,count=2,flag=1;
printf("enter the number\n");
scanf("%d",&n);
while(count<n)
{
if(n%count==0)
{
flag=0;
break;
}
count++;
}
if(flag==1)
printf("%d is prime number",n);
else
printf("%d is not a prime number",n);
}

6. Write a program to generate and print all the prime numbers between given range.

#include <stdio.h>
int main()
{
int a,b,i,flag;
printf("Enter start value:");
scanf("%d",&a);
printf("Enter end value : ");
scanf("%d",&b);
printf("Prime Numbers between %d and %d :", a,b);
while (a<b)
{
flag=0;
for(i=2; i<=a/2; ++i)
{
if(a%i==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d\t",a);
++a;
}
return 0;
}

Write a programto find the factorial of agiven number


#include<stdio.h>
int main()
{
int n,fact=1,i;
printf(“enter the number”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
fact=fact*i;
printf(“the factorial of %d is %d”,n,fact);
}
LAB CYCLE -V
1. Write a program to perform read and write operation using one dimensional array.
#include<stdio.h>
int main()
{
int n a[10],i;
printf(“enter the number of elelments”);
scanf(“%d”,&n);
printf(“enter the elements”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“the array elements are”);
for i=0;i<n;i++)
printf(%d\t”,a[i]);
}

2. Write a c programto input N numbers and perform Linear search for a given key
number

#include<stdio.h>
int main()
{
int n,i,a[10],key;
printf(“enter the number of elements”);
scanf(“%d”,&n);
printf(“enter the elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“enter the key elment to search”);
scanf(“%d”,&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf(“the %d found at the position %d”,key,i+1);
}
}
if(i>n)
printf(“key element not found”);
}

3. Design, Develop and execute a program in C to input N integer numbers into a


single dimension array,sort them in ascending order using BUBBLE SORT
technique, and then to print both the given array and the sorted array with suitable
headings.

#include<stdio.h>
int main()
{
int a[10],n,i,j,temp;
printf(“enter the number of elements”);
scanf(“%d”,&n);
printf(“enter the elments”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf(“the sorted elments are”);
for(i=0;i<n;i++)
printf(“%d\t”,a[i]);
}

4. Design, Develop and execute a program in C to input N integer numbers in


ascending order into a single dimension array, and then to perform a BINARY
SEARCH for a given key integer number and report success or failure in the form
of a suitable message.
#include<stdio.h>
int main()
{
int n,a[10],key,beg,end,mid;
printf(“enter the number of elements”);
scanf(“%d”,&n);
printf(“enter the elements”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“enter the key element to search”);
scanf(“%d”,&key);
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(key==a[mid])
{
printf(“%d found at the position %d”,key,mid+1);
break;
}
else if(key>a[mid])
beg=mid+1;
else
end=mid-1;
}
if(beg>end)
printf(“%d is not found”,key); }

LAB CYCLE VI
1. Write a C program to read two matrices A(M x N) and b(M x N) and perfor
addition OR subtraction of A and B .Output the given matrices,their sum OR
differences.
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],m,n,i,j;
printf(“enter the order of the matrices”);
scanf(“%d%d”,&m,&n);
printf(“enter the elments of first matrices”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“enter the elments of second matrices”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf(“addition of two matrices is”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d”,c[i][j]);
}
}
}
b) subtraction of two matrices is:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],m,n,i,j;
printf(“enter the order of the matrices”);
scanf(“%d%d”,&m,&n);
printf(“enter the elments of first matrices”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“enter the elments of second matrices”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
printf(“addition of two matrices is”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d”,c[i][j]);
}
}
}
2. Write a C program to read a matrix A(M x N), find the TRANSPOSE of the
given matrix and output both the input matrix and transposed matrix.
#include<stdio.h>
Int main()
{
Int a[10][10],b[10][10],m,n,I,j;
Printf(“enter the order of the matrices”);
Scanf(“%d%d”,&m,&n);
Printf(“enter the elments of first matrices”);
For(i=0;i<m;i++)
{
For(j=0;j<n;j++)
{
Scanf(“%d”,&a[i][j]);
}
}
For(i=0;i<m;i++)
{
For(j=0;j<n;j++)
{
B[j][i]=a[i][j];
}
}
Printf(“the input matrix is “);
For(i=0;i<m;i++)
{
For(j=0;j<n;j++)
{
Printf(“%d”,a[i][j]);
}
}
Printf(“the transpose matrix is “);
For(i=0;i<n;i++)
{
For(j=0;j<m;j++)
{
Printf(“%d”,b[i][j]);
}
}
}

You might also like