C & C++ record
C & C++ record
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,sum;
clrscr();
a=12;
b=14;
sum=a+b;
printf("\n sum of two integers is: %d",sum);
getch();
return 0;
}
Output:
Program 2
/* Program to print Integer (Entered by the user ). */
#include<stdio.h>
#include<conio.h>
int main()
{
int x;
clrscr();
printf("\n Enter an Integer: ");
scanf("%d",&x);
printf("\n You have entered: %d",x);
getch();
return 0;
}
Output:
Program 3
/* Program to multiply two floating point numbers */
#include<stdio.h>
#include<conio.h>
int main()
{
float x,y;
float mul;
clrscr();
printf("\n enter a number: ");
scanf("%f",&x);
printf("\n enter a number: ");
scanf("%f",&y);
mul=(x*y);
printf("\n Result of Multiplication is: %f",mul);
getch();
return 0;
}
Output:
Program 4
/* Program to find ASCII value of Characters */
#include<stdio.h>
#include<conio.h>
int main()
char letter;
clrscr();
scanf("%c",&letter);
getch();
return 0;
Output:
Program 5
/* Program to Compute Quotient and Remainder */
#include<stdio.h>
#include<conio.h>
int main()
{
int n,d;
int Q,R;
clrscr();
printf("\n Enter the Numberator: ");
scanf("%d",&n);
printf("\n Enter the denominator: ");
scanf("%d",&d);
Q=n/d;
printf("\n Quotient is: %d",Q);
R=n%d;
printf("\n Reminder is: %d",R);
getch();
return 0;
}
Output:
Program 6
/* Program to find the size of int, float, char and double */
#include<stdio.h>
#include<conio.h>
int main()
{
int x;
float f;
char c;
double d;
clrscr();
printf("\n Size of int is: %d",sizeof(x));
printf("\n Size of float is: %d",sizeof(f));
printf("\n Size of char is: %d",sizeof(c));
printf("\n Size of double is: %d",sizeof(d));
getch();
return 0;
}
Output:
Program 7
/* program to swap two numbers using temporary variable */
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
int temp;
clrscr();
printf("\n Enter value for a: ");
scanf("%d",&a);
printf("\n Enter value for b: ");
scanf("%d",&b);
temp=a;
a=b;
b=temp;
printf("\n After Swap \n");
printf("\n a = %d",a);
printf("\n b = %d",b);
getch();
return 0;
}
Output:
Program 8
/* Program to check whether a number is even or odd */
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
printf("\n Enter a number: ");
scanf("%d",&n);
if(n%2==0)
printf("\n Given number is Even ");
else
printf("\n Given number is Odd ");
getch();
return 0;
}
Output:
Program 9
/* Program to check odd or even using the Ternary Operator */
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
clrscr();
printf("\n Enter a number: ");
scanf("%d",&n);
(n%2==0)?printf("\n Number is Even"):printf("\n Number is Odd");
getch();
return 0;
}
Output:
Program 10
/* Program to check whether a character is a Vowel or Consonant */
#include<stdio.h>
#include<conio.h>
int main()
{
char test;
clrscr();
printf("\n Enter a character: ");
scanf("%c",&test);
switch(test)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("\n Character is a Vowel ");
break;
default:
printf("\n Character is a Consonent ");
}
getch();
return 0;
}
Output:
Program 11
/* Program to find Largest Number Among Three numbers */
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
clrscr();
printf("\n Enter 3 numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a>b&&b>c)
printf("\n a is Largest ");
else if(b>a&&b>c)
printf("\n b is Largest ");
else
printf("\n c is Largest ");
getch();
return 0;
}
Output:
Program 12
/* Program to check given year leap year or not */
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
clrscr();
printf("\n Enter a year: ");
scanf("%d",&year);
if(year%4==0)
printf("\n Given year is a leap year ");
else
printf("\n Given year is not a leap year ");
getch();
return 0;
}
Output:
Program 13
/* Program to check whether a character is an Alphabet or not */
#include<stdio.h>
#include<conio.h>
int main()
{
char alp;
clrscr();
printf("\n Enter a character: ");
scanf("%c",&alp);
if((alp>='a'&&alp<='z')||(alp>='A'&&alp<='Z'))
printf("\n It is an Alphabet ");
else
printf("\n It is not an Alphabet ");
getch();
return 0;
}
Output:
Program 14
/* Program to find the sum of first N natural Numbers */
#include<stdio.h>
#include<conio.h>
int main()
{
int n,sum,i;
clrscr();
sum=0;
printf("\n Enter the limit: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("\n Sum of %d natural numbers is: %d",(i-1),sum);
getch();
return 0;
}
Output:
Program 15
/* Program to find factorial of a number */
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i;
int fact=1;
clrscr();
printf("\n Enter a number: ");
scanf("%d",&n);
for(i=n;i>0;i--)
{
fact=fact*i;
}
printf("\n Factorial of a given number is: %d",fact);
getch();
return 0;
}
Output:
Program 16
/* program to Generate Multiplication table of a given number */
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,r;
clrscr();
printf("\n Enter a Number: ");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
r=n*i;
printf("\n %d * %d = %d",n,i,r);
}
getch();
return 0;
}
Output:
Program 17
/* Program to Display Fibonacci sequence up to n numbers */
#include<stdio.h>
#include<conio.h>
int main()
{
int n,a,b,f;
clrscr();
printf("\n Enter the Limit of the sequence: ");
scanf("%d",&n);
a=0;
b=1;
f=a+b;
printf("\n %d",a);
printf("\n %d",b);
while(f<n)
{
printf("\n %d",f);
a=b;
b=f;
f=a+b;
}
getch();
return 0;
}
Output:
Program 18
/* Program to Count Number of Digits in an Integer */
#include<stdio.h>
#include<conio.h>
int main()
{
int n,r,count=0;
clrscr();
printf("\n Enter a number: ");
scanf("%d",&n);
while(n>0)
{
n=n/10;
count++;
}
printf("\n No.of Digits in a given number is: %d",count);
getch();
return 0;
}
Output:
Program 19
/* Program to Reverse a Number */
#include<stdio.h>
#include<conio.h>
int main()
{
int n,rem,rev=0;
clrscr();
printf("\n Enter a number: ");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
rev=(rev*10)+rem;
n=n/10;
}
printf("\n Reverse of a number is: %d",rev);
getch();
return 0;
}
Output:
Program 20
/* Program to check whether a number is a palindrome or not */
#include<stdio.h>
#include<conio.h>
int main()
{
int n,rem,rev,x;
clrscr();
printf("\n Enter a number: ");
scanf("%d",&n);
x=n;
while(n>0)
{
rem=n%10;
rev=(rev*10)+rem;
n=n/10;
}
if(rev==x)
printf("\n Given number is Palindrome");
else
printf("\n Given number is not a palindrome");
getch();
return 0;
}
Output:
Program 21
/* Program to check whether a number is prime or not */
#include<stdio.h>
#include<conio.h>
int main()
{
int n,d;
int flag=1;
clrscr();
printf("\n Enter a number: ");
scanf("%d",&n);
d=2;
while(d<n)
{
if(n%d==0)
{
flag=0;
break;
}
d++;
}
if(flag==1)
printf("\n Number is prime ");
else
printf("\n Number is not prime ");
getch();
return 0;
}
Output:
Program 22
/* Program to check whether the given number is an Amstrong number or not */
#include<stdio.h>
#include<conio.h>
int main()
{
int n,rem,rev=0,ams;
clrscr();
printf("\n Enter a number: ");
scanf("%d",&n);
ams=n;
while(n>0)
{
rem=n%10;
rev=rev+(rem*rem*rem);
n=n/10;
}
if(rev==ams)
printf("\n Given number is an amstrong number ");
else
printf("\n Given Number is not an amstrong number ");
getch();
return 0;
}
Output:
Program 23
/* Program to code simple calculator */
#include<stdio.h>
#include<conio.h>
int main()
int a,b;
char symbol;
clrscr();
printf("\n + - * / ");
scanf("%c",&symbol);
scanf("%d",&a);
scanf("%d",&b);
switch(symbol)
case '+':
printf("\n %d",(a+b));
break;
case '-':
printf("\n %d",(a-b));
break;
case '*':
printf("\n %d",(a*b));
break;
case '/':
printf("\n %f",(a/b));
break;
default:
getch();
return 0;
Output:
Program 24
/* Program to create Pyramid and Pattern */
#include<stdio.h>
#include<conio.h>
int main()
{
int i,space,rows,k=0;
clrscr();
printf("\n Enter no.fo rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;++i,k=0)
{
for(space=1;space<=rows-i;++space)
{
printf(" ");
}
while(k!=2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}
getch();
return 0;
}
Output:
Program 25
int main()
{
clrscr();
printf("\n Enter a Sentence: ");
reverseSentence();
getch();
return 0;
}
void reverseSentence()
{
char c;
scanf("%c",&c);
if(c!='\n')
{
reverseSentence();
printf("%c",c);
}
}
Output:
Program 26
/* Program to Display Prime Numbers between Intervals using Function */
#include<stdio.h>
#include<conio.h>
void prime(int,int);
int main()
{
int lb,ub;
clrscr();
printf("\n Enter the Lower limit: ");
scanf("%d",&lb);
printf("\n Enter the Upper limit: ");
scanf("%d",&ub);
prime(lb,ub);
getch();
return 0;
}
void prime(int lb,int ub)
{
int d,i;
int flag=0;
for(i=lb;i<=ub;i++)
{
d=2;
flag=0;
while(d<i)
{
if((i%d)==0)
{
flag=1;
break;
}
d++;
}
if(flag==0)
printf("\n prime = %d",i);
}
}
Output:
Program 27
/* Program to convert Binary to decimal and Decimal to Binary */
#include<stdio.h>
#include<conio.h>
#include<math.h>
long long convertbinary(int);
int convertdecimal(long long);
int main()
{
int n; long long n1;
clrscr();
printf("\n Enter a decimal number: ");
scanf("%d",&n);
printf("\n %d in decimal = %lld in binary",n,convertbinary(n));
printf("\n Enter a binary number: ");
scanf("%lld",&n1);
printf("%lld in binary = %d in decimal",n1,convertdecimal(n1));
getch();
return 0;
}
Output:
Program 29
/* program to calculate power using recursion */
#include<stdio.h>
#include<conio.h>
int power(int,int);
int main()
{
int base,p,result;
clrscr();
printf("\n Enter base number: ");
scanf("%d",&base);
printf("\n Enter power number(positive integer): ");
scanf("%d",&p);
result=power(base,p);
printf("%d ^ %d = %d",base,p,result);
getch();
return 0;
}
int power(int base,int p)
{
if(p!=0)
return (base*power(base,p-1));
else
return 1;
}
Output:
Program 30
/* Program to find GCD using recursion */
/* Program to find GCD using recursion */
#include<stdio.h>
#include<conio.h>
int gcd(int,int);
int main()
{
int n1,n2;
clrscr();
printf("\n Enter two positive integers: ");
scanf("%d %d",&n1,&n2);
printf(" GCD of %d and %d is %d",n1,n2,gcd(n1,n2));
getch();
return 0;
}
int gcd(int n1,int n2)
{
while(n1!=n2)
{
if(n1>n2)
return gcd(n1-n2,n2);
else
return gcd(n1,n2-n1);
}
return n1;
}
Output:
Program 31
/* Program to caculate average using Arrays */
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10];
int i,l,avg,sum=0;
clrscr();
printf("\n Enter how many values you want to enter: ");
scanf("%d",&l);
printf("\n Enter %d elements into the array: ",l);
for(i=0;i<l;i++)
scanf("%d",&a[i]);
for(i=0;i<l;i++)
{
sum=sum+a[i];
avg=sum/l;
}
printf("\n average of number is: %d",avg);
getch();
return 0;
}
Output:
Program 32
/* Program to find largest element in an Array */
#include<stdio.h>
#include<conio.h>
int main()
{
int a[6]={7,2,4,91,6,3};
int max,i;
clrscr();
max=a[0];
for(i=0;i<6;i++)
{
if(max<a[i])
max=a[i];
}
printf("\n Largest element in an array is: %d",max);
getch();
return 0;
}
Output:
Program 33
/* Program to add two matrices using multi - dimensional arrays */
#include<stdio.h>
#include<conio.h>
int main()
{
int a[2][2],b[2][2],c[2][2];
int i,j;
clrscr();
printf("\n Enter values into matix a: \n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("\n Enter values into matrix b: \n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
printf("\n after adding a & b: \n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n Result is: \n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(" %d",c[i][j]);
}
printf("\n");
}
getch();
return 0;
}
Output:
Program 34
/* Program to find length of a string */
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
char s[20];
clrscr();
scanf("%s",&s);
getch();
return 0;
Output:
Program 35
/* Program to concatenate two strings */
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char s1[20],s2[10];
clrscr();
printf("\n Enter two strings: ");
scanf("%s %s",&s1,&s2);
strcat(s1,s2);
printf("\n Strings after concatenation is: %s",s1);
getch();
return 0;
}
Output:
Program 36
/* Program to copy string without using strcpy() */
#include<stdio.h>
#include<conio.h>
int main()
{
char s1[10],s2[10];
int i=0;
clrscr();
printf("\n Enter a string: ");
scanf("%s",&s2);
for(i=0;s2[i]!='\0';++i)
{
s1[i]=s2[i];
}
s1[i]='\0';
printf("\n value in s1 after copy is: %s",s1);
getch();
return 0;
}
Output:
Program 37
/* Program to count the number of vowels, consonants in a string */
#include<stdio.h>
#include<conio.h>
int main()
{
char s[10];
int vowels=0,cons=0,spl=0;
int i=0;
clrscr();
printf("\n Enter a string: ");
scanf("%s",&s);
while(s[i]!='\0')
{
swich(s[i])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++;
break;
case '@':
case '.':
case ',':
case '/';
case ':':
case ';':
case '!':
case '?':
spl++;
break;
default:
cons++;
}
i++;
}
printf("\n No.of vowels = %d",vowels);
printf("\n No.of Consonents = %d",cons);
printf("\n No.of Special symbols = %d",spl);
getch();
return 0;
}
Output:
Program 38
/* Program to find the freqency of characters in a string */
#include<stdio.h>
#include<conio.h>
int main()
{
char s[20],ch;
int count=0,i;
clrscr();
printf("\n Enter a string: ");
scanf("%s",&s);
printf("\n Enter a character to find its frequency: ");
scanf("%s",&ch);
for(i=0;s[i]!='\0';++i)
{
if(ch==s[i])
++count;
}
printf("\n Frequency of %c = %d",ch,count);
getch();
return 0;
}
Output:
Program 39
/* Program to Access Array elements using Pointers */
#include<stdio.h>
#include<conio.h>
int main()
{
int x[10];
int *p,i;
clrscr();
printf("\n Enter elements into the array ");
for(i=0;i<10;i++)
scanf("%d",&x[i]);
p=&x;
printf("\n The element at first position of array is: %d",*p);
printf("\n Element at fourth position is: %d",p[4]);
getch();
return 0;
}
Output:
Program 40
/* Program to create, initialize, assign and access a pointer variable */
#include<stdio.h>
#include<conio.h>
int main()
{
int x;
int *p;
clrscr();
p=&x;
printf("\n Initializing value to x using pointer: ");
scanf("%d",&(*p));
printf("\n value in x is %d",x);
getch();
return 0;
}
Output:
Program 41
/* Program to swap two numbers using pointers */
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
int main()
{
int a,b;
clrscr();
printf("\n Enter values into a and b: ");
scanf("%d %d",&a,&b);
printf("\n value of a = %d and b = %d before swap",a,b);
swap(&a,&b);
getch();
return 0;
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
printf("\n Value of a = %d and b = %d after swap",*x,*y);
}
Output:
Program 42
/* Program to count vowels and consonants in a string using pointers */
#include<stdio.h>
#include<conio.h>
int main()
{
char s[20];
char *p;
int v=0,c=0;
clrscr();
printf("\n Enter a string: ");
scanf("%s",&s);
p=s;
while(*p!='\0')
{
if(*p=='a'||*p=='e'||*p=='i'||*p=='o'||*p=='u')
v++;
else
c++;
p++;
}
printf("\n Number of vowels in string is: %d",v);
printf("\n Number of consonants in string is: %d",c);
getch();
return 0;
}
Output:
Program 43
/* Program to store information of a student using structure */
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct student
{
int rno;
char name[20];
char course[10];
};
int main()
{
struct student s;
clrscr();
printf("\n Enter Name: ");
scanf("%s",&s.name);
printf("\n Enter course: ");
scanf("%s",&s.course);
printf("\n Welcome %s",s.name);
printf("\n you are selected for %s",s.course);
s.rno=rand();
printf("\n Your roll number is %d",s.rno);
getch();
return 0;
}
Output:
Program 44
/* Program to add two distances using structures */
#include<stdio.h>
#include<conio.h>
struct distance
{
int d1;
int d2;
};
int main()
{
struct distance ob;
clrscr();
printf("\n Enter distance1: ");
scanf("%d",&ob.d1);
printf("\n Enter distance2: ");
scanf("%d",&ob.d2);
printf("\n sum of distances is: %d",(ob.d1+ob.d2));
getch();
return 0;
}
Output:
Program 45
/* Program to store information of a student using structure */
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct student
{
int rno;
char name[20];
char course[10];
};
int main()
{
struct student s[5];
int i;
clrscr();
for(i=0;i<5;i++)
{
printf("\n Enter Name: ");
scanf("%s",&s[i].name);
printf("\n Enter course: ");
scanf("%s",&s[i].course);
s[i].rno=rand();
}
for(i=0;i<5;i++)
{
printf("\n Welcome %s",s[i].name);
printf("\n you are selected for %s",s[i].course);
printf("\n Your roll number is %d",s[i].rno);
}
getch();
return 0;
}
Output:
Program 46
/* Program to declare and initialize an Union */
#include<stdio.h>
#include<conio.h>
union Test
{
int x;
float f;
};
int main()
{
union Test t;
clrscr();
t.x=4719;
t.f=13.567;
printf("\n Value of x is %d",t.x);
printf("\n Value of f is %f",t.f);
getch();
return 0;
}
Output:
Program 47
/* Program to implement function overloading */
#include<iostream.h>
#include<conio.h>
class FOverload
{
public:
void add(int x1,int x2)
{
cout<<"\n x1 + x2 = "<<(x1+x2);
}
void add(float f,int x1,int x2)
{
cout<<"\n f + x1 + x2 = "<<(f+x1+x2);
}
};
void main()
{
clrscr();
FOverload ob1;
ob1.add(12,13);
ob1.add(13.24,4,7);
getch();
}
Output:
Program 48
/* Program to calculate an area of rectangle using encapsulation */
#include<iostream.h>
#include<conio.h>
class Rectangle
{
private:
int l,b;
public:
Rectangle(int l1,int b1)
{
l=l1;
b=b1;
}
void area()
{
cout<<"\n Area of rectangle (l * b) ="<<(l*b);
}
};
void main()
{
int l,b;
clrscr();
cout<<"\n Enter length: ";
cin>>l;
cout<<"\n Enter breadth: ";
cin>>b;
Rectangle r(l,b);
r.area();
getch();
}
Output:
Program 49
/* Program to add two numbers using data abstraction */
#include<iostream.h>
#include<conio.h>
class addition
{
private:
int x;
int y;
public:
addition(int x1,int y1)
{
x=x1;
y=y1;
}
void add()
{
cout<<"\n x + y ="<<(x+y);
}
};
void main()
{
clrscr();
addition a(12,43);
a.add();
getch();
}
Output:
Program 50
/* Program to overload binary operators */
#include<iostream.h>
#include<conio.h>
class OperatorOverload
private:
int x,y;
public:
OperatorOverload()
cin>>x>>y;
x=this->x+ob.x;
y=this->y+ob.y;
void display()
};
void main()
{
clrscr();
OperatorOverload ob1,ob2;
ob1+=ob2;
ob1.display();
ob2.display();
getch();
Output: