First Program

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr;
cout<<“this is my first program”;
getch();
}

Deletion in an Array

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int n,num,list[100],loc;
cout<<“enter size”;
cin>>n;
cout<<” enter it”;
for(int a=0;a<n;a++)
{
cin>>list[a];
}
cout<<“enter loc nd num”;
cin>>loc>>num;
int beg=0,last=n-1;
while(last>=loc)
{
list[last+1]=list[last];
last–;
}
list[last+1]=num;
cout<<“list”;
for(int b=0;b<=n;b++)
{
cout<<list[b];
}
getch();
}

LCM of 2 numbers

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,large,small,d;
int flag,h;
cout<<“enter two numbers”;
cin>>a>>b;
if(a>b)
{
large=a;
small=b;
}
if(b>a)
{
small=a;
large=b;
}
for(int i=1;i<=small;i++)
{
h=large*i;
d=h%small;
if(d==0)
{
flag=1;
break;
}
}
if(flag==1)
{
cout<<“lcm is”<<h;
}

getch();
}

v

Star Pattern

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<” enter the value”;
cin>>n;
int m=10;
int s=n-1;
for(int i=1;i<=n;i++)
{
for(int j=2*n-3;j>=s;j–)
{
cout<<” “;
}
for(int p=10;p>=m;p=p-1)
{
cout<<“*”;
}
cout<<“\n”;
m=m-2;
s++;
}
int y=n-1;
int v=2*n-2;
for(int q=n;q>=1;q–)
{
for(int g=n-1;g>=y;g–)
{
cout<<” “;
}
for(int h=1;h<v;h=h+1)
{
cout<<“*”;
}
cout<<“\n”;
y–;
v=v-2;
}
getch();
}

Fibonacci Series

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=0,b=1,c,n;
cout<<“enter the limit”;
cin>>n;
cout<<a<<“\n”<<b;
for(int i=2;i<=n;i++)
{
c=a+b;
cout<<“\n”<<c;
a=b;
b=c;
}
getch();
}

Deletion-Linked List

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct stud
{
int roll;
char name[30];
stud *next;
};
void main()
{
clrscr();
int n;
cout<<“enter n”;
cin>>n;
stud *first,*last,*x;
first =new stud;
cout<<“enter the first roll”;
cin>>first->roll;
cout<<“name”;
gets(first->name);
fflush(stdin);
first->next=NULL;

last = first;
for(int a=1;a<n;a++)
{
x=new stud;
cout<<“enter roll”;
cin>>x->roll;
cout<<“name”;
gets(x->name);
fflush(stdin);
x->next=NULL;
last->next=x;
last=x;
}
cout<<“elements are”;
last=first;
while(last!=NULL)
{
cout<<last->roll;
puts(last->name);
fflush(stdout);
cout<<“\n”;
last =last->next;
}
int d;
cout<<“roll no 2 b deleted”;
cin>>d;
int found=0;
stud* ptr;
if(first->roll==d)
{
ptr=first;
first=first->next;
delete ptr;
}
ptr=first->next;
last=first;
while(ptr!=NULL)
{
if(ptr->roll==d)
{
last->next=ptr->next;
delete ptr;
found=1;
break;
}
else{
ptr=ptr->next;
last=ptr;
}
if( found==0)
{
cout<<“deletion not possible”;
}
}
cout<<“elements are”;
last=first;
while(last!=NULL)
{
cout<<last->roll;
puts(last->name);
fflush(stdout);
cout<<“\n”;
last =last->next;
}
delete first;
getch();
}

Pointers and structures

\\ pointers and structures
#include<iostream.h>
#include<stdio.h>
struct student
{
char name[30];
int age;
int roll;
}s;
void main()
{student *ptr;
ptr =&s;
cout<<“enter name,age,roll number”;
gets(s.name); \\reading through dot
cin>>s.age>>s.roll;
cout<<“details are”;
puts(s->name);\\printing through arrow
cout<<s->age<<s->roll;
}

Strings comparison

\\string comparison using pointers
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char a[20],b[20];
char *p1,*p2;
p1=a;
p2=b;
int flag=0;
cout<<“enter two strings”;
gets(b);
gets(a);
while((*p1!=”)||(*p21=”))
{
if(*p1==*p2)
{
p1++;
p2++;
}
else
{
flag=1;
break;
}
}
if(flag==0)
{
cout<<“the two strings are equal”;
}
getch();
}

 

Usage of This pointer

#include<iostream.h>
#include<conio.h>
class emp
{
char name[30];
float salary;
int age;
public:
void reveal()
{
cout<<“\n my object’s address is \t”<<this;
}
};
void main()
{
clrscr();
emp e1,e2,e3;
e1.reveal();
e2.reveal();
e3.reveal();
getch();
}

Merit List using structures and selection sort

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct student
{
int marks;
char name[30];
}s1[100],s2,s3[100];
student sel(student s1[],int n,int b);
void main()
{
clrscr();
int n;
cout<<“enter n”;
cin>>n;
cout<<“enter”;
for(int a=0;a<n;a++)
{
cin>>s1[a].marks;
cin>>s1[a].name;
fflush(stdin);

}
cout<<“list is”;
cout<<“\n”;
for(int b=0;b<n;b++)
{
s3[b]=sel(s1,n,b);
cout<<s3[b].marks;
puts(s3[b].name);
}
getch();
}
student sel(student s1[],int n,int b)
{
int pos,small,temp;
for(int i=0;i<n-1;i++)
{

small=s1[i].marks;
pos=i;
for(int j=i+1;j<n;j++)
{
if(small<s1[j].marks)
{
small=s1[j].marks;
pos=j;
}
}
s2=s1[pos];
s1[pos]=s1[i];
s1[i]=s2;
}
int m;
m=b;

return s1[m];

}

 

Calendar

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

Archives