C Programs
C Programs
#include<stdio.h>
#includesstdio.h>
void main(0 void main()
int a, b, C; float a, b, c;
printf("enter 3 integers\n"); printf("enter 3 float numbers\n"):
scanf("%d%d%d", &a, &b, &c); scanf("%f%f%f", &a, &b, &c);
printf("a=%d, b=%d and c=%d", a, b, c); printf(" a=%.2f, b=9%.2f and c=%./2f"
a, b, c);
Output:
enter 3 integers Output:
enter 3 float numbers
10
20 44.35
30 66.15
a=10, b=20 and c=30 22.54
a=44.35, b=66.15 and c=22.54
include<stdio.h>
oid main( )
Output:
enter 2 numbers
int a, b 50
printf( enter 2 numbers \n"); 30
Scanf(%dod", &a, &b); a=50, b=30
printf('a=%d, b=%d", a, b); a is greater
if(a>b)
printt(" a is greater"):
else
printf'b is greater");
Largest of 3 numbers
#include<stdio.h>
void main(O
int a, b, c; Output:
printf("enter 3 numbers \n"); enter 3 numbers
scanf("%d%d%d", &a, &b, &c); 3
printf("a=Y%d, b=%d and c=%d",a, b, c);
if((a>b)&&{a>c))
printf(" a is greater"); a=3, b=4 and c=5
INSTITUTE, TUMKUR
SSPIDERS SOFTWARE TRAINING
Calculate quotient and remainder
#includesstdio.h> Output:
void main) enter dividend
15
int dividend, divisor, quotient, remainder; 6
printf("enter dividend \n"); Quotient=2 and remainder=3
scanf("%d", ÷nd);
printf("enter divisor\n ");
scanf("%d", &divisor);
quotient=dividend/divisor;
remainder=dividend % divisor
printf("quotient=%d and remainder=%d",
quotient, remainder);
#include<stdio.h>
void main()
Output:
enter a number
int n, years, months, weeks, days;
445
printf('enter a number \n");
scanf("%d", &n); Years=1, months=2, weeks=2,
years=n/365; days=6
n=n-(years*365)
months=n/30;
n=n-(months*30);
weeks=n/7;
n=n-(weeks*7);
days=n;
printf("years=%d, months=%d, weeks=%d,
days=%d", years, months, weeks, days);
#include<stdio.h>
y o i dm a i n ( )
void mainO
b, temp;
int a,
2 numbers \n"): int a, b, temp;
printf("'enter 2 numbers \n");
printf("enter
scanf("%d%d", &a, &b);
Scanf("%d%d", &a, &b);
printf("a=%d andb=%d \n", a, b); and b=%d \n",
a, b):
printi('a=%d
temp=a; a=a"b;
a=a+b;
a=b; (or) b=a/b:
b=a-b;
b=temp;
a=a-b;
a=a/b;
printf("a=%d
and b=%d\n", a, b); and b=%d\n",
a, b);
printf("a=%d
Output:
Output: enter 2 numbers
enter 2 numbers
30
10
20
20 a-30 and b=20
a=10 and b=20
a=20 and b=30
a=20 and b=10
Simple interest
#include<stdio.h>
void main()
int p, t, r
float si;
principle
amount \n");
printf('enter
Output:
scanf("%d", &p); e n t e r principle a m o u n t
period in years\n");
printf"enter time 14245
scanf("%d", &t); period in years
enter time
of interest\n");
printf("enter rate 2
enter rate of interest
scanf("%d", &r);
si=(float)(p't'r)/100;
11
Simple interest=3133.9
printf("simple interest=%f\n", si);
TUMKUR
INSTITUTE,
TRAINING
SOFTWARE
S SPIDERS
Program to checka character is Alphabet or not
#include<stdio.h>
void main()
int year
printf("enter a year \n"); Output:
enter a year
scanf("%d", &year);
if((year%4==0 && year%100!=-0)||(year%400==0)) 1700
1700 is not leap year
INSTITUTE, TUMKUR
SOFTWARE TRAINING
S SPIDERS
m to check a number is
negative or positive
ncludesstdio.h>
oidmain[ )
int n,
Output:
enter a number
else
-8
print(%d is positive number", n):
-8 is negative number
void main()
int n,
13
odd number
TUMKUR
SOFTWARE
TRAINING INSTITUTE,
S SPIDERS
Multiplication table Output:
enter a number
#includestdio.h> 4
void main() 4*1 4
4*2-8
int n, i; 4*3-12
#include<stdio.h> #include<stdio.h>
void main0 void main0
printf("number of digits=%d\n",count)
Output:
enter a number
548
Output:
enter a number
Unit digit=8
548
Number of digits=3 Output:
enter a number
Output: 25
enter a number Unit digit=5
25
Number of digits=2
if(m==reverse) n=n/10;
printf( palindrome number");
else if(m==csum)
printf("armstrong number);
printf("not palindrome number"); else
printf"not armstrong number");
Output Output:
enter a number enter a number
548 153
Not a palindrome number Armstrong number
Output:
Output:
enter a number
enter a number
12321
12321
Not Armstrong number
Palindrome number
SOFTWARE TRAINING
INSTITUTE, TUMKUR
S SPIDERS
Sum of digits of a
given number Decimal to Binary
#include<stdio.h> #include<stdio.h>
void main()
void main)
int n, sum=0, ud; int n, remn, i=0, a[30], j;
printf("enter a number\n"); printf("enter a decimal number\n")
scanf("%d", &n); scanf("%d", &n);
while(n!=0) while(n!=0)
{
ud=n%10; rem=n%2;
sum=sum+ud; ail=rem;
n=n/10; n=n/2;
i++
printf("sum of digits=%d", sum);
printf("binary number is:");
forj=i-1; j>=0; j-)
{
Output: Output: printf("%d",a[j]):
enter a number enter a number
548 25
Sum-17 Sum=7
Output:
enter a decimal number
Sum of n array elements
14
#include<stdio.h> Binary number: 1110
void main(O
ginclude math.h>
#include<stdio.h>
void main()
void main(O
int n, i
int n, ud, i=0, a(30], dec=0;
printf("enter a number\n);
printf("enter a binary number\n");
scanf("%d", &n);
scant("%d", &n); printf("factors are:");
while(n!=0) for(i=1; i<=n ;i++)
ud=n%10; if(n%i==0)
=dec+ud"pow(2,i);
n=n/10; printf("%d\t", i);
i++
#include<stdio.h>
void main()
printf(%d"a); 12
printf("%d".b);
32
printf("%0",a); //octal number of a
printf("%o",b); //octal number of b 14
printf("%x",a); //Hexadecimal value of a
1B
printf(%ox",b); //Hexadecimal value of b
sum=sum+i;
if(count==2)
printf("%d is a prime number", n);
else if(sum==n)
printf("%d is a perfect number", n);
printf("%%d is not prime number", n);
else
printf("%d is not a perfect number",
|
n);
Output: Output:
enter a number enter a number
9 6
9 is not a prime number 6Is a pertect number
Output: Output:
enter a number enter a number
7 28
7 is a prime number 28 is a perfect number
Output: Output:
enter a number enter a number
15 15
15 is not a prime number 15 is not a
perfect number
Output: Output:
Enter a number Enter a number
5
Sum=15 factorial=120
min=ali if(max<ali))
printf("Minimum element=%d", min); max=al
TUMKUR
SSPIDERS SOFTWARE TRAINING INSTITUTE,
sort in array
hble
Buht
Selection sort in array
includesstdio.h. ».h>
#includesstdio.h>
voidmain( Void main()
int a 30,
n, 1,), temp; int al30], n, i, j, temp, min;
p
intf("ente
i n t
ber of elenments\n"); printf('enter number of elements
\n)
scant(od", &n); scanf("%d", &n):
printf('enter od elements\n",n); printf("enter %d elements\n",n);
for(i=0; isn ;i++) for(i=0; i<n ;i++)
scanf("%d", &alil); Scanf("%d", &ali]);
for(i=0;i<n-1 :i++) fori-0;i<n-1;i++)
{
forj=0; jsn-i-1; j++) min=i
forj=i+1:j<n; j++)
iffalj]>alj+1])
if(afmin]alil)
temp= aj:
alil= alj+1]: min=
alj+1] =temp;
temp= a[min];
a[min]= ali}:
printf("Sorted array is\n"); ali =temp
for(i=0; i<n ;i++)
printf("%d\t", afi]): printf("Sorted array is\n");
forfi=0; i<n;i++)
printf("%d\t", afi]);
Output:
Enter number of elements Output:
Enter number of elements
4
Eneter 4 elements 4
3
Eneter 4 elements
9
6
2 4
2
Sorted array is
Sorted array is
2 3 6 9 1 2 4 9
Output:
Enter number of elements
Output: 4
Enter number of elements
Eneter 4 elements
5
3
Eneter 5 elements
6
3
2
array elements are
8 3 69 2
Sorted array is
Reverse array isS
98 6 3 2 29 6 3
#include<stdio.h>
#include<stdio.h>
Output:
Enter number of elements Output:
Enter number of elements
4
4
Enter 4 elements
Enter 4 elements
3
3
6
6
9
2
2
enter index to insert element
2 enter index to delete elenment
3
enter data to insert
10
Resultant array is
3 6 2
Resultant array is
3 10 6 9 2
f3=f1+f2;
#include<stdio.h>
void main()
printf("%d\", 3);
f1-f2;
f2-3;
int a, b, HCF, LCM,i;
printr'enter 2 numbers \n");
Output:
LCM=(a*b)/HCF; Enter 2 numbers
printf("HCF=%d\n", HCF); 8
printf("LCM=%d\n", LCM); 10
HCF=2
LCM=40
void main()
poid
main(
c[50|[50
b[50||50|,
int al50||50J,
50]150],
50], c[50]|50];
int
int m, n, 1, J, K;
nm,
j;:
n, i, printt("enter
row size \n);
int ("enter row size\");
scanf("%d", &m);
size\n");
p r i n t f
s c a n t%
( d " ,&m):
column
printf"enter
printf("enter column size\n");
scant("%d", &n);
is \n")
printt("er
elements
scant("od, &n); matrix
printf("enter A
matriX elements is\n"):
A
nrintf("enter for(i=0; i<m ;it+)
for(i=0; ism ;i++) tor(j=0; j<n:j++)
forj=0: jsn;j++) scanf("%d", &a[i][il);
elements is\n );
scant("%d, &ali]0)): B matrix
printf('enter
matrix elements is\n");
B for(i-0; i<m;i++)
printf("enter
for(i=0; ism ;i++) for(j=0; j<n:j++)
forj=0: j<n;j++) scanf("%d", &bli]li]):
scanf("%d", &b[i|5); for(i=0; i<m ;it+)
printt("\n");
TUMKUR
INSTITUTE,
TRAINING
DERS SOFTWARE
Square root of a number Cube root of a number
#include<stdio.h> #include<stdio.h>
#include<math.h> #include<math.h>
void main() void main()
Output: Output:
Enter a number Enter a number
25 64
square root of 25 =5 cube root of 64 =4
S
E