DAY1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 48

1

1)PROGRAM:
To check the number is armstrong or not.

CODE:
#include<stdio.h>
#include<math.h>
int main()
{
int n,r,sum=0,temp,a,c;
printf("enter the number=");
scanf("%d",&n);
temp=n;
a=n;
while(a>0)
{
a=a/10;
c++;
}
while(n>0)
{
r=n%10;
sum+=pow(r,c);
n=n/10;
}

if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
2

return 0;
}

OUTPUT:
3

2)PROGRAM:
4

CODE:

#include <stdio.h>
void function(int min); //frequent counter method or counter
method
int main()
{
int n;
scanf("%d",&n);
function(n);
return 0;

void function(int n)
{

int count=0;
int i=1,s=1;
count++;
count++;
while(s<=n)
{

count++;
5

i++;
count++;
s+=i;
count++;
}

count++;// after every loop an additional condition is checked


hence this count is incremented
printf("%d",count);

OUTPUT:
6
7

3)PROGRAM:

CODE:

#include <stdio.h>
void function(int n);
int main()
{
8

int n;
scanf("%d",&n);
function(n);
return 0;
}

void function(int n)
{

int count=0;
if(n==1)
{

count++;
count++;
}

else
{

count++;
for(int i=1;i<=n;i++)
{

count++;
9

for(int j=1;j<=n;j++)
{

count++;
count++;
count++;
count++;
break;
}

}
count++;
}

printf("%d",count);

}
10

OUTPUT:
11

4)PROGRAM:

CODE:

#include <stdio.h>

int factor(int n);

int count=0;
12

int main()

int n;

scanf("%d",&n);

factor(n);

printf("%d",count);

return 0;

int factor(int n)

int i;

count++;

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

count++;

if(n%i==0)

//print

}count++;

count++;

return 0;

}
13

OUTPUT:
14

5)PROGRAM:

CODE:

#include <stdio.h>

void function(int n);

int main()

int n;
15

scanf("%d",&n);

function(n); return 0;

void function(int n)

int count=0;

int c=0;

count++;

for(int i=n/2;i<n;i++)

count++;

for(int j=1;j<n;j=2*j)

count++;

for(int k=1;k<n;k=k*2)

count++;

c++;

count++;

}count++;
16

}count++;

}count++;

printf("%d",count);

OUTPUT:
17

6)PROGRAM:

CODE:

#include <stdio.h>

void reverse(int n);


18

int main()

int n;

scanf("%d",&n);

reverse(n);

return 0;

void reverse(int n)

int count=0;

int rev=0,remainder;

count++; while(n!

=0)

count++; remainder=n

%10; count++;

rev=rev*10+remainder;

count++;

n=n/10;

count++;

}
19

count++;

count++;

printf("%d",count);

OUTPUT:
20

7)PROGRAM:

Write a C program to perform Strassen’s Matrix Multiplication for


the 2*2 matrix elements and Estimate time complexity.

CODE:

#include<stdio.h>

int main(){

int a[2][2], b[2][2], c[2][2], i, j;

int m1, m2, m3, m4 , m5, m6, m7;

printf("Enter the 4 elements of first matrix: ");

for(i = 0;i < 2; i++)

for(j = 0;j < 2; j++)

scanf("%d", &a[i][j]);

printf("Enter the 4 elements of second matrix: ");

for(i = 0; i < 2; i++)


21

for(j = 0;j < 2; j++)

scanf("%d", &b[i][j]);

printf("\nThe first matrix is\n");

for(i = 0; i < 2; i++){

printf("\n");

for(j = 0; j < 2; j++)

printf("%d\t", a[i][j]);

printf("\nThe second matrix is\n");

for(i = 0;i < 2; i++){

printf("\n");

for(j = 0;j < 2; j++)

printf("%d\t", b[i][j]);

}
22

m1= (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]);

m2= (a[1][0] + a[1][1]) * b[0][0];

m3= a[0][0] * (b[0][1] - b[1][1]);

m4= a[1][1] * (b[1][0] - b[0][0]);

m5= (a[0][0] + a[0][1]) * b[1][1];

m6= (a[1][0] - a[0][0]) * (b[0][0]+b[0][1]);

m7= (a[0][1] - a[1][1]) * (b[1][0]+b[1][1]);

c[0][0] = m1 + m4- m5 + m7;

c[0][1] = m3 + m5;

c[1][0] = m2 + m4;

c[1][1] = m1 - m2 + m3 + m6;

printf("\nAfter multiplication using Strassen's algorithm \n");

for(i = 0; i < 2 ; i++){

printf("\n");

for(j = 0;j < 2; j++)


23

printf("%d\t", c[i][j]);

return 0;

OUTPUT:

8)PROGRAM:

Write a program to search a number in a list using binary search and


24

estimate time complexity

CODE:

#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
int count=0;
printf("Enter number of elements");
scanf("%d",&n);
printf("Enter %d integers", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find");
scanf("%d", &key);
low = 0;
count++;
high = n - 1;
count++;
mid = (low+high)/2;
count++;
while (low <= high) {
count++;
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
count++;
printf("%d found at location %d ", key, mid+1);
break;
}
else
25

high = mid - 1;
mid = (low + high)/2;
count++;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
printf("time complexity : %d",count);
return 0;
}

OUPUT:

9)PROGRAM:
26

