[go: up one dir, main page]

0% found this document useful (0 votes)
19 views12 pages

Important Programs

The document contains a series of C-language programs that perform various tasks such as arithmetic operations, temperature conversions, and number manipulations. Each program is presented with its code and a brief description of its functionality. The programs cover topics like reading user input, using control structures, and implementing mathematical functions.

Uploaded by

vibefuse0967
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)
19 views12 pages

Important Programs

The document contains a series of C-language programs that perform various tasks such as arithmetic operations, temperature conversions, and number manipulations. Each program is presented with its code and a brief description of its functionality. The programs cover topics like reading user input, using control structures, and implementing mathematical functions.

Uploaded by

vibefuse0967
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/ 12

C-Language Important Programs

Write a program that reads two float values and find Sum, Average and Product of
1
these numbers.
void main ( )
{ float a, b, sum, avg, prod;
printf(“Enter value of a = “);
scanf(“%f”, &a);
printf(“Enter value of b = “);
scanf(“%f”, &b);
sum = a + b;
avg = sum / 2;
prod = a * b;
printf(“Sum of a & b = %f“, sum);
printf(“Average of a & b = %f“, avg);
printf(“Product of a & b = %f“, prod);
getch( );
}

2 Write a program that reads a Character and then display its ASCII value.

void main ( )
{ char ch;
int ascii;
printf(“Enter Character = “);
scanf(“%c”, &ch);
ascii = ch;
printf(“ASCII value of ch = %d“, ascii);
getch( );
}
Write a program that reads temperature in Celsius and then display it in Fahrenheit by
3
using the formula: C = 5/9 (F – 32)
void main ( )
{ float c, f;
printf(“Enter Temperature in Celsius = “);
scanf(“%f”, &c);
f = (c * 9) / 5 + 32;
printf(“Temperature in Fahrenheit = %f“, f);
getch( );
}

4 Write a program that reads two variables from the user and then Swap their values.

void main ( )
{ int a, b, temp;
printf(“Enter value of a = “);
scanf(“%d”, &a);
printf(“Enter value of b = “);
scanf(“%d”, &b);
temp = a;
a = b;
b = temp;
printf(“Value of a = %d“, a);
printf(“Value of b = %d“, b);
getch( );
}
5 Write a program that reads a number and then display its Reverse number.

void main ( )
{ int n, r;
printf(“Enter Number = “);
scanf(“%d”, &n);
while( n > 0 )
{ r = n % 10;
printf(“%d“, r);
n = n / 10;
}
getch( );
}

Write a program that reads the value of a radius and then display the Area and
6
Circumference of a circle. (area = π r2) (circumference = 2 π r)

void main ( )
{ float r, area, circum;
printf(“Enter Radius = “);
scanf(“%f”, &r);
area = 3.14 * r * r;
circum = 2 * 3.14 * r;
printf(“Area = %f“, area);
printf(“Circumference = %f“, circum);
getch( );
}

7 Write a program that reads a number and then display its Square Root.

void main ( )
{ int n, sqr;
NOTE:-
printf(“Enter Number = “);
scanf(“%d”, &n);
Add “math.h” headerfile for sqrt()
sqr = sqrt(n);
printf(“Square-Root = %d“, sqr);
getch( );
}

Write a program that reads the value in Kilometer and then display it in Meters and
8
Miles by using formula: (1 km = 1000 m) and (1 km = 1/1.609 miles)

void main ( )
{ float km, meter, miles;
printf(“Enter value in KM = “);
scanf(“%f”, &km);
meter = km * 1000;
miles = km / 1.609;
printf(“KM to Meters = %f“, meter);
printf(“KM to Miles = %f“, miles);
getch( );
}
9 Write a program that reads Year and then convert it into Months and Days.

void main ( )
{ int year, month, days;
printf(“Enter value in Year = “);
scanf(“%d”, &year);
month = year * 12;
days = year * 365;
printf(“Year to Months = %d“, month);
printf(“Year to Days = %d“, days);
getch( );
}

Write a program that reads two values and then display the Maximum using conditional
10
operator.

void main ( )
{ int a, b, max;
printf(“Enter value of a = “);
scanf(“%d”, &a);
printf(“Enter value of b = “);
scanf(“%d”, &b);
max = (a > b) ? a : b;
printf(“Max = %d“, max);
getch( );
}

Write a program that reads Marks from the user and then display whether he/she is
11
“PASS” or “FAIL”.

void main ( )
{ int marks;
printf(“Enter Marks = “);
scanf(“%d”, &marks);
if (marks >= 40)
printf(“PASS“);
else
printf(“FAIL“);
getch( );
}

Write a program that reads Number from the user and then display whether it is
12
“EVEN” or “ODD”.

void main ( )
{ int num;
printf(“Enter Number = “);
scanf(“%d”, &num);
if (num % 2 == 0)
printf(“EVEN“);
else
printf(“ODD“);
getch( );
}
Write a program that reads Number from the user and then display whether it is
13
“POSITIVE”, “NEGATIVE” or “ZERO”.

