R21 C Programs
R21 C Programs
#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;
scanf("%d%d",&x,&y);
printf("\n y=%d",y);
z=x;
x=y;
y=z;
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
#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:
------
Output:
--------
*****************************************************************
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:
--------
****************************************
OUTPUT:
----------
***************************************
}
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:
-----------------------
********************************************
}
OUTPUT:
---------------------------------------
********************************************
s2[i] = '\0';
printf("String s2: %s", s2);
}
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
}
OUTPUT
Enter Student Details:
Enter name
Sriha
Enter Roll number
18
Enter 6 subject marks
80
85
90
95
98
96
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
OUTPUT
Enter radius of circle:
5
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
void read(int x)
{
value = x;
}
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