Programs Questions and Answers
Input : What is your name
Output : your
<p>import java.io.BufferedReader;<br />
import java.io.IOException;<br />
import java.io.InputStreamReader;</p>
<p>public class StringWordIBM <br />
{</p>
<p> public static void main(String[] args) throws IOException <br />
{<br />
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));<br />
String str;<br />
System.out.println("Enter the String : ");<br />
str=br.readLine();<br />
System.out.println(str);<br />
String s[] = str.split(" ");<br />
System.out.println("Splitted string : ");<br />
for(int i=0;i<s.length;i )<br />
{<br />
System.out.println(s[i]);<br />
<br />
}<br />
System.out.println("Second Last word of String is : " s[s.length-2]);<br />
}<br />
<br />
}</p>
Input : NA
Output : NA
#include<iostream>
using namespace std;
int main()
{
long bin, dec = 0, rem, num, base = 1;
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
while (num > 0)
{
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10;
}
cout << "The decimal equivalent of " << bin << " : " << dec << endl;
return 0;
}
Tags:TCS
Input : NA
Output : NA
#include<stdio.h>
#include<conio.h>
# include <iostream>
using namespace std;
main()
{
int a=10, b=20;
clrscr();
cout<<"Before swap is:"<<a<<"\t"<<b;
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
cout<<"\nAfter swap" <<a<<"\t"<<b;
getch();
}
Tags:Wipro TCS Syntel Inc. Sapient Capgemini
Input : NA
Output : NA
#include<stdio.h>
# include <iostream>
using namespace std;
void main()
{
int n,i,j,ct=0;
cout << "Enter any number \n";
cin>>n;
cout << " All prime numbers are -\n";
for(i=2;i<=n;i++)
{
ct=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
ct=1;
break;
}
}
if(ct==0)
{
cout << i;
}
}
getch();
}
Tags:Wipro TCS Syntel Inc.
Input : NA
Output : NA
<p>#include<stdio.h><br />
#include<stdlib.h><br />
int main(int varc,char *varg[])<br />
{<br />
int n=atoi(varg[1]);<br />
int n1=0,n2=1,n3,i,sum;<br />
if(a==1)<br />
printf("%d",0);<br />
if(a==2)<br />
printf("%d",1);<br />
if(a>2)<br />
{<br />
for(i=3;i<=n;i )<br />
{<br />
n3=n2 n1;<br />
n1=n2;<br />
n2=n3;<br />
printf("%d ",n3);<br />
}<br />
}<br />
return 0;<br />
}</p>
Tags:Cisco TCS Syntel Inc.
Input : NA
Output : NA
<p>#include<stdio.h><br />
#include<conio.h><br />
void main()<br />
{<br />
int i,j=2;<br />
for(i=10;i>=1;i--)<br />
{<br />
if(i%j==1)<br />
{<br />
printf("%d",i);<br />
}<br />
}<br />
}</p>
Tags:TCS
Input : NA
Output : NA
#include <stdio.h>
#include <stdlib.h>
int main() {
int c, n;
printf("Ten random numbers in [1,100]\n");
for (c = 1; c <= 10; c++) {
n = rand() % 100 + 1;
printf("%d\n", n);
}
return 0;
}
Tags:TCS
Input : NA
Output : NA
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void isPalindrome(char str[])
{
int l = 0;
int h = strlen(str) - 1;
while (h > l)
{
if (str[l++] != str[h--])
{
printf("%s is Not Palindromen \r\n", str);
return;
}
}
printf("%s is palindromen\r\n", str);
}
int main(int argc, char * argv[])
{
if (argc == 1 || argc > 2)
{
printf("Enter String \r\n");
exit(0);
}
isPalindrome(argv[1]);
return 0;
Tags:TCS
Input : NA
Output : NA
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
int n, i, flag = 0;
double x;
if (argc == 1 || argc > 2)
{
printf("Enter String \r\n");
exit(0);
}
n = atoi(argv[1]);
x = n;
for(i=2; i<= n/2; ++i)
{
if(n%i == 0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%.2lf \r\n", sqrt (x) );
else
printf("%.2lf \r\n",0.00);
return 0;
}
Tags:TCS
Input : NA
Output : NA
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
int x,y;
if (argc == 1 || argc > 3)
{
printf("Enter Two Number\r\n");
exit(0);
}
x=atoi(argv[1]);
y=atoi(argv[2]);
printf("\nSum of two number %d and %d is : %d\r\n",x,y,x+y) ;
return 0;
}
Tags:TCS