0% found this document useful (0 votes)
16 views37 pages

C & C++ Record

The document contains a series of C programs that demonstrate various programming concepts including arithmetic operations, input/output handling, control structures, string manipulation, and data structures. Each program is designed to perform a specific task such as calculating sums, checking for prime numbers, and manipulating arrays and structures. The programs are structured with standard C syntax and utilize functions like printf and scanf for user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views37 pages

C & C++ Record

The document contains a series of C programs that demonstrate various programming concepts including arithmetic operations, input/output handling, control structures, string manipulation, and data structures. Each program is designed to perform a specific task such as calculating sums, checking for prime numbers, and manipulating arrays and structures. The programs are structured with standard C syntax and utilize functions like printf and scanf for user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Program 1

/* Program to find sum of Two numbers */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
clrscr();
a=12;
b=14;
sum=a+b;
printf("\n sum of two integers is: %d",sum);
getch();

}
Output:
Program 2
/* Program to print Integer (Entered by the user ). */

#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("\n Enter an Integer: ");
scanf("%d",&x);
printf("\n You have entered: %d",x);
getch();

}
Output:
Program 3
/* Program to multiply two floating point numbers */

#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 4
/* Program to find ASCII value of Characters */

#include<stdio.h>

#include<conio.h>

void main()

char letter;

clrscr();

printf("\n Enter a character: ");

scanf("%c",&letter);

printf("\n ASCII value of given Character is: %d",letter);

getch();

Output:
Program 5
/* Program to Compute Quotient and Remainder */

#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 6
/* Program to find the size of int, float, char and double */

#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 7
/* program to swap two numbers using temporary variable */

#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 8
/* Program to check whether a number is even or odd */

#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 9
/* Program to check odd or even using the Ternary Operator */

#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 10
/* Program to check whether a character is a Vowel or Consonant */
#include<stdio.h>
#include<conio.h>
void 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();
}
Output:
Program 11
/* Program to find Largest Number Among Three numbers */

#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 12
/* Program to check given year leap year or not */

#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 13
/* Program to check whether a character is an Alphabet or not */

#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 14
/* Program to find the sum of first N natural Numbers */
#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 15
/* Program to find factorial of a number */

#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 16
/* program to Generate Multiplication table of a given number */

#include<stdio.h>
#include<conio.h>
void 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();
}
Output:
Program 17
/* Program to Display Fibonacci sequence up to n numbers */

#include<stdio.h>
#include<conio.h>
void 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();
}
Output:
Program 18
/* Program to Count Number of Digits in an Integer */
#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 19
/* Program to Reverse a Number */
#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 20
/* Program to check whether a number is a palindrome or not */

#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 21
/* Program to check whether a number is prime or not */

#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 25
/* Program to reverse a sentence using recursion */
#include<stdio.h>
#include<conio.h>
void reverseSentence();

void main()
{
clrscr();
printf("\n Enter a Sentence: ");
reverseSentence();
getch();
}
void reverseSentence()
{
char c;
scanf("%c",&c);
if(c!='\n')
{
reverseSentence();
printf("%c",c);
}
}

Output:
Program 32
/* Program to find largest element in an Array */
#include<stdio.h>
#include<conio.h>
void 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();

Output:
Program 33
/* Program to add two matrices using multi - dimensional arrays */
#include<stdio.h>
#include<conio.h>
void 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();
}
Output:
Program 34:

/* Program to find length of a string */

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char s[20];

clrscr();

printf("\n Enter a string: ");

scanf("%s",&s);

printf("\n Length of given string is: %d",strlen(s));

getch();

Output:
Program 35:
/* Program to concatenate two strings */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void 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();

Output:
Program 36:
/* Program to copy string without using strcpy() */
#include<stdio.h>
#include<conio.h>
void 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();

}
Output:
Program 40:
/* Program to create, initialize, assign and access a pointer variable */
#include<stdio.h>
#include<conio.h>
void 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();

Output:
Program 41
/* Program to swap two numbers using pointers */
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void 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();
}
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 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];
};
void 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();

Output:
Program 44:
/* Program to add two distances using structures */
#include<stdio.h>
#include<conio.h>
struct distance
{
int d1;
int d2;
};
void 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();

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];
};
void 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();
}
Output:
Program 46
/* Program to declare and initialize an Union */
#include<stdio.h>
#include<conio.h>
union Test
{
int x;
float f;
};
void 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();

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 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:

You might also like