0% found this document useful (0 votes)
55 views11 pages

C Important Programs

The document provides code snippets for 17 different programming problems involving arrays, numbers, and mathematical operations in C language. The problems include calculating simple and compound interest, finding the hypotenuse of a triangle, addition of two numbers, finding the sum and minimum element of an array, checking if a number is abundant or semi-perfect, and more.

Uploaded by

navi
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)
55 views11 pages

C Important Programs

The document provides code snippets for 17 different programming problems involving arrays, numbers, and mathematical operations in C language. The problems include calculating simple and compound interest, finding the hypotenuse of a triangle, addition of two numbers, finding the sum and minimum element of an array, checking if a number is abundant or semi-perfect, and more.

Uploaded by

navi
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/ 11

C IMPORTANT PROGRAMS -HCL(HINDUSTAN COMPUTERS LIMITED)

1. SIMPLE INTEREST
#include <stdio.h>
int main()
{
float principle, time, rate, SI;

/* Input principle, rate and time */


printf("Enter principle (amount): ");
scanf("%f", &principle);

printf("Enter time: ");


scanf("%f", &time);

printf("Enter rate: ");


scanf("%f", &rate);

/* Calculate simple interest */


SI = (principle * time * rate) / 100;

/* Print the resultant value of SI */


printf("Simple Interest = %f", SI);

return 0;
}
2. COMPOUND INTEREST
#include <stdio.h>
#include <math.h>

int main()
{
float principle, rate, time, CI;

/* Input principle, time and rate */


printf("Enter principle (amount): ");
scanf("%f", &principle);

printf("Enter time: ");


scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);

/* Calculate compound interest */


CI = principle* (pow((1 + rate / 100), time));

/* Print the resultant CI */


printf("Compound Interest = %f", CI);

return 0;
}

3. Pythagorean Theorem

#include<stdio.h>

#include<math.h>

int main()

int a,b,h;

printf("Enter a,b");

scanf("%d %d",&a,&b);

h= sqrt ((a*a) + (b*b));

printf("hypotenouse is %d",&h);

return 0;

4.Addition of two numbers

#include <stdio.h>

int main() {

int a, b, result;

printf("Enter first number : ");

scanf("%d", &a);
printf("Enter second number : ");

scanf("%d", &b);

//add two numbers

result = a + b;

printf("Sum : %d\n", result);

return 0;

5. SUM OF ARRAY ELEMENTS

#include <stdio.h>

int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int sum, loop;

sum = 0;

for(loop = 9; loop >= 0; loop--) {


sum = sum + array[loop];
}

printf("Sum of array is %d.", sum);

return 0;
}

6.FACTORIAL

#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
7.Smallest of two numbers
#include<stdio.h>
int main()
{
int a,b,small;
printf("Enter a,b numbers: ");
scanf("%d %d",&a,&b);
small = (b>a) ? a : b;
printf("Smallest value is %d",small);
return 0;
}
8.Find the minimum element in the array
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],n,i,min;
printf("Enter num of variables to be used: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the value of a[%d]",i);
scanf("%d",&a[i]);
}
min=a[0];
for(i=1;i<n;i++)
{
if(a[i]<min)
{
min=a[i];
}
}
printf("The min value is : %d\n ",min);
9.SUM OF TWO LARGEST NUMBERS IN ARRAY
def findlargestsum(arr,n):
if arr[0]>arr[1]:
first=arr[0]
second=arr[1]
else:
first=arr[1]
second=arr[0]
for i in range(2,n):
if arr[i]>first:
second=first
first=arr[i]
elif arr[i]>second and arr[i]!= first:
second=arr[i]
return (first+second)
n = int(input("Enter number of elements : "))
arr = list(map(int,input("\nEnter the numbers : ").strip().split()))[:n]
print("Sum is",findlargestsum(arr,n))

(OR)
def find_len(list1):
length = len(list1)
list1.sort()
print("Largest element is:", list1[length-1])
print("Second Largest element is:", list1[length-2]

list1=[12, 45, 2, 41, 31, 10, 8, 6, 4]


Largest = find_len(list1)
10.EQUILIBRIUM POSITION
def equilibrium(arr):
leftsum = 0
rightsum = 0
n = len(arr)

# Check for indexes one by one


# until an equilibrium index is found
for i in range(n):
leftsum = 0
rightsum = 0

# get left sum


for j in range(i):
leftsum += arr[j]

# get right sum


for j in range(i + 1, n):
rightsum += arr[j]

# if leftsum and rightsum are same,


# then we are done
if leftsum == rightsum:
return i

# return -1 if no equilibrium index is found


return -1

# driver code
arr = list(map(int,input().split(",")))
print (equilibrium(arr))

11.COUNT EVEN NUMBERS & SUM IN AN ARRAY


a=list(map(int , input().split()))
res=0
c=0
for i in a:
if (i%2==0):
res+=i
c=c+1
print(res)
print(c)

12.POSITIVE MIDDLE ELEMENT IN AN ARRAY


n = int(input("Enter number of elements : "))
a = list(map(int,input("\nEnter the numbers : ").strip().split()))[:n]
a.sort()
print("mid value is",a[int(len(a)/2)])

13.ARRAY A IN ELEMENT B BUT NOT IN C


def AinBbutnotinc(a,b,c):
res=[]
for i in a:
if i in b:
if i not in c:
res.append(i)
return res
a=list(map(int,input().split(",")))
b=list(map(int,input().split(",")))
c=list(map(int,input().split(",")))
print(AinBbutnotinc(a,b,c))

14.MISSING ELEMENT IN AN ARRAY


def getMissingNo(arr, n):
total = (n + 1)*(n + 2)/2
sum_of_A = sum(arr)
return total - sum_of_A

# Driver code
if _name_ == '_main_':
arr = list(map(int,input().split(",")))
N = len(arr)

# Function call
miss = getMissingNo(arr, N)
print(miss)

15.PRODUCT OF NUMBERS IN AN INTEGER ARRAY


import numpy
list1 = list(map(int,input().split(",")))
# using numpy.prod() to get the multiplications
result1 = numpy.prod(list1)
print(result1)

16.WEIRED NUMBER
from math import sqrt
from itertools import combinations as z
def factors(n):
v = []
v.append(1)

for i in range(2, int(sqrt(n)) + 1, 1):

if (n % i == 0):
v.append(i);

if (int(n / i) != i):
v.append(int(n / i))

return v

def checkAbundant(n):
sum = 0

v = factors(n)

for i in range(len(v)):
sum += v[i]
if (sum > n):
return True
else:
return False

def checkSemiPerfect(n):

v = factors(n)
l=[]
if sum(v)==n:
return True
else:
j=len(v)
for i in range(2,j-1):
k=z(v,i)
#print(list(k))
h=list(k)
for i in h:
if(sum(i)==n):
return True
return False

def checkweird(n):
if (checkAbundant(n) == True and
checkSemiPerfect(n) == False):
return True
else:
return False

if name == 'main':
o=[70, 836, 4030, 5830, 7192, 7912, 9272, 10430, 10570, 10792, 10990, 11410, 11690,
12110, 12530, 12670, 13370, 13510, 13790, 13930, 14770]
for ii in o:
n = ii
if (checkweird(n)):
print("Weird Number")
else:
print("Not Weird Number")

17.COUNT OF SUBLIST
a=list(map(int,input().split()))
Print(“subset”,2**len(a))
Print(“proper subset “,(2**len(a)-1))

You might also like