DAY1
DAY1
DAY1
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++;
}
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 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)
}count++;
count++;
return 0;
}
13
OUTPUT:
14
5)PROGRAM:
CODE:
#include <stdio.h>
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>
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:
CODE:
#include<stdio.h>
int main(){
scanf("%d", &a[i][j]);
scanf("%d", &b[i][j]);
printf("\n");
printf("%d\t", a[i][j]);
printf("\n");
printf("%d\t", b[i][j]);
}
22
c[0][1] = m3 + m5;
c[1][0] = m2 + m4;
c[1][1] = m1 - m2 + m3 + m6;
printf("\n");
printf("%d\t", c[i][j]);
return 0;
OUTPUT:
8)PROGRAM:
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
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
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);
OUTPUT:
29
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
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++;
}
return 0;
}
OUTPUT:
13) PROGRAM:
35
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) {
if (n == 1)
return 0;
OUTPUT:
39
15) PROGRAM:
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:
CODE:
#include<stdio.h>
void main(){
int ele,count=0;
int arr[ele];
printf("Enter the elements: ");
count++;
arr[i]=arr[j];
count++;
arr[j]=temp;
count++;
}
}count++;
}count++;
OUTPUT:
44
17) PROGRAM:
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:
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: