SlideShare a Scribd company logo
1. Write a program to checka givennumberisprime ornot.
Algorithm:
Step1: Start
Step2: Declare variablesn,i,prime.
Step3: Initialize variables
prime←0
i←2
Step4: Readn fromuser.
Step5: Repeatthe stepsuntil i<=(n/2)
5.1 If remainderof n÷i equals0
prime←1
Go to step6
5.2 i←i+1
Step6: If prime=0
Displayn isprime
else
Displayn isnot prime
Step7: Stop
Source Code:
#include<stdio.h>
voidmain()
{inti,n,prime=0;
printf("Enteranynumber:");
scanf("%d",&n);
i=2;
while(i<=n/2)
{if(n%i==0)
{prime=1;
break;
}
i++;
}
if(prime==0)
printf("nItisa prime number");
else
printf("nItisnota prime number");
}
Output:
Discursion:
If the while loop terminates when the test expression of loop i <= n/2 is false, the entered
number is a prime number. The value of prime is equal to 0 in this case.
2. Write a c program to check whether a number is a krishnamurty number or not
Algorithm:
1. Input a number from user to check for strong number. Store this in a variable say num.
Copy it to a temporary variable for calculations purposes, say originalNum = num.
2. Initialize another variable to store sum of factorial of digits, say sum = 0.
3. Find last digit of the given number num. Store the result in a variable say lastDigit =
num % 10.
4. Find factorial of lastDigit. Store factorial in a variable say fact.
5. Add factorial to sum i.e. sum = sum + fact.
6. Remove last digit from num as it is not needed further.
7. Repeat steps 3 to 6 till num > 0.
8. After loop check condition for strong number. If sum == originalNum, then the given
number is Strong number otherwise not.
#include <stdio.h>
voidmain()
{
int i,originalNum,num,lastDigit,sum;
longfact;
printf("Enteranynumbertocheckkrishnamurtynumber:");
scanf("%d",&num);
originalNum=num;
sum = 0;
while(num>0)
{
lastDigit=num %10;
fact= 1;
for(i=1;i<=lastDigit;i++)
{
fact = fact * i;
}
sum= sum+ fact;
num= num/ 10;
}
if(sum== originalNum)
{
printf("%diskrishnamurtyNUMBER",originalNum);
}
else
{
printf("%disNOTkrishnamurtyNUMBER",originalNum);
}
}
3. Fibonacci seriesusingrecursive functioninC
#include<stdio.h>
intFibonacci(int);
intmain()
{
intn, i = 0, c;
printf("nEnterhowmanytermyouwantin Fibonacci series:");
scanf("%d",&n);
printf("nFibonacci series:");
for ( c = 1 ; c <= n ; c++ )
{
printf("%dt",Fibonacci(i));
i++;
}
}
intFibonacci(intn)
{
if ( n == 0 )
return0;
else if ( n == 1 )
return1;
else
return( Fibonacci(n-1) +Fibonacci(n-2) );
}
Discursion:
Fibonacci series is a sequence of following numbers
0 1 1 2 3 5 8 13 ....
It adds previous two numbers value to compute the next number value. In this program
fibonacci series is calculated using recursion, with seed as 0 and 1. Recursion means a
function calling itself, in the below code fibonacci function calls itself with a lesser value
several times. An termination condition is very important to recursion function, i.e n == 0
and n == 1 or the recursive call would be infinite leading to stack overflow error.
4. C Program to print array in reverse
Algorithm:
1. Input size and elements in an array. Store it in some variable
say size and arr respectively.
2. Declare another array that will store reversed array elements of original array with same
size, say reverse[size].
3. Initialize two variables that will keep track of original and reverse array. Here we will
access original array from last and reverse array from first. Hence, initialize arrIndex =
size - 1 and revIndex = 0.
4. Run loop from size - 1 to 0 in decremented style. The loop structure should look
like while(arrIndex >= 0).
5. Inside loop copy original array to reverse array i.e. reverse [revIndex] = arr[arrIndex];.
6. After copy, increment revIndex and decrement arrIndex.
7. Finally after loop print reverse array.
Code:
#include <stdio.h>
voidmain()
{
int arr[50];
int size,i;
printf("Entersize of the array:");
scanf("%d",&size);
printf("Enterelements inarray:");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
printf("nArrayinreverseorder:");
for(i = size-1;i>=0; i--)
{
printf("%dt",arr[i]);
}
}
Discursion:
This algorithm in real does not produces a reversed array. Instead it just prints array in
reverse order. If you are looking to reverse the elements then skip to next logic. So here
goes step by step descriptive logic to print array in reverse order.
a. Input size and elements in array from user. Store it in some variable
say size and arr.
b. Run a loop from size - 1 to 0 in decremented style. The loop structure should look
like for(i=size-1; i>=0; i--).
c. Inside loop print current array element i.e. arr[i].
5. C Program to Merge the Elements of 2 Sorted Array
1. Create two arrays of some fixed size and define their elements in sorted fashion.
2. Take two variables i and j, which will be at the 0th position of these two arrays.
3. Elements will be compared one by one using i and j in for loop, and whichever element is smaller
than the other, that element will get inserted to final array and the position(either i or j) will move by
one, whereas the other array’s track position will remain in that same place.
4. Above work will be done till we reach the end of either array. After that, one of the array whose
elements are still to be added, its elements will get straightaway added to the final array.
#include <stdio.h>
void main()
{
int array1[50], array2[50], array3[100], m, n, i, j, k = 0;
printf("n Enter size of array Array 1: ");
scanf("%d", &m);
printf("n Enter sorted elements of array 1: n");
for (i = 0; i < m; i++)
{
scanf("%d", &array1[i]);
}
printf("n Enter size of array 2: ");
scanf("%d", &n);
printf("n Enter sorted elements of array 2: n");
for (i = 0; i < n; i++)
{
scanf("%d", &array2[i]);
}
i = 0;
j = 0;
while (i < m && j < n)
{
if (array1[i] < array2[j])
{
array3[k] = array1[i];
i++;
}
else
{
array3[k] = array2[j];
j++;
}
k++;
}
if (i >= m)
{
while (j < n)
{
array3[k] = array2[j];
j++;
k++;
}
}
if (j >= n)
{
while (i < m)
{
array3[k] = array1[i];
i++;
k++;
}
}
printf("n After merging: n");
for (i = 0; i < m + n; i++)
{
printf("n%d", array3[i]);
}
}
Discursion:
1. Declare 2 1D arrays of some fixed size, then take size of the arrays from user and define all the
elements of the array according to the size in sorted fashion.
2. Take two variables, i and j as iterators which will track the position of elements in arrays.
3. Running a while loop till we reach the end of either array, the element at ith and jth position of two
arrays are compared.
4. The smaller element gets inserted into final array (third array, whose size is the sum of the size of
these two arrays) and the track position gets incremented by 1.
5. This process continues, till we reach the end of either array.
6. After finishing the loop above, one of the array’s tracker(i.e either i or j) will not be at the last
position of the corresponding array, in that case we will have to add all the remaining elements of
that array to the final array as it is one by one
6. C Program to Check if a String is a Palindrome without using the Built-in Function
Algorithm:
a. Take a string as input and store it in the array.
b. Reverse the string and store it in another array.
c. Compare both the arrays.
#include <stdio.h>
void main()
{
char string[25], reverse_string[25] = {'0'};
int i, length = 0, flag = 0;
printf("Enter a string:");
gets(string);
for (i = 0; string[i] != '0'; i++)
{
length++;
}
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}
for (flag = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("n%s is a palindrome", string);
else
printf("n%s is not a palindrome", string);
}
Discursion:
a. Take a string as input and store it in the array string[].
b. Store the same string into the another array reverse_string[] in the reverse fashion.
c. Using for loop compare the elements of both the arrays.
d. If all the elements of the array are same, then it is a palindrome. Otherwise it is not a palindrome.
Ad

