1.Write a C Program to Add Two Integers.
#include <stdio.h>
void main()
{ int a,b,c;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
c=a+b;//Adding two numbers
printf("\nSum of %d and %d is %d\n",a,b,c);
}
Output:
Enter two numbers
25
35
Sum of 25 and 35 is 60
*********************************************
2.Write a C Program to Find ASCII Value of a Character
#include <stdio.h>
void main()
{ char c;
printf("Enter a Character\n");
scanf("%c",&c);
printf("\nASCII value of %c is %d",c,c);
}
Output
Enter a Character
a
ASCII value of a is 97
*********************************
3.Write a C Program to Find the Size of int, float, double and char.
#include <stdio.h>
void main()
{ int i;
float f;
double d;
char c;
printf("Size of Integer is %lu",sizeof(i));
printf("\nSize of Float is %lu",sizeof(f));
printf("\nSize of Double is %lu",sizeof(d));
printf("\nSize of Character is %lu",sizeof(c));
}
Output
Size of Integer is 2
Size of Float is 4
Size of Double is 8
Size of Character is 1
*********************************
4.Write a C Program to Swap Two Numbers Using Temporary Variable.
#include <stdio.h>
void main()
{ int x,y,z;
printf("Enter two numbers\n");
scanf("%d%d",&x,&y);
printf("\n Before Swapping");
printf("\n x=%d",x);
printf("\n y=%d",y);
z=x;
x=y;
y=z;
printf("\n After Swapping");
printf("\n x=%d",x);
printf("\n y=%d",y);
Output
Enter two numbers
15
25
Before Swapping
x=15
y=25
After Swapping
x=25
y=15
******************************
5.Write a C Program to Check Odd or Even Using the Ternary Operator.
#include <stdio.h>
void main()
{ int n;
printf("Enter a number\n");
scanf("%d",&n);
(n%2==0)?printf("%d is Even",n):printf("%d is Odd",n);
}
Output
Enter a number
56
56 is Even
********************************************
6.Write a C Program to Check Whether a Character is a Vowel or Consonant.
#include <stdio.h>
# include <ctype.h>
void main()
{ char c,c1;
printf("Enter an Alphabet\n");
scanf("%c",&c);
c1=tolower(c);
if(c1=='a'|| c1=='e'||c1=='i'||c1=='o'||c1=='u')
printf("\n %c is an Vowel",c);
else
printf("\n %c is a Consonant",c);
}
Output
Enter an Alphabet
I
I is an Vowel
***************************************
7.Write a C Program to Find the Largest Number Among Three Numbers.
#include <stdio.h>
void main()
{ int a,b,c;
printf("Enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{ if(a>c)
printf("\n %d is the Largest number",a);
else
printf("\n %d is the Largest number",c);
}
else
{ if (b>c)
printf("\n %d is the Largest number",b);
else
printf("\n %d is the Largest number",c);
}
}
Output
Enter three numbers
12
23
34
34 is the Largest number
*******************************************
8.Write a C Program to Calculate the Sum of first ‘N’ Natural Numbers.
#include <stdio.h>
void main()
{
int i,n,sum=0;
printf("Enter a Number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("\nSum of first %d numbers is :%d",n,sum);
}
Output
Enter a Number
10
Sum of first 10 numbers is :55
************************************************
9.Write a C Program to Find Factorial of a Number
#include <stdio.h>
void main()
{
int n,i,fact=1;
printf("Enter a number\n");
scanf("%d",&n);
if (n==0)
printf("\n Factorial of 0 is 1");
else
{
for(i=1;i<=n;i++)
fact=fact*i;
printf("\n Factorial of %d = %d",n,fact);
}
}
Output
Enter a number
5
Factorial of 5 = 120
***********************************************
10.Write a C Program to Generate Multiplication Table of a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n;
clrscr();
printf("Enter a Number\n");
scanf("%d",&n);
printf("\n Multiplication Table of %d is",n);
for(i=1;i<=10;i++)
printf("\n%d * %d = %d",n,i,n*i);
}
Output:
-------
*****************************************************
11.Write a C Program to Display Fibonacci Sequence up to ‘n’ numbers.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,f0,f1,f2,n;
printf("Enter a number\n");
scanf("%d",&n);
f0=0;
f1=1;
printf("%3d%3d",f0,f1);
for(i=3;i<=n;i++)
{
f2=f0+f1;
printf("%3d",f2);
f0=f1;
f1=f2;
}
}
Output
----
*************************************************************
12.Write a C Program to Count Number of Digits in an Integer.
#include <stdio.h>
#include <conio.h>
void main()
{
int n1,n2,count=0;
clrscr();
printf("Enter a number:\n");
scanf("%d",&n1);
n2=n1;
while(n1>0)
{
count++;
n1=n1/10;
}
printf("\n Number of digits in %d is %d",n2,count);
}
Output:
-----
***************************************************
13.Write a C Program to Check Whether a Number is Palindrome or Not.
#include <stdio.h>
#include <conio.h>
void main()
{
int n,original_number,rem,result=0;
clrscr();
printf("Enter a Number\n");
scanf("%d",&n);
original_number=n;
while(n>0)
{
rem=n%10;
result=result*10+rem;
n=n/10;
}
if(original_number==result)
printf("\n %d is a Palindrome",original_number);
else
printf("\n %d is not a Palindrome",original_number);
}
Output:
------
14.Write a C Program to Check whether the given number is an Armstrong Number or
not.
#include <stdio.h>
#include <conio.h>
void main()
{
int n,original_number,rem,result=0;
clrscr();
printf("Enter a Number\n");
scanf("%d",&n);
original_number=n;
while(n>0)
{
rem=n%10;
result=result+rem*rem*rem;
n=n/10;
}
if (original_number==result)
printf("\n %d is an Armstrong Number",original_number);
else
printf("\n %d is Not an Armstrong Number",original_number);
}
Output:
--------
*****************************************************************
15.Write a C Program to Make a Simple Calculator Using switch...case.
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
char calc;
clrscr();
printf("\nPress \"+\" for Addition");
printf("\nPress \"-\" for Subtraction");
printf("\nPress \"*\" for Multiplcation");
printf("\nPress \"/\" for Division\n");
printf("Enter the arthimetic operation symbol\n");
scanf("%c",&calc);
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
switch(calc)
{
case '+': printf("\n%d + %d = %d",a,b,a+b);
break;
case '-': printf("\n%d - %d = %d",a,b,a-b);
break;
case '*': printf("\n%d * %d = %d",a,b,a*b);
break;
case '/': printf("\n%d / %d = %d",a,b,a/b);
break;
default: printf("\n Invalid symbol");
}
}
Output:
--------
****************************************
16.Write a C Program to Check Prime Using User-defined Function.
#include <stdio.h>
#include <conio.h>
void chkprime(int n);
void main()
{
int m;
clrscr();
printf("\n Enter a Number\n");
scanf("%d",&m);
chkprime(m);
}
void chkprime(int n)
{ int i;
for(i=2;i<n;i++)
{
if(n%i==0)
break;
}
if(i==n)
printf("\n %d is a Prime Number\n",n);
else
printf("\n %d is not a Prime Number",n);
}
OUTPUT:
--------
***************************************
17.Write a C program to calculate the power using recursion.
#include <stdio.h>
#include <conio.h>
int power(int x,int y);
void main()
{
int m,n,res;
clrscr();
printf("\n Enter a Number\n");
scanf("%d",&m);
printf("Enter the Power to be calculated\n");
scanf("%d",&n);
res=power(m,n);
printf("\n %d to the power of %d is : %d",m,n,res);
}
int power(int x,int y)
{ if(y==0)
return(1);
else
return(x*power(x,y-1));
}
OUTPUT:
----------
***************************************
18.Write a C Program to Find G.C.D Using functions.
#include <stdio.h>
#include <conio.h>
int gcd1(int x,int y);
void main()
{
int n1, n2,gcd;
clrscr();
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
gcd=gcd1(n1,n2);
printf("G.C.D of %d and %d is %d", n1, n2, gcd);
}
int gcd1(int x,int y)
{ int temp,i;
for(i=1; i <= x && i <= y; ++i)
{
// checks if i is factor of both integers
if(x%i==0 && y%i==0)
temp = i;
}
return(temp);
}
OUTPUT:
-----------------------
********************************************
19.Write a C Program to Calculate Average Using Arrays.
#include <stdio.h>
#include <conio.h>
void main()
{
int marks[6],i,total=0;
float avg;
clrscr();
printf("Enter marks of 6 subjects\n");
for(i=0;i<6;i++)
{
scanf("%d",&marks[i]);
total=total+marks[i];
}
avg=(float)total/6;
printf("\n Total of marks is : %d",total);
printf("\n Average of marks is : %f",avg);
}
OUTPUT:
---------------------------------------
********************************************
20.Write a C Program to Add Two Matrices Using Multi-Dimensional Arrays.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,rows,cols;
clrscr();
printf("Enter number of rows and columns\n");
scanf("%d%d",&rows,&cols);
printf("\nEnter Matrix1 with %d numbers\n",rows*cols);
// Reading Matrix1
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
scanf("%d",&a[i][j]);
// Reading Matrix2
printf("\nEnter Matrix2 with %d numbers\n",rows*cols);
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
scanf("%d",&b[i][j]);
printf("\n Matrix Addition is\n");
for(i=0;i<rows;i++)
{
printf("\n");
for(j=0;j<cols;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
} // j loop closed
}// i loop closed
}
21.Write a C Program to Find the Length of a String.
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char s1[20];
clrscr();
printf("Enter a String\n");
gets(s1);
printf("\nLength of the String %s is :%ld",s1,strlen(s1));
}
Output
----------------
22. Write a C Program to Copy String Without Using strcpy ().
#include <stdio.h>
#include <conio.h>
void main()
{
char s1[100], s2[100], i;
clrscr();
printf("Enter string s1: ");
gets(s1);
for (i = 0; s1[i] != '\0'; ++i)
{
s2[i] = s1[i];
}
s2[i] = '\0';
printf("String s2: %s", s2);
}
OUTPUT:
------------------------------------
23. Write a C Program to Access Array Elements Using Pointers.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[5]={1,2,3,4,5};
int i;
int *ptr=a;
clrscr();
printf("Array Element Values Accessed by Pointer ptr are\n ");
for(i=0;i<5;i++)
{
printf("%d\n",*ptr);
ptr++;
}
}
OUTPUT
---------------------------
24. Write a C program to create, initialize, assign and access a pointer variable.
#include <stdio.h>
#include <conio.h>
void main()
{
int x=10;
int *ptr=&x;
clrscr();
printf("\nValue of x = %d",x);
printf("\nAddress of x =%p",&x);
printf("\n Value of ptr =%p",ptr);
printf("\n Value of x accessed by pointer = %d",*ptr);
}
OUTPUT
----------------------------------
25. Write a C program to swap two numbers using pointers
#include <stdio.h>
#include <conio.h>
void swap1(int a, int b);
void swap2(int *p1, int *p2);
void main()
{ int x=10,y=20;
clrscr();
printf("\n Before swapping");
printf("\n x=%d",x);
printf("\n y=%d",y);
swap1(x,y);
printf("\n After swapping by Call By Value");
printf("\n x=%d",x);
printf("\n y=%d",y);
swap2(&x,&y);
printf("\n After swapping by Call By Reference");
printf("\n x=%d",x);
printf("\n y=%d",y);
}
void swap1(int a, int b)
{ int temp;
temp=a;//temp=10
a=b;//a=20
b=temp;//b=10
}
void swap2(int *p1, int *p2)
{ int temp;
temp=*p1;//temp=10
*p1=*p2;//a=20
*p2=temp;//b=10
}
Output
-------------------------
26. Write a C Program to Store Information of a Student Using Structure.
#include <stdio.h>
#include <conio.h>
struct student
{
char name[20];
int rno;
float marks[6];
float total;
};
void main()
{
struct student s;
int i;
printf("Enter Student Details:\n");
printf("Enter name\n");
gets(s.name);
printf("Enter Roll number\n");
scanf("%d",&s.rno);
printf("Enter 6 subject marks\n");
s.total=0;
for(i=0;i<6;i++)
{
scanf("%f",&s.marks[i]);
s.total=s.total+s.marks[i];
}
printf("\n Displaying Student Information:\n");
printf("Student Name: %s",s.name);
printf("\nStudent Roll Number: %d",s.rno);
printf("\nStudent Total marks: %f",s.total);
}
OUTPUT
Enter Student Details:
Enter name
Sriha
Enter Roll number
18
Enter 6 subject marks
80
85
90
95
98
96
Displaying Student Information:
Student Name: Sriha
Student Roll Number: 18
Student Total marks: 544.000000
27. Write a C program to declare, initialize a union
#include <stdio.h>
#include <string.h>
union employee
{
char name[20];
int age;
float salary;
};
void main()
{
union employee emp;
strcpy(emp.name,"John");
printf("\n Employee Name=%s\t Memory location=%p",
emp.name,&emp.name);
emp.age=30;
printf("\n Employee age=%d\t Memory location=%p",emp.age,&emp.age);
emp.salary=12500;
printf("\n Employee salary=%f\t Memory location=%p",
emp.salary,&emp.salary);
printf("\n Displaying all Union members");
printf("\nName:%s",emp.name);
printf("\nAge:%d",emp.age);
printf("\nSalary:%f",emp.salary);
}
OUTPUT
Employee Name=John Memory location=0x7ffe82465810
Employee age=30 Memory location=0x7ffe82465810
Employee salary=12500.000000 Memory location=0x7ffe82465810
Displaying all Union members
Name:
Age:1178816512
Salary:12500.000000
28. Write a C++ program to implement function overloading
#include<iostream.h>
// using namespace std; //Online compiler
#include<conio.h>
float area(float r);
int area(int l1,int b1);
float area(float l2,float b2);
int main()
{
int l,b;
float r,fl,fb;
clrscr();
cout<<"\nEnter radius of circle:\n";
cin>>r;
cout<<"\nEnter length and breadth of rectangle:\n";
cin>>l>>b;
cout<<"\nEnter length and breadth in floating points\n ";
cin>>fl>>fb;
cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of circle is "<<area(r);
cout<<"\nArea of rectangle with float values is "<<area(fl,fb);
}
int area(int l1,int b1)
{
return(l1*b1);
}
float area(float r)
{
return(3.14*r*r);
}
float area(float l2,float b2)
{
return(l2*b2);
}
OUTPUT
Enter radius of circle:
5
Enter length and breadth of rectangle:
10
4
Enter length and breadth in floating points
21.1
3.4
Area of rectangle is 40
Area of circle is 78.5
Area of rectangle with float values is 71.74
29.Write a C++ program to calculate an area of rectangle using encapsulation.
#include <iostream.h>
#include <conio.h>
//using namespace std;//[Online Compiler]
class Rect
{ int length,breadth;
public:
void read();
int area();
};
void Rect::read()
{
cout<<"\nEnter Length and Breadth\n";
cin>>length>>breadth;
}
int Rect::area()
{
return(length*breadth);
}
int main()
{
Rect r1;
clrscr();
r1.read();
cout<<"Area of the rectangle is "<<r1.area();
return 0;
}
OUTPUT
Enter Length and Breadth
20
6
Area of the rectangle is 120
30. Write a C++ program to add two numbers using data abstraction.
#include <iostream.h>
#include <conio.h>
//using namespace std;//[Online Compiler]
class add
{ int x,y;
public:
void read()
{ cout<<"\nEnter two numbers\n";
cin>>x>>y;
}
void sum()
{
cout<<"\n"<<x<<"+"<<y<<"="<<x+y;
}
};
int main()
{ add a1;
clrscr();
a1.read();
a1.sum();
return 0;
}
OUTPUT
Enter two numbers
35
56
35+56=91
31.Write a C++ program to overload binary operators
#include <iostream.h>
#include <conio.h>
//using namespace std;//[Online Compiler]
class abc
{
int value;
public:
void read(int x)
{
value = x;
}
abc operator+(abc t1)
{
abc t;
t.value = value + t1.value;
return (t);
}
void display()
{
cout<<"Value is "<<value<<endl;
}
};
int main()
{
abc a1,a2,a3;
clrscr();
a1.read(15);
a2.read(25);
a3=a1+a2;
a1.display();
a2.display();
a3.display();
return 0;
}
OUTPUT
Value is 15
Value is 25
Value is 40