0% found this document useful (0 votes)
20 views14 pages

Top 100 Questions Basic

Uploaded by

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

Top 100 Questions Basic

Uploaded by

184L2 Navya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

Positive or Negative Numbers


#####################################
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n>0)
printf("positive number");
else if(n<0)
printf("negative number");
else
printf("0");
return 0;
}
######################################
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include <stdio.h>

int main()
{
int n,i;
scanf("%d",&n);
if(n>0){
for(i=1;i<=n;i++){
printf("%d\n",i);
}
}
else{
for(i=n;i<0;i++){
printf("%d\n",i);
}
}
return 0;
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2. Even or Odd Numbers
######################################################
#include <stdio.h>

int main()
{
int n;
scanf("%d",&n);
if(n%2==0)
printf("even number");
else
printf("odd number");

return 0;
}
#######################################################
3. Sum of First N Natural numbers
###############################################
#include<stdio.h>
int main(){
int n,i,sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("%d\n",i);
sum+=i;
}
printf("%d",sum);
return 0;
}
############################################

4. Sum of N natural numbers


5. Sum of numbers in a given range
#####################################
#include<stdio.h>
int main(){
int a,b,i,sum=0;
scanf("%d%d",&a,&b);
for(i=a;i<=b;i++){
sum+=i;
}
printf("%d",sum);
return 0;
}
###################################

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include<stdio.h>
int main(){
int a,b,sum=0,x,y;
scanf("%d%d",&a,&b);
x=(a*(a+1))/2;
y=(b*(b+1))/2;

printf("%d",y-x);
return 0;
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6. Greatest of two numbers
7. Greatest of the Three numbers
######################################
#include<stdio.h>
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
printf("%d",a);
else if((b>a)&&(b>c))
printf("%d",b);
else
printf("%d",c);

return 0;
}
#########################################
8. Leap year or not
#########################################
#include<stdio.h>
int main(){
int year;
scanf("%d",&year);
(((year%4==0)&&(year%100!=0))||(year%400==0))?printf("true"):printf("false");
return 0;
}
##########################################
9. Prime number
#######################################
#include<stdio.h>
int main(){
int n,i,c=0;
scanf("%d",&n);
if(n%2==0)
printf("not a prime");
else
{
for(i=3;i<=n;i+=2){
if(n%i==0)
c++;
}
if(c==1)
printf("is a prime");
else
printf("not a prime");
}
return 0;
}
####################################

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include<stdio.h>
int main(){
int n,i,c=0;
scanf("%d",&n);
for(i=1;i<=n;i++){
if(n%i==0)
c++;
}
if(c==2)
printf("a prime");
else
printf("not a prime");

return 0;
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10. Prime number within a given range
#############################################

#include<stdio.h>
int main(){
int n,i,c,j;
scanf("%d",&n);
for(i=1;i<=n;i++){
c=0;
for(j=1;j<=n;j++){
if(i%j==0)
{
c++;
}}
if(c==2)
printf("%d\n",i);
}
return 0;
}
###########################################
11. Sum of digits of a number
##############################################
#include <stdio.h>

int main()
{
int n,sum=0,rem;
scanf("%d",&n);
while(n>0){
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("sum is %d",sum);

return 0;
}
###############################################
12. Reverse of a number
##################################################
#include <stdio.h>

int main()
{
int n,sum=0,rem;
scanf("%d",&n);
while(n>0){
rem=n%10;
sum=sum*10+rem;
n=n/10;
}
printf("sum is %d",sum);

return 0;
}
###########################################

13. Palindrome Number


###############################################
#include <stdio.h>

int main()
{
int n,sum=0,rem,m;
scanf("%d",&n);
m=n;
while(n>0){
rem=n%10;
sum=sum*10+rem;
n=n/10;
}if(m==sum)
printf("it is a palindrome");
else
printf("not a palindrome");

return 0;
}

#############################################
14. Armstrong Number of 3rd order
#######################################
#include <stdio.h>

int main()
{
int n,sum=0,rem,m;
scanf("%d",&n);
m=n;
while(n>0){
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}if(m==sum)
printf("armstrong number");
else
printf("not a armstrong number");

return 0;
}
#########################################
armstrong number of nth order
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include <stdio.h>
#include<math.h>

int main()
{
int n,sum=0,rem,m,c=0,n1;
scanf("%d",&n);
m=n;
n1=n;
while(n!=0){
n=n/10;
c++;
}
while(n1>0){
rem=n1%10;
sum=sum+(pow(rem,c));
n1=n1/10;
}
if(m==sum)
printf("armstrong number");
else
printf("not a armstrong number");

return 0;
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15. Armstrong Number in a given range
16. Fibonacci Series upto nth term
#########################################
#include <stdio.h>
#include<math.h>

int main()
{
int n,t1=0,i,t2=1,a[100];
a[0]=0;
a[1]=1;
scanf("%d",&n);
int next=t1+t2;
for(i=2;i<=n;i++){
a[i]=next;
//printf("%d",next);
t1=t2;
t2=next;
next=t1+t2;
}
for(i=0;i<=n;i++){
printf("%d\t",a[i]);
}
return 0;
}
############################################
17. Find the Nth Term of the Fibonacci Series
######################################
#include <stdio.h>
#include<math.h>

int main()
{
int n,t1=0,i,t2=1,a[100];
a[0]=0;
a[1]=1;
scanf("%d",&n);
int next=t1+t2;
for(i=2;i<=n;i++){
a[i]=next;
//printf("%d",next);
t1=t2;
t2=next;
next=t1+t2;
}
for(i=n;i<=n;i++){
printf("%d\t",a[i]);
}
return 0;
}
###########################################
18. Factorial of a number
############################################
#include <stdio.h>
#include<math.h>

int main()
{
int n,fact=1,i;
scanf("%d",&n);
if(n<0)
printf("error");
else
{
for(i=1;i<=n;i++){
fact*=i;
}
printf("fact is %d",fact);
}

return 0;
}
################################################
19. Power of a number
###############################################
#include <stdio.h>
#include<math.h>

int main()
{
int n,base,exponential,result=1;
scanf("%d%d",&base,&exponential);
while(exponential!=0){
result*=base;
exponential--;
}

printf("power is %d",result);

return 0;
}
#################################################
20. Factor of a number
21. Finding Prime Factors of a number
22. Strong number
23. Perfect number
24. Perfect Square
25. Automorphic number
26. Harshad number
27. Abundant number
########################################################################
#include <stdio.h>

int main()
{
int i,n,sum,m;
scanf("%d",&n);
sum=0;
m=n;
for(i=1;i<n;i++){
if(n%i==0){
sum+=i;
}
}
if(sum>m)
printf("True");
else
printf("False");
return 0;
}
#################################################
28. Friendly pair

WORKING WITH NUMBERS

1. Highest Common Factor(HCF)


2. Lowest Common Multiple(LCM)
3. Greatest Common Divisor
4. Binary to Decimal Conversion
5. Octal to Decimal Conversion
6. Hexadecimal to Decimal Conversion
7. Decimal to Binary Conversion
8. Decimal to Octal Conversion
9. Decimal to Hexadecimal Conversion
10. Binary to Octal Conversion
11. Octal to Binary Conversion
12. Quadrants in which a given coordinate lies
13. Permutations in which n people can occupy r seats in a classroom
14. Maximum number of handshakes
15. Addition of two fractions
16. Replace all 0’s with 1 in a given integer
17. Can a number be expressed as a sum of two prime numbers
18. Count possible decoding of a given digit sequence
19. Calculate the area of a circle
20. Find the prime numbers between 1 to 100
21. Calculate the number of digits in an integer
22. Convert digit/number to words.
23. Counting number of days in a given month of a year
24. Finding Number of times x digit occurs in a given input
25. Finding number of integers which has exactly x divisors
26. Finding Roots of a quadratic equation

CODE FOR RECURSION


1. Power of a Number
2. Prime Number
3. Largest element in an array
4. Smallest element in an array
5. Reversing a Number
6. HCF of two numbers
7. LCM of two numbers
8. Program to calculate length of the string using recursion
9. Print All Permutations of a String
10. Given an integer N the task to print the F(N)th term.
11. Given a list arr of N integers, print sums of all subsets in it
12. Last non-zero digit in factorial
13. Given a positive integer N, return the Nth row of pascal’s triangle
14. Given an integer N representing the number of pairs of parentheses,
the task is to generate all combinations of well-formed(balanced) parentheses
15. Find the Factorial of a number using recursion
16. Find all possible Palindromic partitions of the given String
17. Find all the N bit binary numbers having more than or equal 1’s than 0’s
18. Given a set of positive integers, find all its subsets
19. Given a string s, remove all its adjacent duplicate characters recursively

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IMPORTANT CODES RELATED TO ARRAYS

1. Find Largest element in an array


2. Find Smallest element in an array
3. Find the Smallest and largest element in an array
4. Find Second Smallest Element in an Array
5. Calculate the sum of elements in an array
6. Reverse an Array
7. Sort first half in ascending order and second half in descending
8. Sort the elements of an array
9. Finding the frequency of elements in an array.
10. Sorting elements of an array by frequency
11. Finding the Longest Palindrome in an Array
12. Counting Distinct Elements in an Array.
13. Finding Repeating elements in an Array
14. Finding Non Repeating elements in an Array
15. Removing Duplicate elements from an array
16. Finding Minimum scalar product of two vectors
17. Finding Maximum scalar product of two vectors in an array
18. Counting the number of even and odd elements in an array
19. Find all Symmetric pairs in an array
20. Find maximum product sub-array in a given array
21. Finding Arrays are disjoint or not
22. Determine Array is a subset of another array or not
23. Determine can all numbers of an array be made equal
24. Finding Minimum sum of absolute difference of given array
25. Sort an array according to the order defined by another array
26. Replace each element of the array by its rank in the array
27. Finding equilibrium index of an array
28. Rotation of elements of array- left and right
29. Block swap algorithm for array rotation
30. Juggling algorithm for array rotation
31. Finding Circular rotation of an array by K positions
32. Balanced Parenthesis Problem

OPERATIONS ON STRINGS

1. Check whether a character is a vowel or consonant.


2. Check whether a character is a alphabet or not
3. Find the ASCII value of a character
4. Length of the string without using strlen() function
5. Toggle each character in a string
6. Count the number of vowels
7. Remove the vowels from a String
8. Check if the given string is Palindrome or not
9. Print the given string in reverse order.
10. Remove all characters from string except alphabets.
11. Remove spaces from a string
12. Remove brackets from an algebraic expression
13. Count the sum of numbers in a string
14. Capitalize the first and last character to each word of a string
15. Calculate frequency of characters in a string
16. Find non-repeating characters in a string
17. Check if two strings are Anagram or not.
18. Replace a sub-string in a string
19. Replacing a particular word with another word in a string
20. Count common sub-sequence in two strings
21. Check if two strings match where one string contains wildcard characters.
22. Print all permutations of a given string in lexicographically sorted order.

###################################################################################
##################################

# linear search

def search(a,x):
for i in range(0,len(a)):
if a[i]==x :
return i
return -1
if __name__=="__main__":
a=list(map(int,input().split()))
x=int(input())
r=search(a,x)
if(r==-1):
print("element not in the list")
else:
print("element is at the index at",r)

#binary search

def binarysearch(a,l,r,x):
while(l<r):
mid=l+(r-l)//2
if(a[mid]==x):
return mid
elif (a[mid]<x):
l=mid+1
else:
r=mid-1
if __name__=="__main__":
a=list(map(int,input().split()))
x=int(input())
r=binarysearch(a,0,len(a)-1,x)
if(r==-1):
print("element nbot in the list")
else:
print("element is present at index",r)

#find three largest elements

def threelargest(a):
f=[]
for i in range(len(a)-1,len(a)-4,-1):
f.append(a[i])
return f
if __name__=="__main__":
a=sorted(list(map(int,input().split())))
print("first three largest elements are ", threelargest(a))

#missing number in a list for dirst natural numbers

def missing(a):
n=len(a)+1
k=(n)*(n+1)//2
return k-sum(a)
if __name__=="__main__":
a=list(map(int,input().split()))
r=missing(a)
print("the missing number is ",r)

#Find a pair with the given difference


def findPair(arr,n):
size = len(arr)
i,j = 0,1
while i < size and j < size:
if i != j and arr[j]-arr[i] == n:
print ("Pair found (",arr[i],",",arr[j],")")
return True
elif arr[j] - arr[i] < n:
j+=1
else:
i+=1
print ("No pair found")
return False
arr = [1000, 8, 30, 40, 100]
arr=sorted(arr)
n = 992
findPair(arr, n)

# no of subsets for an array

import itertools
s=[1,2,3,4,5,6,7,8,9]
s=set(s)
k,si=len(s),0
for i in range(0,k+1):
sub = list(itertools.combinations(s, i))
si+=len(sub)
print(si)

#Check if subarray with given product exists in an array

def split(a,n):
left=0
for i in range(0,n):
left+=a[i]
right=0
for j in range(i+1,n):
right+=a[j]
if(left==right):
return i+1
return -1
def printsplit(a,n):
s=split(a,n)
if (s == -1 or s == n ) :
print ("Not Possible")
return
for i in range(0,n):
if(s==i):
print()
print(str(a[i])+" ",end=" ")
a=[1,2,3,4,5,5]
n=len(a)
printsplit(a,n)
#Check if subarray with given product exists in an array

def product(a,n,p):
maxval=a[0]
minval=a[0]
maxproduct=a[0]
for i in range(1,n):
if(a[i]<0):
maxval,minval=minval,maxval
maxval=max(a[i],a[i]*maxval)
minval=min(a[i],a[i]*minval)
if(maxval==p or minval==p):
return True
maxproduct=max(maxproduct,maxval)
return False
if __name__=="__main__":
a=[1,2,-5,-4]
n=len(a)
p=20
if(product(a,n,p)):
print("YES")
else:
print("NO")

# Subarray of size k with given sum

a= [1, 4, 2, 10, 2, 3, 1, 0, 20];


k = 4;
s = 18;
n = len(a);
for i in range(n-k+1):
current_sum=0
for j in range(k):
current_sum+=a[i+j]
if(current_sum==s):
k=1
if(k==1):
print("Yes")
else:
print("No")

#Count the Subarrays with all elements geater than k

a=[3,4,5,6,7,2,10,11]
d=len(a)
k=5
c=n=0
for i in range(d):
if(a[i]>k):
c+=1
else:
n+=c*(c+1)/2
c=0
if(c):
n+=c*(c+1)/2
print(int(n))
##########################3
#include <stdio.h>
int main()
{
float var = 23.564327;

// Declaring pointer variables upto level_4


float *ptr1, **ptr2, ***ptr3, ****ptr4;

// Initializing pointer variables


ptr1 = &var;
ptr2 = &ptr1;
ptr3 = &ptr2;
ptr4 = &ptr3;

// Printing values
printf("Value of var = %f\n", var);
printf("Value of var using level-1"
" pointer = %f\n",
*ptr1);
printf("Value of var using level-2"
" pointer = %f\n",
**ptr2);
printf("Value of var using level-3"
" pointer = %f\n",
***ptr3);
printf("Value of var using level-4"
" pointer = %f\n",
****ptr4);

return 0;
}
#################
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(char** names,int n){
int i,j;
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(strcmp(names[j],names[j+1])>0){
char* temp;
temp=(char*)calloc(30,sizeof(char));
strcpy(temp,names[j]);
strcpy(names[j],names[j+1]);
strcpy(names[j+1],temp);

}
}
}
}
int main(){
char** names;
int n,i;
printf("enter the no of names");
scanf("%d\n",&n);
names=(char**)calloc(n,sizeof(char*));
for(i=0;i<n;i++){
names[i]=(char*)calloc(30,sizeof(char));
scanf("%s",names[i]);

}
sort(names,n);
printf("array after sorting\n");
for(i=0;i<n;i++){
printf("%s\n",names[i]);
}

return 0;

You might also like