SlideShare uma empresa Scribd logo
1 de 9
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.

Mais conteúdo relacionado

Mais procurados

C programs
C programsC programs
C programs
Minu S
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
Simple c program
Simple c programSimple c program
Simple c program
Ravi Singh
 

Mais procurados (20)

C programs
C programsC programs
C programs
 
Ansi c
Ansi cAnsi c
Ansi c
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentals
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in c
 
Simple c program
Simple c programSimple c program
Simple c program
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
Chapter 2 : Balagurusamy_ Programming ANsI in C
Chapter 2 :  Balagurusamy_ Programming ANsI in CChapter 2 :  Balagurusamy_ Programming ANsI in C
Chapter 2 : Balagurusamy_ Programming ANsI in C
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 

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

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
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 

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

Cpds lab
Cpds labCpds lab
Cpds lab
 
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
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
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
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Pnno
PnnoPnno
Pnno
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
C programs
C programsC programs
C programs
 
Array Cont
Array ContArray Cont
Array Cont
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
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
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
C-programs
C-programsC-programs
C-programs
 
Basic C Programming Lab Practice
Basic C Programming Lab PracticeBasic C Programming Lab Practice
Basic C Programming Lab Practice
 
Examples sandhiya class'
Examples sandhiya class'Examples sandhiya class'
Examples sandhiya class'
 

Último

Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
chumtiyababu
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 

Último (20)

Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 

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.