More Related Content

What's hot (20)

Producer consumer problem operating system
Producer consumer problem operating systemProducer consumer problem operating system
Producer consumer problem operating system
Al Mamun
 
Method overriding
Method overridingMethod overriding
Method overriding
Azaz Maverick
 
Special value testing
Special value testingSpecial value testing
Special value testing
Bapi Das
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Shaina Arora
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
RAJ KUMAR
 
HTTP request and response
HTTP request and responseHTTP request and response
HTTP request and response
Sahil Agarwal
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
nested if.pptx
nested if.pptxnested if.pptx
nested if.pptx
HarishRagavB
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
Archana Gopinath
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
Mahantesh Devoor
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
All about decision making statements in php
All about decision making statements in phpAll about decision making statements in php
All about decision making statements in php
Programing Code Example
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
rajshreemuthiah
 
Digital Logic circuit
Digital Logic circuitDigital Logic circuit
Digital Logic circuit
kavitha muneeshwaran
 
C operator and expression
C operator and expressionC operator and expression
C operator and expression
LavanyaManokaran
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
programming9
 
Producer consumer problem operating system
Producer consumer problem operating systemProducer consumer problem operating system
Producer consumer problem operating system
Al Mamun
 
Special value testing
Special value testingSpecial value testing
Special value testing
Bapi Das
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Shaina Arora
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
RAJ KUMAR
 
HTTP request and response
HTTP request and responseHTTP request and response
HTTP request and response
Sahil Agarwal
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
Archana Gopinath
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
Mahantesh Devoor
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
All about decision making statements in php
All about decision making statements in phpAll about decision making statements in php
All about decision making statements in php
Programing Code Example
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
programming9
 

