Does this code satisfy the conditions of the question?

``````#include<stdio.h>
void main()
{
int n=6,i,k=0,a[]={1,2,3,4,5,6};
for(i=0;i<n;i++)
{
if(a[i]%2!=0)
{
printf("%d,",a[i]);
}
else
k++;
}
for(i=0;i<k;i++)
{
printf("0");
if(i!=k-1)
printf(",");
}
}
``````

Does this code satisfy the conditions of the question?

Note: You can use only one loop for your logic.

No.

``````/*N=4
4444444
4333334
4322234
4321234
4322234
4333334
*/
class ex1
{
public static void main(String d[])
{
int n=4;

for(int i=n;i>1;i--)
{
for(int j=n;j>i;j--)
{
System.out.print(j);
}
for(int k=1;k<i;k++)
{
System.out.print(i);
}
for(int k=i;k>0;k--)
{
System.out.print(i);
}
for(int j=i+1;j<n+1;j++)
{
System.out.print(j);
}

System.out.println("");
}
for(int i=1;i<n+1;i++)
{
for(int j=n;j>i;j--)
{
System.out.print(j);
}
for(int k=1;k<i;k++)
{
System.out.print(i);
}
for(int k=i;k>0;k--)
{
System.out.print(i);
}
for(int j=i+1;j<n+1;j++)
{
System.out.print(j);
}

System.out.println("");
}
}
}

for(int k=1;k<i;k++)
{
System.out.print(i);
}
for(int k=i;k>0;k--)
{
System.out.print(i);
}
for(int j=i+1;j<n+1;j++)
{
System.out.print(j);
}

System.out.println("");
}
}
}
``````

I am considering 'a' and 'A' as same character

``````#include<iostream.h>
#include<conio.h>
int main()
{
char string[]={"aAAabBcD"};
int token[26],sum=0;

for(int i=0;i<26;++i)
token[i]=0;

for(int i=0;i<(int)strlen(string);++i)
{
int ascii = (int)string[i];
if(ascii>=65 && ascii<=90)
{
token[ascii-65]++;
}
else if(ascii>=97 && ascii<=122)
{
token[ascii-97]++;
}
}

for(int i=0;i<26;++i)
{
if(token[i]%2!=0)
sum++;
}

if(sum==0 || sum==1)
cout<<"Palindrome Possible\n";
else
cout<<"Palindrome NOT Possible\n";

getch();
return 0;
}
``````

I am considering 'a' and 'A' as same character

Instead of lines 13-21, just use toupper() or tolower() to convert the characters

``````if( isalpha(string[i]) )
token[toupper(string[i])-65]++;
``````
``````//spiral matrix code
#include<stdio.h>
int left(int row, int col)
{
col=col-1;
return col;
}
int down(int row, int col)
{
row=row+1;
return row;
}
int right(int row, int col)
{
col=col+1;
return col;
}
int up(int row, int col)
{
row=row-1;
return row;
}
int main()
{
int n;
printf("Enter the order of matrix");
scanf("%d",&n);
int a[n][n];
int i,j;
printf("Enter the elements");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
int temp=n/2;
printf("%d",a[temp][temp]);
int r=n/2,c=n/2;
int inter,x,y;
for(inter=1;inter<n;inter=inter+2)
{
for(x=1;x<=inter;x++)
{
c=left(r,c);
printf("%d",a[r][c]);
}
for(x=1;x<=inter;x++)
{
r=down(r,c);
printf("%d",a[r][c]);
}
if(inter+1>n)
break;
for(y=1;y<=inter+1;y++)
{
c=right(r,c);
printf("%d",a[r][c]);
}
for(y=1;y<=inter+1;y++)
{
r=up(r,c);
printf("%d",a[r][c]);
}
}
inter=inter-1;
for(x=1;x<=inter;x++)
{
c=left(r,c);
printf("%d",a[r][c]);
}
return 0;
}
``````
``````/*
for this problem
N=4
4444444
4333334
4322234
4321234
4322234
4333334
4444444
N can be any number
*/

