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

A "Welcome in C Programming Class Welcome Again To C Class !" B A.lower C B.split D Set (C) Print (Len (D) )

The document contains 19 code snippets demonstrating various algorithms and programming concepts in C/C++. The snippets include programs to: count unique words in a string; print patterns of numbers based on user inputs; find the greatest common divisor of numbers; remove all vowels from a string; calculate the sum of all elements in an array; and find the maximum sum of a contiguous subarray within a 1D array.

Uploaded by

Ranjeet Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

A "Welcome in C Programming Class Welcome Again To C Class !" B A.lower C B.split D Set (C) Print (Len (D) )

The document contains 19 code snippets demonstrating various algorithms and programming concepts in C/C++. The snippets include programs to: count unique words in a string; print patterns of numbers based on user inputs; find the greatest common divisor of numbers; remove all vowels from a string; calculate the sum of all elements in an array; and find the maximum sum of a contiguous subarray within a 1D array.

Uploaded by

Ranjeet Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

11. C Program to Count the Number of Unique Words.

Input:
Welcome in C Programming Class welcome again to C class !
Output:
8

a="Welcome in C Programming Class welcome again to C class !"


b=a.lower()
c=b.split()
d=set(c)
print(len(d))
12. Given an integer N. print 2*N lines in following manner
if N=4 and s=3
the pattern generated would be
3
44
555
6666
6666
555
44
3
where (0<=N<=100) and s>0

N = int(input())
S = int(input())
for i in range(1,N+1):
    for j in range(i):
        print(S+i-1,end=" ")
    print()
for i in range(N,0,-1):
    for j in range(i):
        print(S+i-1,end = " ")
    print()
13. Write a program to find greatest common divisor.
def computeGCD(x, y):
  
   while(y):
       x, y = y, x % y
  
   return x
  
a = 60
b= 48
  
# prints 12
print ("The gcd of 60 and 48 is : ",end="")
print (computeGCD(60,48))

14. Write a progarm to find greatest common divisor for series of numbers.

def findgcd(x, y):


   while(y):
      x, y = y, x % y
   return x
l =eval(input(“enter series of number in list”))
num1=l[0]
num2=l[1]
gcd=findgcd(num1,num2)
for i in range(2,len(l)):
   gcd=findgcd(gcd,l[i])
print("gcd is: ",gcd)

15. Given a string str, write a program to eliminate all the vowels from it.

s = input(“Enter any string to remove all vowels from it: “)


vowels = (‘aeiou’)
s = s.lower()
for x in s:
if x in vowels:
s = s.replace(x, “”) // Replacing the vowels with ‘’
print(s)

16. the pattern for N=4 and s=4 where s is generated would be
1234
9 10 11 12
13 14 15 16
5678

N = int(input())
S = int(input())
for i in range(0,N,2):
    for j in range(1,S+1):
        print(i*S+j,end=" ")
    print()
    
for i in range(int((N+2)/2),0,-2):
    for j in range(1,S+1):
        print(S*i+j,end=" ")
    print()

17. the pattern for N=4 and s=5 where s is generated would be
12222
33332
34444
55554

N = int(input())
S = int(input())
for i in range(N):
    if i%2 == 0:
        print(i+1,end=" ")
    for j in range(S-1):
        print(i+2,end=" ")
    if i%2 != 0:
        print(i+1,end=" ")
    print()
18. C program to find the sum of all elements of an array using Pointers as arguments
Problem Text: Given a List of N number a1,a2,a3........an. on an array of integers.
Example:
Input: N=5
A [10, 20, 30, 40, 50] 
Output 150

total = 0
  
# creating a list
list1 = [10, 20, 30, 40, 50] 
  
# Iterate each element in list
# and add them in variale total
for ele in range(0, len(list1)):
    total = total + list1[ele]
  
# printing total value
print("Sum of all elements in given list: ", total)

19. C Program to Find the Sum of Contiguous Subarray within a 1 - D Array of Numbers which has the Largest Sum
Problem Text: Given a List of N number a1,a2,a3........an. on an array of integers.
Example:
1) Input N=8
A [10, -2, 15, 9, -8, 12, 20, -5]
Output 56

def maxSubArraySum(a, size):

max_so_far = 0
max_ending_here = 0

for i in range(0, size):


max_ending_here = max_ending_here + a[i]
if max_ending_here < 0:
max_ending_here = 0

# Do not compare for all elements. Compare only


# when max_ending_here > 0
elif (max_so_far < max_ending_here):
max_so_far = max_ending_here

return max_so_far
a=[10, -2, 15, 9, -8, 12, 20, -5]
print(maxSubArraySum(a,8))

You might also like