Similar to Write a program to check a given number is prime or not (20)

All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
sandeep kumbhkar
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
DR B.Surendiran .
 
Cpds lab
Cpds labCpds lab
Cpds lab
praveennallavelly08
 
C programming codes for the class assignment
C programming codes for the class assignmentC programming codes for the class assignment
C programming codes for the class assignment
Zenith SVG
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
vrgokila
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
Bilal Mirza
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston
 
Programming egs
Programming egs Programming egs
Programming egs
Dr.Subha Krishna
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
ADA FILE
ADA FILEADA FILE
ADA FILE
Gaurav Singh
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
YOGESH SINGH
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
Prof. Dr. K. Adisesha
 
Pnno
PnnoPnno
Pnno
shristichaudhary4
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
manoj11manu
 
C programs
C programsC programs
C programs
Vikram Nandini
 
Array Cont
Array ContArray Cont
Array Cont
Ashutosh Srivasatava
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
sowmya koneru
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
sandeep kumbhkar
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
DR B.Surendiran .
 
C programming codes for the class assignment
C programming codes for the class assignmentC programming codes for the class assignment
C programming codes for the class assignment
Zenith SVG
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
vrgokila
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
Bilal Mirza
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
YOGESH SINGH
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
sowmya koneru
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
Ad

Recently uploaded (20)

Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdfDavid Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
Reflections on Morality, Philosophy, and History
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
AI Publications
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdfDavid Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
AI Publications
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Ad