#include<iostream>
using namespace std;
int main()
{
int i,j,tot,v1,v2,target,p,count=0,n;
cin>>n;
tot=2*n-1;
p=n;
int f;
for(i=0;i<=tot/2;i++)
{
v1=i*2;
v2=tot-v1;
target=n-i;
p=n;
count=0;
f=0;
for(j=1;j<=tot;j++)
{
cout<<p;
if(p==target&&f==0)
count++;
else if(f==0)
p--;
if(count==v2)
{p++;f=1;}
}
cout<<endl;
}
p=n;
for(i-=2;i>=0;i--)
{

v1=i*2;
v2=tot-v1;
target=n-i;
p=n;
count=0;
f=0;
for(j=1;j<=tot;j++)
{
cout<<p;
if(p==target&&f==0)
count++;
else if(f==0)
p--;
if(count==v2)
{p++;f=1;}
}
cout<<endl;
}
}
``````
``````#include<iostream>
using namespace std;
int main()
{
for(int i=0;i<7;i++)
{
int n=4;
for(int j=0;j<7;j++)
{
printf("%d",n);
if(i-j>0)
--n;
if(i+j>=6)
++n;
}
printf("\n");
}
}
/*
OUTPUT:
4444444
4333334
4322234
4321234
4322234
4333334
4444444
*/
``````

//solution for the first program with only one loop and two variables

``````#include<stdio.h>
#include<stdlib.h>
#define length sizeof(input)/sizeof(input[0])
int main()
{
int input[]={1,2,3,4,5,6,7},i,j,*output=(int *) calloc(length,sizeof(int ));
for(i=0,j=0;i<length;i++)
{
if(input[i]%2!=0)
{
output[j]=input[i];
j++;
}
}
for(i=0;i<length;i++)
{
printf("%d ",output[i]);
}
}
``````

/code to check if a string can be re-arranged to form a palindrome in linear time/

``````#include<stdio.h>
#include<string.h>
#define MAX 512
int main()
{
char ch;
int count[MAX],i=0,odd=0;
while((ch=(char) getchar())!='\n')
{
count[ch]++;
}
for(i=0;i<256;i++)
{
if(count[i]%2!=0)
{
odd++;
}
}
if(odd>1)
{
printf("cannot form palindrome\n");
}
else
{
printf("can form palindrome\n");
}
}
``````

Given a string , you have to predict whether it can be changed into a palindrome or not by changing the positions of the characters
Eg. Tests
Yes
Explanation:tests can be changed to tsest or stets

``````package javaapplication1;

import java.util.Scanner;

/**
*
* @author Karthigeyan
*/
public class JavaApplication1 {

public static void main(String[] args) {

int a=0,z=0,m=0,type;
String str =new String();
Scanner in =new Scanner(System.in);
str = in.nextLine();
StringBuffer s = new StringBuffer(str);
StringBuffer s1 = new StringBuffer(s);
int[] arr = new int[s.length()];
//checking
for(int i=0;i<s.length();i++){
for(int j=0;j<s.length();j++){
if(s.charAt(i)==s.charAt(j)){
++arr[i];
if(arr[i]>1){
s.deleteCharAt(j);
j--;
}
}
}
if((arr[i]%2)!=0){
a++;
if(a==2){
System.out.println("palindrome is not possible");
break;
}
}
}
//checking ends
//rearraging elements

int[] arr1 = new int[s.length()];
for(int k=0;k<s.length();k++){
arr1[k]=arr[k];
}
char[] carr = new char[s1.length()];
if((s1.length()%2)==0){
type= (s1.length()/2);
}
else{
type = ((s1.length()/2)+1);
}
for(int k=0;((s.length())>z);k++){
if(arr1[k]>1){
if((carr[((type-1))]==s.charAt(z))&&((s.length()%2)!=0)){
--k;
}
carr[m]=carr[s1.length()-m-1]=s.charAt(z);
arr1[k]=arr1[k]-2;
m++;
}
else{
carr[((type-1))]=s.charAt(z);
--arr1[k];
}
if(arr1[k]==0){
++z;
}
else{
--k;
}
if(k==s.length()-1){
k=0;
}
}

for(int k=0;k<s1.length();k++){
System.out.print(carr[k]);
}

}
}
``````

(Partitioning and merging)
Given an array and a partition size, you have to partition the array with that value , then we will specify the partition order , you have to merge based on that order
Array : 1 2 3 4 5
Partition size 2
(so the array will be partitioned as 1 2, 3 4, 5)
Partition order 3 2 1
o/p: 5 3 4 1 2

