0% found this document useful (0 votes)
4 views

cprogram

Simple Programs in c language

Uploaded by

mmunavar66
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)
4 views

cprogram

Simple Programs in c language

Uploaded by

mmunavar66
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/ 6

ARMSTRONG NUMBER

AIM:
Write a C program to Check whether given number
is Armstrong Number or Not.
ALGORITHM:
1: start
2:read n
3:assign sum=0, i, m, n, count=0
4:if m>0, repeat the following
4.1:m=m/10
4.2:count++
.3:until the condition fail
5: if i>0, repeat step 4 until condition fails
5.1:rem=I%10
5.2:sum= sum+pow(rem,count)
5.3:I=I/10
6: if n=sum, then print Armstrong
7: Otherwise, print not Armstrong
8:stop
PROGRAM:
#include <stdio.h>
int main()
{
int n, n1, rem, num=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if (num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
LINEAR SEARCH
AIM:Write a C program to Implement Linear Search.
ALGORITHM:
Step 1: start
Step 2 : Initialize array a.
Step 3 : Read n
Step 4 : Set i=0 repeat step 5,6 until i<n.
Step 5 : Read a[i]
Step 6 : i++
Step 7 : Read s
Step 8 : Set i=0 repeat step 9 to 11 until i<n.
Step 9 : if a[i] equal to s then
Step 10 : Print s,i+1
Step 11 : i++
Step 12 : if a[i] equal to n then
Step 13 : Print Not present.
Step 14 : Stop
PROGRAM:
#include <stdio.h>
int main()
{
int a[100], s, i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Enter a number to search\n");
scanf("%d", &s);
for (i= 0; i < n; i++)
{
if (a[i] == s)
/* If required element is found */
{
printf("%d is present at location %d.\n", s, i+1);
break;
}}
if (i == n)
printf("%d isn't present in the array.\n", s); return 0;
}
SELECTION SORT
AIM:
Write a C program to implement selection sort.
ALGORITHM:
Step 1 : Set min to the first location
Step 2 : Search the minimum element in the array
Step 3 : swap the first location with the minimum value in the array
Step 4 : assign the second element as min.
Step 5 : Repeat the process until we get a sorted array.
PROGRAM:
#include <stdio.h>
int main()
{
int a[100], n, c, d, position, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &a[c]);
for (c = 0; c < (n - 1); c++)
// finding minimum element (n-1) times
{
position = c;
for (d = c + 1; d < n; d++)
{
if (a[position] > a[d])
position = d;
}
if (position != c)
{
t = a[c];
a[c] = a[position];
a[position] = t;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
STRING PALINDROME
AIM:
Write a C program to find palindrome using string.
ALGORITHM:
Step 1: Start
Step 2: Declare the variables i, n, c, s
Step 3: Read the string
Step 4: Store the length to variable n using function strlen()
Step 5: Using for loop, check if (s[p]==s[n-i-1]), along incrementing c
Step 6: If True, print “String is Palindrome”
Step 7: Otherwise, print “String is not Palindrome”
Step 8: Stop
PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000];
int i,n,c=0;
printf("Enter the string : ");
scanf("%s",s);
n=strlen(s);
printf("Length of the string is %d\n",n);
for(i=0;i<n/2;i++)
{
if(s[i]==s[n-i-1])
c++;
}
if(c==i)
printf("string is palindrome");
else
printf("string is not palindrome");
return 0;
}
POINTER AS FUNCTION ARGUMENTS
AIM:
Write a C program to use pointer as function arguments.
ALGORITHM:
Step 1: Start
Step 2: Initialize the variables a,b,temp
Step 3: Declare the function swap(int *n1, int *n2)
Step 4: Read the variables a, b
Step 5: Initialize temp=*n1;
*n1=*n2;
*n2=temp;
Step 6: Read the numbers before swapping
Step 7: Call the function swap(&a, &b)
Step 8: Read the numbers after swapping
Step 9: Stop
PROGRAM:
#include<stdio.h>
void swap(int*n1,int*n2)
{
int temp;
temp=*n1;
*n1=*n2;
*n2=temp;
int main()
{
int a,b;
printf("Enter Two Numbers: \n");
scanf("%d%d",&a,&b);
printf("\nNumbers Before Swapping: \n");
printf("\n a=%d b=%d\n",a,b);
swap(&a,&b);
printf("\nNumbers After Swapping: \n");
printf("\n a=%d b=%d",a,b);
return 0; }
SMALLEST ELMENT IN AN ARRAY USING POINTER
AIM:
Write a C program to find smallest element in array using pointer.
ALGORITHM:
Step 1: Start
Step 2: Initialize the variables *s, i, small, a[5]
Step 3: Set s=&a[0]
Step 4: Read 5 elements
Step 5: Use for loop (i=0;i<5;i++;s++)
Step 6: If s=&a[0]; then small=*s
Step 7: Use for loop (i=0;i<5;i++;s++)
Step 8: Check if(*s<small), then small=*s
Step 9: Print the smallest element
Step 10: Stop
PROGRAM:
#include<stdio.h>
int main()
{
int a[5],*s,i,small;
s=&a[0];
printf("Enter 5-Elements :\n\n ");
for(i=0;i<5;i++,s++)
scanf("%d",s);
s=&a[0];
small=*s;
for(i=0;i<5;i++,s++)
if(*s<small)
small=*s;
printf("\nSmallest Element : %d",small);
return 0;
}

You might also like