[go: up one dir, main page]

0% found this document useful (0 votes)
514 views11 pages

Week 4 Coding Assignment Name: Priyanka Indra Roll No.: 84 Dept: CSE Sem: 6

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

WEEK 4 CODING ASSIGNMENT

Name: Priyanka Indra Roll No.: 84 Dept: CSE Sem: 6th


Program 1:
The problem is quite simple. You're given a number N and a positive integer K. Tell
if N can be represented as a sum of K prime numbers (not necessarily distinct).
Input Format
The first line contains a single integer T, denoting the number of test cases.
Each of the next T lines contains two positive integers, N & K, separated by a single
space.
Output Format
For every test case, output "Yes" or "No".
Input
2
10 2
16
Output
Yes
No
Test Cases:
1. VALID INPUTS:
a) Only integer will be given as input from STDIN.
Constraints
1 <= T <= 5000
1 <= N <= 1000
1 <= K <= 1000
2. INVALID INPUTS:
a) String.
b) Fraction.
c) Negative number.
3. OUTPUT:
a) Write the output to STDOUT without any other additional text.
b) In case of invalid input print 'ERROR' to the STDOUT without any other additional
text
and
terminate.

SOURCE CODE
#include<stdio.h>
#include<stdlib.h>
bool isPrime(int x){
for(int i=2;i*i<=x;i++)
if(x%i==0)
return false;
return true;
}
bool isSum(int n,int k){
if(n<(2*k))
return false;
if(k==1)
return isPrime(n);
if(k==2){
if(n%2==0)
return true;
return isPrime(n-2);
}
return true;
}
int main(){
int t,n,k;
scanf("%d",&t);
if(t>=1 && t<=5000){
for(int i=1;i<=t;i++){
scanf("%d %d",&n,&k);
if((n>=1 && n<=1000)&&(k>=1 && k<=1000)){
if(isSum(n,k))
printf("Yes\n");
else
printf("No\n");
}
else printf("ERROR\n");
}
}
else printf("ERROR\n");
return 0;
}

OUTPUT
2
10 2
Yes
16
No
Program 2
Given an array of integers, task is to print the array in the order – smallest number,
Largest number, 2nd smallest number, 2nd largest number, 3rd smallest number,
3rd largest number and so on.....
Examples:
Input:
arr[] = [5, 8, 1, 4, 2, 9, 3, 7, 6]
Output:
arr[] = {1, 9, 2, 8, 3, 7, 4, 6, 5}
Input:
arr[] = [1, 2, 3, 4]
Output:
arr[] = {1, 4, 2, 3}
Test Cases:
1. VALID INPUTS:
a) Only integer will be given as input through STDIN.
2. INVALID INPUTS:
a) String s
3. OUTPUT:
a) Write the output to STDOUT without any other additional text.
b) In case of invalid input print 'ERROR' to the STDOUT without any other additional
text
and
terminate.

SOURCE CODE
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i;
printf("Enter array size:\n");
scanf("%d",&n);
int arr[n];
printf("Enter array:\n");
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int temp;
for(i=0;i<n;i++){
for(int j=0;j<n;j++){
if(arr[i]<arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
int arr1[n],a=1,b=0;
for(i=0;i<n;i++){
if(i%2==0){
arr1[i]=arr[b++];
}
else{
arr1[i]=arr[n-a++];
}
}
printf("\n");
for(i=0;i<n;i++)
printf("%d ",arr1[i]);
return 0;
}
OUTPUT
Enter array size:
9
Enter array:
581429376
192837465
Program 3
Consider the below series:
1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17, ...
This series is a mixture of 2 series – all the odd terms in the series form a
Fibonacci series and all the even terms are the prime numbers in ascending order.
Write a program to find the Nth term in the series.
The value N in a positive integer that should be read from STDIN. The Nth term that
is calculated by the program should be written to STDOUT. Other than the value of
Nth term, no other characters/string or message should be written to STDOUT.
For example when N=14, the 14th term in the series is 17.
Test Cases:
1. VALID INPUTS:
a) Only integer will be given as input through STDIN.
2. INVALID INPUTS:
a) String.
b) Fraction.
c) Negative number as input argument.
3. OUTPUT:
a) Write the output to STDOUT without any other additional text.
b) In case of invalid input print 'ERROR' to the STDOUT without any other additional
text
and
terminate.

SOURCE CODE
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,flag,i,j,t=1;
int a=0,b=1,c=0,r=1,temp;
scanf("%d",&n);

if(n%2!=0){
while(r<=(n/2+1)){
temp=b;
c=a;
a=b;
b=a+c;
r++;
}
printf("%d",temp);
}
else{
for(i=2;t<=(n/2);i++){
flag=0;
for(j=2;j<i;j++){
if(i%j==0){
flag=1;
break;
}
}
if(flag==0)
t++;
}
printf("%d",(i-1));
}
return 0;
}
OUTPUT
14
17
Program 4
C program to reverse a String Using Recursion
Input String:
margorp emosewa
Output String:
awesome program
Test Cases:
1. VALID INPUTS:
a) Only String will be given as input through STDIN.
2. INVALID INPUTS:
a) Characters other than alphabet.
3. OUTPUT:
a) Write the output to STDOUT without any other additional text.
b) In case of invalid input print 'ERROR' to the STDOUT without any other additional
text
and
terminate.

SOURCE CODE
#include<stdio.h>
#include<string.h>
void reverse(char *str);
int main(){
char str[100];
gets(str);
reverse(str);
return 0;
}
void reverse(char *str){
if(*str){
reverse(str+1);
printf("%c",*str);
}
}
OUTPUT
margorp emosewa
awesome program
Program 5
C Program to Remove all Characters in a String except Alphabet and store the
resultant string within the same string. Change all lower case letters to upper case
and then check whether the string is palindrome is not without using library
function.
Input String:
In.form,atio1n Tec?hnol-og=y
Output String:
Information Technology
INFORMATION TECHNOLOGY
NO
Test Cases:
1. VALID INPUTS:
a) Only String will be given as input through command line argument.
2. INVALID INPUTS:
a) No command line argument.
b) More than 1 command line arguments.
3. OUTPUT:
a) Write the output to STDOUT without any other additional text.
b) In case of invalid input print 'ERROR' to the STDOUT without any other additional
text
and
terminate.
SOURCE CODE
#include<stdio.h>
#include<string.h>
char *reverse(char *str);
int main(){
char str[100];
char *rev;
int i,j;
gets(str);
for(i=0;str[i]!='\0';++i){
while(!((str[i]>='a' && str[i]<='z')||(str[i]>='A' &&
str[i]<='Z')||(str[i]=='\0')||(str[i]==' ')))
{
for(j=i;str[j]!='\0';++j){
str[j]=str[j+1];
}
str[j]='\0';
}
}
printf("%s\n",str);
for(i=0;str[i]!='\0';++i){
if(str[i]>='a' && str[i]<='z')
str[i]=str[i]-32;
}
printf("%s\n",str);
rev=reverse(str);
if(!strcmp(str,rev))
printf("YES\n");
else
printf("NO\n");
return 0;
}
char *reverse(char *str){
static int i=0;
static char rev[100];
if(*str){
reverse(str+1);
rev[i++]=*str;
}
return rev;
}

OUTPUT
information tech#@!nolog&y
information technology
INFORMATION TECHNOLOGY
NO

You might also like