Write a program to search a number in a list using linear search and


estimate time complexity

CODE:
#include<stdio.h>
int main()
{
int array[100], search, c, n;
int count=0;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
{
count++;
scanf("%d", &array[c]);
}
count++;
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
count++;
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
27

count++;
if (c == n)
printf("%d isn't present in the array.\n", search);
printf("%d",count);

return 0;
}

OUTPUT:

10)PROGRAM:

Compute the program to find the GCD of two numbers. And also find the
28

finf of time Recursion used to estimate time complexity.

CODE:
#include <stdio.h>
int main()
{
int n1, n2, i, GCD_Num;
int count=0;
printf ( " Enter any two numbers: \n ");
scanf ( "%d %d", &n1, &n2);

for( i = 1; i <= n1 && i <= n2; ++i)


{
count++;
if (n1 % i ==0 && n2 % i == 0)
GCD_Num = i;
count++;
}
count++;

printf ("gcd of two numbers %d and %d is %d ", n1, n2, GCD_Num);


printf("time complexity :%d ",count);
return 0;
}

OUTPUT:
29

11) Generate a program for Pascal triangle.


Estimate the time complexity for the row=5
30

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Code:
#include<stdio.h>
int main()
{
int rows, coef = 1, space, i, j;
int count=0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 0; i<rows; i++)
{
count++;
for (space = 1; space <= rows - i; space++)
printf(" ");
count++;
for (j = 0; j <= i; j++)
{
count++;
31

if(j == 0 || i == 0){
coef = 1;
count++;
}

else
{
coef = coef * (i - j + 1) / j;
}
count++;
printf("%4d", coef);
}
printf("\n");
count++;
}
printf("%d",count);
return 0;
}

OUTPUT:
32

12) PROGRAM:

Write a program to find the largest element value in an array. Estimate the
33

time complexity and no of comparison for the given set of values

CODE:
#include <stdio.h>
int main() {
int n;
int count=0;
double arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
count++;
for (int i = 0; i < n; ++i) {
count++;
printf("Enter number%d: ", i + 1);
scanf("%lf", &arr[i]);
}
for (int i = 1; i < n; ++i) {
count++;
if (arr[0] < arr[i]) {
arr[0] = arr[i];
}
count++;
}

printf("Largest element = %.2lf ", arr[0]);


printf("%d",count);
34

return 0;
}
OUTPUT:

13) PROGRAM:
35

Write a program to find the factorial (fact)of a number and to estimate


time complexity.
Condition such as i. n=0, return 1 otherwise fact (n-1) * n

CODE:
#include <stdio.h>
int main() {
int n, i;
int count=0;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
count++;
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
count++;
}
printf("Factorial of %d = %llu ", n, fact);
printf(" time compexity : %d ",count);
}

return 0;
}
36

OUTPUT:

14)PROGRAM:

Write a program to print the first n perfect numbers. (Hint Perfect number
37

means a positive integer that is equal to the sum of its proper divisors)

