0% found this document useful (0 votes)
57 views7 pages

C Programs for Common Algorithms

The document outlines a series of C programming tasks including generating prime numbers, checking for Armstrong numbers, verifying palindromes, printing leap years, and more. Each task includes an aim, algorithm, and coding example. These programs are designed to enhance understanding of basic programming concepts and functions in C.

Uploaded by

madhupinky2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views7 pages

C Programs for Common Algorithms

The document outlines a series of C programming tasks including generating prime numbers, checking for Armstrong numbers, verifying palindromes, printing leap years, and more. Each task includes an aim, algorithm, and coding example. These programs are designed to enhance understanding of basic programming concepts and functions in C.

Uploaded by

madhupinky2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Programs

1. Write a C program to generate “n” prime numbers.


2. Write a C program to check whether the entered number is Armstrong Number or not.
3. Write a C program to check whether the given number is Palindrome or not.
4. Write a C program to print all leap years from 1 to N.
5. Write a C program to print the Fibonacci series up to n numbers.
6. Write a C program to print the multiplication table of the derived numbers from 1 to 20 count.
7. Write a C Program to sort the given set of numbers in ascending order.
8. Write a C program to perform following string handling functions
a) Find the Length of the string. b) Reverse a String.
c) Concatenation of two strings. d) Compare two strings.
9. Write a C program to find the factorial of a given number using Recursive function.
10. Write a C program to generate patterns by using Symbols and Numbers.

1. Write a C program to generate “n” prime numbers.


Prime Number Generation
AIM:
To write a C program to generate ‘n’ prime numbers.
ALGORITHM:
Step 1: Start the C program.
Step 2: Declare the necessary variables.
Step 3: Get the number of prime numbers that are to be printed.
Step 4: Check the availability of prime numbers within the specified limit by checking the
condition.
Step 5: The non-prime numbers are to be skipped and the prime numbers alone are to be
displayed one after the other.
Step 6: Compile and execute the program.
Step 7: Print the result.
Step 8: Stop the C program.
Coding:
#include <stdio.h>
int main()
{
int n, i = 3, count, c;
printf("Enter the number of prime numbers to print\n");
scanf("%d", &n);

if (n >= 1)
{
printf("First %d prime numbers are:\n",n);
printf("2\n");
}

for (count = 2; count <= n;)


{
for (c = 2; c <= i - 1; c++)
{
if (i%c == 0)
break;
}
if (c == i)
{
printf("%d\n", i);
count++;
}
i++;
}

return 0;
}
2. Write a C program to check whether the entered number is Armstrong Number or not.
Armstrong Number
AIM:
To write a C program to check whether the entered number is Armstrong Number or not.
ALGORITHM:
Step 1: Start the C program.
Step 2: Declare and initialize the variables.
Step 3: Get the input value n and assign that to temporary variable temp.
Step 4: Check the necessary condition using while loop.
Step 5: Use if statement to check whether the number is Armstrong number or not.
Step 6: Compile and execute the program.
Step 7: Print the result.
Step 8: Stop the C program.

Coding :

#include<stdio.h>
Void main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
}
3. Write a C program to check whether the given number is Palindrome or not.

PALINDROME CHECKING
AIM:
To write a C program to check whether the given number is Palindrome or not.
ALGORITHM:
Step 1: Start the C program.
Step 2: Declare the necessary variables.
Step 3: Obtain an integer number as an input from the user.
Step 4: Perform the calculations among the declared variables and reverse the
number.
Step 5: Compare them both and categorize the result either as a palindrome number
or a non-palindrome number.
Step 6: Compile and execute the program.
Step 7: Print the result.
Step 8: Stop the C program.
Coding :

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,rev=0,temp;
printf("Enter an Integer:");
scanf("%d",&n);
temp=n;
while(n)
{
r=n%10;
n=n/10;
rev=rev*10+r;
}
printf("\n Given number is:%d",temp);
printf("\n Reversed number is:%d",rev);
if(temp==rev)
{
printf("\n The number is a Palindrome!");
}
else
{
printf("\n The number is not a Palindrome!");
}
getch();

}
4. Write a C program to print all leap years from 1 to N.

LEAP YEARS FROM 1 TO N


AIM:
To write a C program to print all leap years from 1 to N.
ALGORITHM:
Step 1: Start the C program.
Step 2: Declare and define a user defined function
Step 3: Declare the necessary variables.
Step 4: Obtain the value of n from the user.
Step 5: Plot out the leap years from 1 to the specified year.
Step 6: Compile and execute the program.
Step 7: Print the result.
Step 8: Stop the C program.

Coding:
#include <stdio.h>
#include<conio.h>
int checkLeapYear(int year)
{
if( (year % 400==0)||(year%4==0 ) )
{
return 1;
}
else
{
return 0;
}
}
void main()
{
int i,n;
printf("Enter the value of N: ");
scanf("%d",&n);
printf("\n Leap years from 1 to %d are:\n",n);
for(i=1;i<=n;i++)
{
if(checkLeapYear(i))
{
printf("%d\t",i);
}
}
getch();
}

You might also like