below is the solution:

``````#include<iostream>
int main()
{
int num_elements,part_size,num_part;
std::cout<<"enter number of elemnts to be present in the array ";
std::cin>>num_elements;
int arr_get[num_elements];

//get elements and partition size
std::cout<<"enter the elements ";
for(int i=0;i<num_elements;i++)
std::cin>>arr_get[i];
std::cout<<"enter the partition size ";
std::cin>>part_size;

//number of partition
int last_part;
if(num_elements%part_size==0)
{
num_part=num_elements/part_size;
last_part=part_size;
}
else
{
num_part=num_elements/part_size+1;
last_part=num_elements%part_size;
}
int temp_arr[num_elements],part_order[num_part];

std::cout<<"enter the merging order ";
for(int i=0;i<num_part;i++)
{
std::cin>>part_order[i];
}
int temp1=0;
int temp2=0;
for(int i=0;i<num_elements;i++)
{
//int temp;
temp_arr[i]=arr_get[((part_order[temp2])*part_size)+temp1-part_size];
// temp=((part_order[temp2])*part_size)+temp1-part_size;
//std::cout<<temp<<'\t';

if(temp1+1<part_size)
{
if(part_order[temp2]==num_part)
{
if(temp1+1<last_part)
{
temp1++;
}
else
{
temp2++;
temp1=0;
}
}
else
{
temp1++;

}
}
else
{
temp2++;
temp1=0;
}

}
// std::cout<<"last part contain "<<last_part<<" elements \n there are about "<<num_part<<"parts \n";
for(int i=0;i<num_elements;i++)
{
std::cout<<temp_arr[i]<<'\n';
}
}
``````

Write a C program to Remove even numbers and fill it with zeros.
Note: You can use only one loop for your logic.
Two variables apart from the array.
Input array 1,2,3,4,5,6
Output 1,3,5,0,0,0

here is the solution

``````#include<iostream>
int main()
{
int n,s=0;
std::cout<<"enter the number of elements to be placed in an array";
std::cin>>n;
std::cout<<"enter "<<n<<" numbers";
int a[n];
for(int i=0;i<n;i++)
{
std::cin>>a[i];
if(a[i]%2==0)
{
a[i]=0;
}
else
{
if(s!=i)
{
a[s]=a[i];
a[i]=0;
}
s++;
}
}
for(int i=0;i<n;i++)
{std::cout<<a[i];}
}
``````
commented: i in the for loop is also a variable. So your program uses 3 variables apart from the array +0

Write a C program to Remove even numbers and fill it with zeros.
Note: You can use only one loop for your logic.
Two variables apart from the array.
Input array 1,2,3,4,5,6
Output 1,3,5,0,0,0

here is the solution

I've Written the program in C.... 'i' and 'temp' are the 2 variables used apart from the array

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

int main(){

int num[]={1,2,3,4,5,6};
int i,*temp;
temp=num;

for(i=0;i<(sizeof(num)/sizeof(num[0]));){
if(num[i]%2!=0){
i++;
temp=&num[i];
continue;
}
else{
if(*temp==0 && temp==&num[(sizeof(num)/sizeof(num[0]))-1]){
if(num[i]%2==0)
num[i]=0;
*temp=0;
break;
}
else if(*temp==0)
temp++;
else if(*temp!=0){
num[i]=0;
num[i]=*temp;
*temp=0;
}
else {}

}

}

/*
for(i=0;i<(sizeof(num)/sizeof(num[0]));i++){
printf("%d ",num[i]);
}
*/

return 0;
}
``````

Correct me if i'm wrong

``````#include<stdio.h>
void main()
{
int i,j,k,n,m,a[100][100];
clrscr();
printf("Enter no of rows and columns\n");
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("the orginal matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\nThe lower triangular matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i>=j)
printf("%d ",a[i][j]);
else
printf(" ");

}
printf("\n");
}

getch();
}
``````

you have rand5() function.this function returns numbers between 1 and 5 randomly with equal probability.implement a function rand7() which makes use of rand5 to return a number between 1 and 7randomly with equal probability.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.