void main ( )
{ int num;
printf(“Enter Number = “);
scanf(“%d”, &num);
if (num > 0)
printf(“POSITIVE“);
else if (num < 0)
printf(“NEGATIVE“);
else if (num == 0)
printf(“ZERO“);
getch( );
}
Write a program that reads Year and then display whether the given Year is a “LEAP
YEAR” or “NOT A LEAP YEAR”.
14
if (year % 4 == 0)
else if (year % 100 && year % 400)
void main ( )
{ int year;
printf(“Enter Year = “);
scanf(“%d”, &year);
if (year % 4 == 0)
printf(“LEAP YEAR“);
else if (year % 100 && year % 400)
printf(“LEAP YEAR“);
else
printf(“NOT A LEAP YEAR“);
getch( );
}
Write a program that reads Marks and then display his/her Grade by using following
criteria.
A Grade if marks >= 90
15 B Grade if marks >= 80
C Grade if marks >= 70
D Grade if marks >= 60
Otherwise FAIL
void main ( )
{ int marks;
printf(“Enter Marks = “);
scanf(“%d”, &marks);
if (marks >= 90)
printf(“A“);
else if (marks >= 80)
printf(“B“);
else if (marks >= 70)
printf(“C“);
else if (marks >= 60)
printf(“D“);
else
printf(“FAIL“);
getch( );
}
Write a program that reads temperature and then display “HOT DAY”, “PLEASANT
DAY” or “COOL DAY” by using following criteria.
16 Hot Day if temp > 35
Pleasant Day if temp >= 15
Cool Day if temp < 15
void main ( )
{ int temp;
printf(“Enter Temperature = “);
scanf(“%d”, &temp);
if (temp > 35)
printf(“HOT DAY“);
else if (temp >= 15)
printf(“PLEASANT DAY“);
else if (temp < 15)
printf(“COOL DAY“);
getch( );
}

17 Write a program to make Simple Calculator using Switch statement.

void main ( )
{ float a, b, sum, sub, mul, div;
char op;
printf(“Enter First Number = “);
scanf(“%f”, &a);
printf(“Enter Second Number = “);
scanf(“%f”, &b);
printf(“Enter Operator = “);
scanf(“%c”, &op);
switch (op)
{
case ‘+’:
sum = a + b;
printf(“Sum = %f“, sum);
break;

case ‘-’:
sub = a - b;
printf(“Subtraction = %f“, sub);
break;

case ‘*’:
mul = a * b;
printf(“Multiplication = %f“, mul);
break;

case ‘/’:
div = a / b;
printf(“Division = %f“, div);
break;

default:
printf(“INVALID OPERATOR“);
}
getch( );
}
Write a program that reads an Alphabet from the user and then display whether it is
18
“VOWEL” or “CONSONANT”.

void main ( )
{ char ch;
printf(“Enter Character = “);
scanf(“%c”, &ch);
switch (ch)
{
case ‘a’:
case ‘A’:
case ‘e’:
case ‘E’:
case ‘i’:
case ‘I’:
case ‘o’:
case ‘O’:
case ‘u’:
case ‘U’:
printf(“VOWEL“);
break;

default:
printf(“CONSONANT“);
}
getch( );
}

19 Write a program that display EVEN numbers from 1 to 10 using for-loop.

void main ( )
{ int i;
for (i = 1; i <= 10; i++)
{
if (i % 2 == 0)
printf(“%d\n“, i);
}
getch( );
}

20 Write a program that display ODD numbers from 1 to 10 using for-loop.

void main ( )
{ int i;
for (i = 1; i <= 10; i++)
{
if (i % 2 == 1)
printf(“%d\n“, i);
}
getch( );
}
21 Write a program that display Sum of Even and Odd numbers from 1 to 10 using loop.

void main ( )
{ int I, sume = 0, sumo = 0;
for (i = 1; i <= 10; i++)
{
if (i % 2 == 0)
sume = sume + i;
else
sumo = sumo + i;
}
printf(“Sume of Even = %d “, sume);
printf(“Sume of Odd = %d “, sumo);
getch( );
}

Write a program that read a number from the user and then display its Table on the
22
screen.

void main ( )
{ int i, b;
printf(“Enter Number = “);
scanf(“%d”, &n);
for (i = 1; i <= 10; i++)
{
printf(“%d x %d = %d”, n, i, n*i);
}
getch( );
}

Write a program that reads a number from the user and then display its Factorial using
23
While Loop.

void main ( )
{ int i, n, fact = 1;
printf(“Enter Number = “);
scanf(“%d”, &n);
i = 1;
while ( i <= n )
{
fact = fact * i;
i++;
}
printf(“Factorial = %d”, fact);
getch( );
}
24 Write a program that finds the sum of Series (1 + 1/3 + 1/5 + ……………. + 1/99)

