0% found this document useful (0 votes)
29 views9 pages

Programming Tasks and Solutions

The document contains a series of programming tasks and their corresponding solutions in C++ and Java. Tasks include finding the HCF of two numbers, compressing strings, converting decimal to binary, generating Fibonacci triangles, and checking for prime numbers, among others. Each task is accompanied by sample input and output for clarity.

Uploaded by

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

Programming Tasks and Solutions

The document contains a series of programming tasks and their corresponding solutions in C++ and Java. Tasks include finding the HCF of two numbers, compressing strings, converting decimal to binary, generating Fibonacci triangles, and checking for prime numbers, among others. Each task is accompanied by sample input and output for clarity.

Uploaded by

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

1. Write a program to find HCF of two numbers by without using recursion.


Input format: The first line contains any 2 positive numbers separated by space.​
Output format:
●​ Print the HCF of given two numbers.​

Sample Input: 70 15​
Sample Output: 5

#include<iostream>
int gcd(int,int);
int main()
{
int m,n,ans;
scanf("%d",&m);
scanf("%d",&n);
while(m!=n)
{
if(m>n)
{
m=m-n;
}
else
{
n=n-m;
}
}
printf("%d",m);
return 0;
}​

2. Consider a string, S, that is a series of characters, each followed by its frequency as an
integer.
The string is not compressed correctly, so there may be multiple occurrences of the same
character.A properly compressed string will consist of one instance of each character in
alphabetical order followed by the total count of that character within the string.​

import [Link].*;

public class Main {


public static String properCompression(String s) {
StringBuilder compressedStr = new StringBuilder();

for (int i = 0; i < [Link](); i += 2) { char c = [Link](i); int count =


[Link]([Link](i + 1)); while (count > 0) {
[Link](c);
count--;
}
}

return [Link]();
}

public static void main(String[] args) {


String inputStr = "a3b5c2a2";
String compressedResult = properCompression(inputStr);
[Link](compressedResult); // Output: "aaabbbbbcc"
}
}​

3. Write a C++ Program to Change Decimal Number to Binary?​

#include<iostream>
namespace std;
int main ()
{
int a[10], n, i;
cout << "Enter the number to convert: ";
cin >> n;
for (i = 0; n > 0; i++)
{
a[i] = n % 2;
n = n / 2;
}
cout << "Binary of the given number= ";
for (i = i - 1; i >= 0; i--)
{
cout << a[i];
}
}​

4. C++ Program to generate Fibonacci Triangle​

#include<iostream>
using namespace std;
int main()
{
int a=0,b=1,i,c,n,j;
cout<<"Enter the limit: ";
cin>>n;
for(i=1; i<=n; i++)
{
a=0;
b=1;
cout<<b<<"\t";
for(j=1; j<i; j++)
{
c=a+b;
cout<<c<<"\t";
a=b;
b=c;
}
cout<<"\n";
}
return 0;
}​

5. What is the Output of the program
Using namespace std;
int main()
{
int a=5,b=10,c=15;
int*arr[]={&a,&b,&c};
cout<<arr[1];
return 0;
}​

5​
10​
15​
It will print their address of variable b.


6. What is the Output of the program

Using namespace std;


int main()
{
Char arr[20];
int i;
for(i=0;i<10;i++)
*(arr+i)=65 +1;
*(arr+i)=0;
cout<<arr;
return(o);
}​

ABCDEFGHIJ

7. What is the Output of the program

#include<iostream>
Using namespace std;
int main()
{
char*ptr;
Char Str[]="abcdefg";
ptr=Str;
ptr+=5;
cout<<ptr;
return 0;
}
fg
cdef
defg
Abcd​

8. For each element of an array of non-negative integers, arrin], is calculated as:
prefli] = arr[1JIJ⊕ arrl2JIJO . . . ⊕arrli]
Here x ⊕ y is the bitwise XOR of x and y.
The array pref[n] contains the prefix XOR of all elements in arr[n]where 1 s is n:
1
2

Given the array pref, find the original array arr.

Note: There is always a unique arr for a given pref.

public class Main {


public static int[] findOriginalArray(int[] pref) {
int n = [Link];
int[] arr = new int[n];

arr[0] = pref[0];
for (int i = 1; i < n; i++) {
arr[i] = pref[i] ^ pref[i - 1];
}

return arr;
}

public static void main(String[] args) {


int[] pref = {3, 5, 2, 10};
int[] originalArray = findOriginalArray(pref);
[Link]("Original Array: ");
for (int num : originalArray) {
[Link](num + " ");
}
// Output: Original Array: 3 8 10 2
}
}​

9. Given a string of characters followed by their frequency, compress it into a proper format.
Example

Input
a3b5c2a2

Output
abc

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

void properCompression(const char *s, char *result) {


int freq[256] = {0}; // Frequency array for ASCII

for (int i = 0; s[i] != '\0'; i += 2) {


char c = s[i];
int count = s[i + 1] - '0';
freq[c] += count;
}

int index = 0;
for (char c = 'a'; c <= 'z'; c++) {
if (freq[c] > 0) {
result[index++] = c;
}
}
result[index] = '\0';
}

int main() {
char inputStr[] = "a3b5c2a2";
char compressedResult[100];
properCompression(inputStr, compressedResult);
printf("%s\n", compressedResult);
return 0;
}​

10. Write a program to calculate the sum of digits of a number.
Example
Input
12345

Output
15

#include <stdio.h>

int main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);

while (n > 0) {
sum += n % 10;
n /= 10;
}

printf("Sum of digits: %d\n", sum);


return 0;
}


11. Write a C program to convert the Decimal to a Binary number
Example

Input
10

Output
Binary of the given number = 1010

#include <stdio.h>

int main() {
int n;
printf("Enter the number to convert: ");
scanf("%d", &n);

int binary[32], i = 0;
while (n > 0) {
binary[i] = n % 2;
n = n / 2;
i++;
}

printf("Binary of the given number = ");


for (i = i - 1; i >= 0; i--) {
printf("%d", binary[i]);
}
return 0;
}​

12. What is the output of the following program?
Example

Input
No input is required

Output
10

#include <stdio.h>

int main() {
int a = 5, b = 10, c = 15;
int *arr[] = {&a, &b, &c};
printf("%d\n", *arr[1]);
return 0;
}​

13. Write a program to reverse a given string.
Example

Input
nxtwave

Output
evawtxn

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

int main() {
char str[100];
printf("Enter a string: ");
gets(str);
int n = strlen(str);
for (int i = n - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");
return 0;
}​

14. Write a program to check if a number is prime.
Example

Input
29

Output
Prime

#include <stdio.h>

int main() {
int n, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);

if (n <= 1) isPrime = 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
isPrime = 0;
break;
}
}

if (isPrime) printf("Prime\n");
else printf("Not Prime\n");
return 0;
}

15. Write a program to count the number of vowels in a given string
Input
Hello World

Output
Number of vowels: 3
#include <stdio.h>

int main() {
char str[100];
int count = 0;

printf("Enter a string: ");


gets(str);

for (int i = 0; str[i] != '\0'; i++) {


char ch = str[i];
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
count++;
}
}

printf("Number of vowels: %d\n", count);


return 0;
}

You might also like