Write a program to check a given number is prime or not

  • 1. 1. Write a program to checka givennumberisprime ornot. Algorithm: Step1: Start Step2: Declare variablesn,i,prime. Step3: Initialize variables prime←0 i←2 Step4: Readn fromuser. Step5: Repeatthe stepsuntil i<=(n/2) 5.1 If remainderof n÷i equals0 prime←1 Go to step6 5.2 i←i+1 Step6: If prime=0 Displayn isprime else Displayn isnot prime Step7: Stop Source Code: #include<stdio.h> voidmain() {inti,n,prime=0; printf("Enteranynumber:"); scanf("%d",&n); i=2; while(i<=n/2) {if(n%i==0) {prime=1; break; } i++; } if(prime==0) printf("nItisa prime number"); else printf("nItisnota prime number"); }
  • 2. Output: Discursion: If the while loop terminates when the test expression of loop i <= n/2 is false, the entered number is a prime number. The value of prime is equal to 0 in this case. 2. Write a c program to check whether a number is a krishnamurty number or not Algorithm: 1. Input a number from user to check for strong number. Store this in a variable say num. Copy it to a temporary variable for calculations purposes, say originalNum = num. 2. Initialize another variable to store sum of factorial of digits, say sum = 0. 3. Find last digit of the given number num. Store the result in a variable say lastDigit = num % 10. 4. Find factorial of lastDigit. Store factorial in a variable say fact. 5. Add factorial to sum i.e. sum = sum + fact. 6. Remove last digit from num as it is not needed further. 7. Repeat steps 3 to 6 till num > 0. 8. After loop check condition for strong number. If sum == originalNum, then the given number is Strong number otherwise not. #include <stdio.h> voidmain() { int i,originalNum,num,lastDigit,sum; longfact; printf("Enteranynumbertocheckkrishnamurtynumber:"); scanf("%d",&num);
  • 3. originalNum=num; sum = 0; while(num>0) { lastDigit=num %10; fact= 1; for(i=1;i<=lastDigit;i++) { fact = fact * i; } sum= sum+ fact; num= num/ 10; } if(sum== originalNum) { printf("%diskrishnamurtyNUMBER",originalNum); } else { printf("%disNOTkrishnamurtyNUMBER",originalNum); } } 3. Fibonacci seriesusingrecursive functioninC #include<stdio.h> intFibonacci(int); intmain() { intn, i = 0, c; printf("nEnterhowmanytermyouwantin Fibonacci series:"); scanf("%d",&n);
  • 4. printf("nFibonacci series:"); for ( c = 1 ; c <= n ; c++ ) { printf("%dt",Fibonacci(i)); i++; } } intFibonacci(intn) { if ( n == 0 ) return0; else if ( n == 1 ) return1; else return( Fibonacci(n-1) +Fibonacci(n-2) ); } Discursion: Fibonacci series is a sequence of following numbers 0 1 1 2 3 5 8 13 .... It adds previous two numbers value to compute the next number value. In this program fibonacci series is calculated using recursion, with seed as 0 and 1. Recursion means a function calling itself, in the below code fibonacci function calls itself with a lesser value several times. An termination condition is very important to recursion function, i.e n == 0 and n == 1 or the recursive call would be infinite leading to stack overflow error. 4. C Program to print array in reverse Algorithm:
  • 5. 1. Input size and elements in an array. Store it in some variable say size and arr respectively. 2. Declare another array that will store reversed array elements of original array with same size, say reverse[size]. 3. Initialize two variables that will keep track of original and reverse array. Here we will access original array from last and reverse array from first. Hence, initialize arrIndex = size - 1 and revIndex = 0. 4. Run loop from size - 1 to 0 in decremented style. The loop structure should look like while(arrIndex >= 0). 5. Inside loop copy original array to reverse array i.e. reverse [revIndex] = arr[arrIndex];. 6. After copy, increment revIndex and decrement arrIndex. 7. Finally after loop print reverse array. Code: #include <stdio.h> voidmain() { int arr[50]; int size,i; printf("Entersize of the array:"); scanf("%d",&size); printf("Enterelements inarray:"); for(i=0;i<size;i++) { scanf("%d",&arr[i]); } printf("nArrayinreverseorder:"); for(i = size-1;i>=0; i--) { printf("%dt",arr[i]); } } Discursion:
  • 6. This algorithm in real does not produces a reversed array. Instead it just prints array in reverse order. If you are looking to reverse the elements then skip to next logic. So here goes step by step descriptive logic to print array in reverse order. a. Input size and elements in array from user. Store it in some variable say size and arr. b. Run a loop from size - 1 to 0 in decremented style. The loop structure should look like for(i=size-1; i>=0; i--). c. Inside loop print current array element i.e. arr[i]. 5. C Program to Merge the Elements of 2 Sorted Array 1. Create two arrays of some fixed size and define their elements in sorted fashion. 2. Take two variables i and j, which will be at the 0th position of these two arrays. 3. Elements will be compared one by one using i and j in for loop, and whichever element is smaller than the other, that element will get inserted to final array and the position(either i or j) will move by one, whereas the other array’s track position will remain in that same place. 4. Above work will be done till we reach the end of either array. After that, one of the array whose elements are still to be added, its elements will get straightaway added to the final array. #include <stdio.h> void main() { int array1[50], array2[50], array3[100], m, n, i, j, k = 0; printf("n Enter size of array Array 1: "); scanf("%d", &m); printf("n Enter sorted elements of array 1: n"); for (i = 0; i < m; i++) { scanf("%d", &array1[i]); } printf("n Enter size of array 2: "); scanf("%d", &n); printf("n Enter sorted elements of array 2: n"); for (i = 0; i < n; i++) { scanf("%d", &array2[i]); } i = 0; j = 0; while (i < m && j < n) { if (array1[i] < array2[j]) { array3[k] = array1[i]; i++;
  • 7. } else { array3[k] = array2[j]; j++; } k++; } if (i >= m) { while (j < n) { array3[k] = array2[j]; j++; k++; } } if (j >= n) { while (i < m) { array3[k] = array1[i]; i++; k++; } } printf("n After merging: n"); for (i = 0; i < m + n; i++) { printf("n%d", array3[i]); } }
  • 8. Discursion: 1. Declare 2 1D arrays of some fixed size, then take size of the arrays from user and define all the elements of the array according to the size in sorted fashion. 2. Take two variables, i and j as iterators which will track the position of elements in arrays. 3. Running a while loop till we reach the end of either array, the element at ith and jth position of two arrays are compared. 4. The smaller element gets inserted into final array (third array, whose size is the sum of the size of these two arrays) and the track position gets incremented by 1. 5. This process continues, till we reach the end of either array. 6. After finishing the loop above, one of the array’s tracker(i.e either i or j) will not be at the last position of the corresponding array, in that case we will have to add all the remaining elements of that array to the final array as it is one by one 6. C Program to Check if a String is a Palindrome without using the Built-in Function Algorithm: a. Take a string as input and store it in the array. b. Reverse the string and store it in another array. c. Compare both the arrays. #include <stdio.h> void main() { char string[25], reverse_string[25] = {'0'};
  • 9. int i, length = 0, flag = 0; printf("Enter a string:"); gets(string); for (i = 0; string[i] != '0'; i++) { length++; } for (i = length - 1; i >= 0 ; i--) { reverse_string[length - i - 1] = string[i]; } for (flag = 1, i = 0; i < length ; i++) { if (reverse_string[i] != string[i]) flag = 0; } if (flag == 1) printf ("n%s is a palindrome", string); else printf("n%s is not a palindrome", string); } Discursion: a. Take a string as input and store it in the array string[]. b. Store the same string into the another array reverse_string[] in the reverse fashion. c. Using for loop compare the elements of both the arrays. d. If all the elements of the array are same, then it is a palindrome. Otherwise it is not a palindrome.
  翻译: