CHANDU SIR C&CPP LAB PROGRAMS CMDC
1. Write a c program to display Fibonacci series without recursion
#include<stdio.h>
int main( )
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);
for (i=2;i<number;++i)
{
n3=n1+n2;
printf (" %d", n3);
n1=n2;
n2=n3;
}
return 0;
}
OUTPUT:
Enter the Number of Elements: 5
011235
EEEEEEEEEENTEREE222
2. Write a C program To Check Whether The Given Number is
Prime Or Not
#include<stdio.h>
int main( )
{
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 1
CHANDU SIR C&CPP LAB PROGRAMS CMDC
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
return 0;
}
OUTPUT :
Enter the number to check prime:56
Number is not prime
Enter the number to check prime:23
Number is prime
3. Write a c program check whether number is palindrome or
not.
#include<stdio.h>
int main( )
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 2
CHANDU SIR C&CPP LAB PROGRAMS CMDC
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}
OUTPUT
Enter the number=121
Palindrome number
Enter the number=123
Not Palindrome
4. Write a C Program to Check Armstrong Number or not
#include<stdio.h>
int main( )
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 3
CHANDU SIR C&CPP LAB PROGRAMS CMDC
printf("not armstrong number");
return 0;
}
OUTPUT
Enter the number=371
Armstrong Number
Enter the number=123
Not Armstrong Number
5. Write a C Program Sum Of Digits Program In C.
#include<stdio.h>
int main()
{
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf ("Sum is=%d",sum);
return 0;
}
Output:
Enter a Number : 123
Sum is=6
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 4
CHANDU SIR C&CPP LAB PROGRAMS CMDC
6. Write a program to display reverse a given number .
#include<stdio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
return 0;
}
Output :
Enter a number: 123
Reversed Number: 321
7. Write a c program matrix multification?
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 5
CHANDU SIR C&CPP LAB PROGRAMS CMDC
#include<stdlib.h>
int main(){ for(k=0;k<c;k++)
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; {
system("cls"); mul[i][j]+=a[i][k]*b[k][j];
printf("enter the number of row="); }
scanf("%d",&r); }
printf("enter the number of column="); }
scanf("%d",&c); //for printing result
printf("enter the first matrix element=\n"); for(i=0;i<r;i++)
for(i=0;i<r;i++) {
{ for(j=0;j<c;j++)
for(j=0;j<c;j++) {
{ printf("%d\t",mul[i][j]);
scanf("%d",&a[i][j]); }
} printf("\n");
} }
printf("enter the second matrix element=\n"); return 0;
for(i=0;i<r;i++) }
{
for(j=0;j<c;j++) OUTPUT:
{
enter the number of row=3
scanf("%d",&b[i][j]);
enter the number of column=3
} enter the first matrix element=
} 111
222
333
printf("multiply of the matrix=\n"); enter the second matrix element=
111
for(i=0;i<r;i++)
222
{ 333
for(j=0;j<c;j++)
multiply of the matrix=
6 6 6
{ 12 12 12
mul[i][j]=0; 18 18 18
8.write a c program on Strings
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 6
CHANDU SIR C&CPP LAB PROGRAMS CMDC
#include <stdio.h>
#include <string.h>
int main()
{
/* String Declaration*/
char name[20];
char str1[20] = "city mahila degree college";
printf("Length of string str1: %d", strlen(str1));
printf("Enter your name:");
scanf("%s", name);
/*Displaying String*/
printf("%s",name);
return 0;
}
Output :
Length of string str1: 26
Enter your name : chandu sir
chandu sir
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 7
CHANDU SIR C&CPP LAB PROGRAMS CMDC
9. Wap To Find Biggest Among N Given Numbers Into An Array By Defining
Udfs
#include<stdio.h>
read_array(int m[ ],int n)
{
Int i;
for(i=0;i<=n-1;i++)
scanf("%d",&m[i]);
}
main()
{
int x[50],n;
clrscr();
printf("Enter how many numbers to be accepted:");
scanf("%d",&n);
printf("Enter%dvalues:\n",n);
read_array(x,n);
printf("Biggest=%d",big_array(x,n));
getch();
}
intbig_array(intm[],intn)
{
Int i,big;
big=m[0];
for(i=1;i<=n-1;i++)
if(m[i]>big)
big=m[i];
return(big);
}
OUTPUT
Enter how many numbers to be accepted: 5
Enter 5 values: 1 2 3 4 5
Biggest=5
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 8
CHANDU SIR C&CPP LAB PROGRAMS CMDC
10.W AP To read and print a single student record using structures
#include<stdio.h>
#include<String.h>
main()
{
Struct Student
{
int rno,s1,s2,s3,total;
char name[20],result[5]; OUTPUT:
float avg;
}; Enter RollNo :101
Struct Student x; Enter Name : RAJU
clrscr(); Enter three sub marks :42 54 21
printf("EnterRollno :");
scanf("%d",&x.rno);
printf("EnterName :"); Rollno: 101
scanf("%s",x.name); Name : RAJU
printf("Enter three sub marks :"); Subject1 marks: 42
scanf("%d%d%d",&x.s1,&x.s2,&x.s3); Subject2marks: 54
x.total=x.s1+x.s2+x.s3; Subject3marks: 90
x.avg=x.total/3.0; Total Marks: 186
if(x.s1>34&&x.s2>34&&x.s3>34) Average Marks: 62.00
strcpy(x.result,"pass"); Result: PASS
else
strcpy(x.result,"fail");
clrscr();
printf("Rollno :%d",x.rno);
printf("\nName :%s",x.name);
printf("\nSubject1marks:%d",x.s1);
printf("\nSubject2marks:%d",x.s2);
printf("\nSubject3marks:%d",x.s3);
printf("\nTotalMarks :%d",x.total);
printf("\nAverageMarks:%.2f",x.avg);
printf("\nResult :%s",x.result);
getch();
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 9
CHANDU SIR C&CPP LAB PROGRAMS CMDC
11.WAP swap two given numbers by defining a UDF Using Call by Value
InCall byValue method,we create dummy variables as formal parameters
With in the function header row and receive the copy of the actual parameters
passed from the calling point ,manipulating which within the function does
not affects the original actual parameters data/value.
#include<stdio.h>
swap(int x,int y)
{
intz;
z=x;
x=y;
y=z;
printf("\nAfter swapping values are%d and %d",x,y);
}
main()
{
inta,b;clrscr();
printf("Enter two values:");
scanf("%d%d",&a,&b);
printf("Before swapping values are%d and %d",a,b);
swap(a,b);
getch();
}
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 10
CHANDU SIR C&CPP LAB PROGRAMS CMDC
12. WAP swap two given numbers by defining a UDF Using Call by
Reference
In Call by Reference method,we create pointer type of variables as formal
Parameters with in the function header row and receives the
reference(address)of
The actual parameters from the calling point,manipulating which with in the
function
Directly affects the original actual parameters data/value.
/*To swap the two given numbers by defining a UDF*/
/*UsingCallbyReferencemethod*/
#include<stdio.h>
swap(int *x , int *y)
{
Int z;
z=*x;
*x=*y;
*y=z;
}
main()
{
inta,b;clrscr();
printf("Enter two values:");
scanf("%d%d",&a,&b);
printf("Before swapping values area=%dandb=%d",a,b);
swap(&a,&b);
printf("\nAfter swapping values area=%d and b=%d",a,b);
getch();
}
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 11
CHANDU SIR C&CPP LAB PROGRAMS CMDC
13 ./* Write a c++ program to display Employee details */
#include <iostream.h>
#include <conio.h>
class Employee
{ int eno;
char name[ ];
long salary;
public :
void getdata()
{
cout<<”enter employee no”<<endl;
cin>>eno;
cout<<”enter employee name”<<endl;
cin>>name;
cout<<”enter employee salary”<<endl;
cin>>salary;
}
void putdata()
{
cout<<” employee no”<< eno <<endl;
cout<<” employee name”<< name <<endl;
cout<<”enter employee salary”<< salary <<endl;
}
};
void main ()
{
clrscr();
Employee e;
e.getdata();
e.putdata();
getch();
}
OUTPUT :
enter employee no 101
enter employee name RAJU
enter employee salary 20,000
employee no 101
employee name RAJU
employee salary 20,000
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 12
CHANDU SIR C&CPP LAB PROGRAMS CMDC
14 ./* Write a c++ program on Single Inheritance */
Single Inheritance int base :: geta(void)
{
A derived class has only one base return a;
class. }
void base :: show(void)
#include<iostream.h> {
#include<conio.h> cout<<"value of a ="<<a<<endl;
#include<iomanip.h> }
class base void derived :: mul(void)
{ {
//private data members c=b*geta();
int a; //not inheritable }
public:
int b; //ready for inheritance void derived :: display(void)
void getab(void); {
int geta(void); cout<<"value of a
void show(void); ="<<geta()<<endl;
}; cout<<"value of b ="<<b<<endl;
cout<<"value of c ="<<c<<endl;
//derived class }
class derived : public base void main()
{ {
int c; derived d; // derived class object
public: clrscr();
void mul(void); d.getab();
void display(void); d.mul();
}; d.show();
d.display();
//function definitions outside class // d.a=30; //not accessible as it is
void base :: getab(void) private
{ d.b=20;
a=5; d.mul();
b=10; d.display();
} }
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 13
CHANDU SIR C&CPP LAB PROGRAMS CMDC
OUTPUT:
B.COM (CA) I YEAR/II SEM C&CPP LAB PROGRAMS Page 14