void main ( )
{ float sum = 0.0;
int i;
for (i = 1; i <= 99; i++)
{
sum = sum + 1.0 / i;
}
printf(“Sum of Series = %f”, sum);
getch( );
}

Write a program that reads a number from the user and then display its Reverse
number by using loop.
25
while (num > 0)
{ r = num % 10; display r; num = num / 10 }

void main ( )
{ int n, r;
printf(“Enter Number = “);
scanf(“%d”, &n);
while ( n > 0 )
{
r = n % 10;
printf(“%d”, r);
n = n / 10;
}

getch( );
}

Write a program that display the following pattern:


*
26 **
***
****

void main ( )
{ int i, j;

for (i = 1; i <= 4; i++)


{
for (j = 1; j <= i; j++)
{
printf(“ * ”);
}

Printf(“\n”);
}

getch( );
}
Write a program that display the following pattern:

12345
27 1234
123
12
1

void main ( )
{
int i, j;

for (i = 5; i >= 1; i--)


{
for (j = 1; j <= i; j++)
{
printf(“ %d ”, j);
}

Printf(“\n”);
}

getch( );
}

Write a program that display the following pattern:


1 2 3 4 5
28 2 4 6 8 10
3 6 9 12 15
4 8 12 16 20

void main ( )
{
int i, j;

for (i = 1; i <= 5; i++)


{
for (j = 1; j <= 5; j++)
{
printf(“ %d\t ”, i*j);
}

Printf(“\n”);
}

getch( );
}
Write a program that reads a number and display whether it is Prime number or not by
29 using following function.
void Is_Prime( );

void Is_Prime( );

void main ( )
{
Is_Prime( );
}

void Is_Prime( )
{ int i, n, flat = 0;
printf(“Enter Number = “);
scanf(“%d”, &n);
for (i = 2; i <= n/2; i++)
{
if (n % i == 0)
flat = 1;
}

if (flat == 0)
printf(“Prime Number“);
else
printf(“Not Prime Number“);

getch( );
}

Write a program that reads two numbers and finds their GCD by using following
30 function.
int GCD (int, int);

int GCD (int, int );

void main ( )
{
int a, b, r;
printf(“Enter value of a = “);
scanf(“%d”, &a);
printf(“Enter value of b = “);
scanf(“%d”, &b);
r = GCD (a, b);
printf(“GCD = %d”, r);
getch( );
}

int GCD(int x, int y)


{ int i, m = 1;
for (i = 2; i <= x; i++)
{
if (x % i == 0 && y % i == 0)
m = i;
}
return m;
}
Write a simple calculator program by using following functions.
void Add( );
31 void Sub( ):
void Mul( );
void Div( ):
void Add ( );
void Sub ( );
void Mul ( );
void Div ( );

void main ( )
{ char op;
printf(“Enter Operator = “);
scanf(“%c”, &op);
switch (op)
{
case ‘+’:
Add ( );
break;
case ‘-’:
Sub ( );
break;
case ‘*’:
Mul ( );
break;
case ‘/’:
Div ( );
break;
default:
printf(“INVALID OPERATOR“);
} getch( );
}

void Add( )
{ float a, b;
printf(“Enter a & b = “);
scanf(“%f %f”, &a, &b);
printf(“Sum = %f”, a+b);
}
void Sub( )
{ float a, b;
printf(“Enter a & b = “);
scanf(“%f %f”, &a, &b);
printf(“Subtraction = %f”, a-b);
}
void Mul( )
{ float a, b;
printf(“Enter a & b = “);
scanf(“%f %f”, &a, &b);
printf(“Multiplication = %f”, a*b);
}
void Div( )
{ float a, b;
printf(“Enter a & b = “);
scanf(“%f %f”, &a, &b);
printf(“Division = %f”, a/b);
}
Write a program that display the following pattern by using following function:
*
**
32
***
****
void Asterisk( );

void Asterisk( );

void main ( )
{
Asterisk( );
}

void Asterisk( )
{ int i, j;

for (i = 1; i <= 4; i++)


{
for (j = 1; j <= i; j++)
{
printf(“ * ”);
}

Printf(“\n”);
}

getch( );
}
Write a program that reads a number from the user and then display each digit on a
separate line. For example if user enters 587 then display it as on the screen.
33 7
8
5
void main ( )
{ int n, r; void main ( )
printf(“Enter Number = “); { int n, r, max;
scanf(“%d”, &n);
while ( n > 0 ) printf(“Enter Number = “);
{ scanf(“%d”, &n);
r = n % 10; max = n % 10;
This code displays the
printf(“%d\n”, r);
each digit on the
n = n / 10; while ( n > 0 )
} separate line and also
{
finds the maximum
r = n % 10;
getch( ); from them.
printf(“%d\n”, r);
} If u replace the > sign
in if condition with <
if (r > max)
sign then it can find
max = r;
the minimum.
n = n / 10;
}
printf(“Max = %d“, max);
getch( );
}

You might also like