Subject: Computational Programming LAB (20CS12L/20CS22L) : List of Programs
Subject: Computational Programming LAB (20CS12L/20CS22L) : List of Programs
Subject: Computational Programming LAB (20CS12L/20CS22L) : List of Programs
LAB(20CS12L/20CS22L)
LIST OF PROGRAMS
LAB CYCLE-I
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;
}
#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);
}
#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;
}
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;
}
}
#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);
}
#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;
}
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”);
}
#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]);
}
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]);
}
}
}