Sample Input:
N=3
Sample Output:
First 3 perfect numbers are: 6 , 28 , 496

CODE:
#include <stdio.h>
#include<math.h>
int count=0;
int isPerfect(long long int n) {

long long int dsum = 0;


long long int i;
count++;
for (i = 1; i <= sqrt(n); ++i) {
count++;
if (n % i == 0) {
count++;
if (i == n / i) {
dsum += i;
}
else {
dsum += i;
dsum += n / i;
count++;
}
count++;
}
count++;
}
count++;
dsum = dsum - n;
count++;
if (dsum == n) return 1;
else return 0;
}
38

int isPrime(long long int n) {

if (n == 1)
return 0;

for (int i = 2; i <= sqrt(n); ++i) {


count++;
if (n % i == 0)
return 0;
}
return 1;
count++;
}
int main() {
long long int n, i, temp;
printf("Enter n: ");
scanf("%d", &n);
count++;
i = 1;
while (n > 0) {
count++;
if (isPrime(i) == 1) {
temp = pow(2, i - 1) * (pow(2, i) - 1);
count++;
if (isPerfect(temp) == 1) {
printf("%d ", temp);
n = n - 1;
count++;
}
}
i = i + 1;
count++;
}
printf("\n");
printf("%d",count);
}

OUTPUT:
39

15) PROGRAM:

Write a C program to check whether is a given input is a palindrome


40

CODE:
#include <stdio.h>
#include <string.h>
int main(){
char string1[20];
int i, length;
int flag = 0;
int count=0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
count++;
for(i=0;i < length ;i++){
count++;
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
count++;
}
count++;
}
count++;
if (flag) {
printf("%s is not a palindrome ", string1);
}
else {
printf("%s is a palindrome ", string1);
}
printf("time complexity : %d",count);
return 0;
}

OUTPUT:
41

16)PROGRAM:

Write a program to perform Bubble sort and estimate time Complexity


42

CODE:

#include<stdio.h>

void main(){
int ele,count=0;

printf("Enter tot element: ");


scanf("%d",&ele);

int arr[ele];
printf("Enter the elements: ");

for (int i = 0; i < ele; i++){


count++;
scanf("%d",&arr[i]);
}count++;

for (int i = 0; i < ele; i++)


{
count++;
for (int j =i+1; j < ele; j++)
{
count++;
if (arr[i]>arr[j])
{
count++;
int temp=arr[i];
43

count++;
arr[i]=arr[j];
count++;
arr[j]=temp;
count++;
}
}count++;

}count++;

printf("sorted array: ");


for (int i = 0; i < ele; i++)
{count++;
count++;
printf("%d ",arr[i]);
}count++;
printf("count: %d",count);
}

OUTPUT:
44

17) PROGRAM:

Write a program to check sub string is there in a string or not.

CODE:
45

#include<stdio.h>

int main()
{
char str[80], search[10];
int count1 = 0, count2 = 0, i, j, flag;
int count=0;
printf("Enter a string:");
gets(str);
printf("Enter search substring:");
gets(search);
while (str[count1] != '\0')
count1++;
while (search[count2] != '\0')
count2++;
for (i = 0; i <= count1 - count2; i++)
{
count++;
for (j = i; j < i + count2; j++)
{
count++;
flag = 1;
if (str[j] != search[j - i])
{
count++;
flag = 0;
break;
}
count++;
}
if (flag == 1)
break;
count++;
}
count++;
if (flag == 1)
printf("found");
46

else
printf("not found");
printf("%d",count);
}

OUTPUT:

18)PROGRAM:

Write a program to generate all the reverse of a prime should be prime


47

( for example 907 is prime and reverse 709 is also prime )


Generate all the no’s upto N and estimate time complexity.

CODE:

#include <stdio.h>
int main()
{
int n, reverse,sum=0 , flag;
printf("Enter the prime number?");
scanf("%d",&n);
while(n!=0)
{
reverse = n%10;
sum = sum*10 + reverse;
n= n/10;
}
printf("\n");
flag = 0;
for (int j = 2; j <= sum / 2; j++)
{
if ((sum % j) == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is also prinme number",sum);
else
printf("Not Prime number");
}
48

OUPUT:

You might also like