Basic programs for the begineers 
    Part -2 :-
    Lab Practical programs for BCA Student.



       Questions


          1. Write a Program  which  which  demonstrate menu  driven.
          2.  Write a program  to reverse any number by using for  loop. 
          3. Write a program for the factorial of any positive number.
          4. Write a program for the Fibonacci series
          5. Write a program  to check whether the given number is  Palindrome or not.
          6. Write a program to convert binary to decimal .
          7. Write a program to check whether the given number is prime or not.
         8. Write a program to check whether the given number is armstrong or not.
         9. Write a program to perform  the search operation in the array.
        10. Write a program to implement sorting operation in the array.
        11. Write a program to display even and odd list in the array.
        12. Write a program for the multiplication of two matrix.
               Solution.
  

1. Write a Program  which  which  demonstrate menu driven.
   Solution.           

 The menu driven program is used to create an effictive  program or user friendly.
Here in this program we use multiple operator to perform different operation like multiplication ,addition,subtraction,division these basic operator are use here to perform different operation .
These operators can operate between two operands.
  

                                     //menu driven +,-,*,/  


#include<stdio.h>

#include<conio.h>

//drive code
void main()
{
char ch;
int a,b,c;
clrscr();
// data input
printf("enter your choice("+,-,*,/");
scanf("%c",&ch);
printf("enter two no");
scanf("%d%d",&a,&b);

switch(ch)
{
case '+':
c=a+b;
printf("sum of %d and %d%d",a,b,c);
break;
case '-':
c=a-b;
printf("substraction of %d and %d=   %d",a,b,c);                           
break;
case '*':c=a*b;
printf("product of %d and %d%d",a,b,c);
break;
case '/':c=a/b;
printf("division od %d and %d%d",a,b,c);
break;
default:
printf("Invalid Input");
}
getch();
}


2.  Write a program  to reverse any number by using for loop. 

       Solution. 



                                        //reverse any no by using loop

//header file

#include<stdio.h>
#include<conio.h>
//drive code


void main()
{
int i,num,R=0,Revno=0;
clrscr();
printf("Enter any no=");
scanf("%d",&num);

// for loop start
for(i=num;num>0;i++)
{
R=num%10;
Revno=Revno*10+R;
num=num/10;
}


//end of loop
printf("Reverse no=%d",Revno);
getch();
}






           

  

3. Write a program for the factorial of any positive number.



      Solution.

                                                      //factorial any positive no


//header file
#include<stdio.h>
#include<conio.h>

//drive code
void main()
{
long i,num,fact=1;
clrscr();
printf("Enter any no=");
scanf("%ld",&num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("factorial of %ld=%ld",num,fact);
getch();
}


4. Write a program for the Fibonacci series

      Soluition.

                                                   //Fibonacci series

//header file
#include<stdio.h>
#include<conio.h>
//drive code

void main()
{
long a=0,b=1,c=0,i,n;
clrscr();
printf("How many term you want for
Fibonacci =");
scanf("%ld",&n);
printf("%ld %ld",a,b);
for(i=1;i<=n;i++)
{
c=a+b;
printf("\t%ld",c);
a=b;
b=c;
}
getch();
}




 5. Write a program  to check whether the given number is   
     Palindrome or not.

      Solution. 

       A palindrome  number is the number that is  same  after  reversing the number .              
        For example :121,181,191,202,303,404,505......

                                                         //palindrome

#include<stdio.h>
#include<conio.h>
void main()
{
long i,num,R=0,Rn=0,num1=0;
clrscr();
printf("Enter any positive integer=");
scanf("%ld",&num);
num1=num;
for(i=num;num>0;num=num/10)
{
R=num%10;
Rn=Rn*10+R;
}
if(num1==Rn)
printf("Origanl %ld=same as reverse %ld",num1,Rn);
else
printf("not palindrome");
getch();
}




 6. Write a program to convert binary to decimal .

     Solution.

                                               //convert binary to decimal

#include<stdio.h>
#include<conio.h>
void main()
{
long i,bn=0,dn=0,R=0,base=1,decno;
clrscr();
printf("Enter any binary no(0 or 1)=");
scanf("%ld",&bn);
decno=bn;
for(i=bn;bn>0;bn=bn/10)
{
R=bn%10;
dn=dn+R*base;
base=base*2;
}
printf("decimal equivalent of %ld=%ld",decno,dn);
getch();
}




7. Write a program to check whether the given number is  
     prime or not.

     Solution.  

                                        //check no is prime

#include<stdio.h>
#include<conio.h>
void main()
{
int i,num,count=0;
clrscr();
printf("enter any no=");
scanf("%d",&num);
for(i=2;i<=num;i++)
{
if(num%i==2)
count++;
}
if(count==1)
printf("\n%d is a prime no",num);
else
printf("\n%d is not a prime no");
getch();
}


8. Write a program to check whether the given number is  
    armstrong or not.

  

    Solution.


   Armstrong is the number that is equal to the sum of cubes of its digit.

                      For example :       
   


                                    //check no is armstrong

#include<stdio.h>
#include<conio.h>
void main()
{
int i,num,arm=0,num1=0,r,sum=0;
clrscr();
printf("enter any no=");
scanf("%d",&num);
num1=num;
for(i=num;num>0;num=num/10)
{
r=num%10;
arm=r*r*r;
sum=sum+arm;
}
if(sum=num1)
printf("\n%d is a armstrong no",num);
else
printf("\n%d is not a armstrong no");
getch();
}



 9. Write a program to perform  the search operation in the array.

      Solution.


 #include<stdio.h>
#include<conio.h>
void main()
{
int i,arr[100],n,sno,flag=0;
printf("enter size of an array=");
scanf("%d",&n);
printf("Enter data in array=");
for(i=0;i<n;i++)
{
printf("\n Enter %dth element in array",i);
scanf("%d",&arr[i]);
}
printf("\nDispaly of array");
for(i=0;i<n;i++)
printf("\n%d",arr[i]);
printf("\nEnter no to search=");
scanf("%d",&sno);
for(i=0;i<n-1;i++)
{
if(sno==arr[i])
{
flag=1;
printf("%d is found on%d postion",sno,i);
getch();
exit(0);
}
}
if(flag==0)
printf("\n%d is not found list=",sno);
getch();
}



10.  Write a program to implement sorting operation in the array.

       Solution.


   #include<stdio.h>
    #include<conio.h>
void main()
{
int i,arr[100],n,j,temp=0;
clrscr();
printf("enter size of an array=");
scanf("%d",&n);
printf("Enter data in array=");
for(i=0;i<n;i++)
{
printf("\n Enter %dth element in array",i);
scanf("%d",&arr[i]);
}
printf("\nDispaly of array");
for(i=0;i<n;i++)
printf("\n%d",arr[i]);
for(i=0;i<n;i++)
{
for(j=0;j<(n-i-1);j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n%After sorting display of an arr==");
for(i=0;i<n;i++)
printf("\n%d",arr[i]);
getch();
}

 11. Write a program to display even and odd list in the array.

       Solution.


#include<stdio.h>
#include<conio.h>
void main()
{
int i,arr[100],n,j=0,even[100],odd[100],k=0;
printf("enter size of an array=");
scanf("%d",&n);
printf("Enter data in array=");
for(i=0;i<n;i++)
{
printf("\n Enter %dth element in array",i);
scanf("%d",&arr[i]);
}
printf("\nDispaly of array");
for(i=0;i<n;i++)
printf("\n%d",arr[i]);

for(i=0;i<n;i++)
{
if(arr[i]%2==0)
{
even[j]=arr[i];
j++;
}
else
{
odd[k]=arr[i];
k++;
}
printf("\n%Display of even list\n=");
for(i=0;i<j;i++)
printf("\n%d",even[i]);

printf("\nDisplay of odd list\n");

for(i=0;i<k;i++)
printf("\n%d",odd[i]);

getch();
}
}

12. Write a program for the multiplication of two matrix.

      Solution.

#include<stdio.h>
#include<conio.h>
void main()
{
int r,i,j,a[10][10],b[10][10],c[10][10],k=0,sum=0;
printf("Enter size of row and column");
scanf("%d%d",&r,&c);
printf("Enter data in first matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
}
printf("Enter data in second matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&b[i][j]);
}
printf("Display of first matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d",a[i][j]);
}
printf("\nDisplay of second matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("\t%d",b[i][j]);
}
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sum=0;
}
for(k=0;k<r;k++)
{
sum=a[i][k]*b[k][j];
}
c[i][j]=sum;
}
getch();
}

Post a Comment

Previous Post Next Post