Skip to content
Home » Uncategorized » TCS and Infosys Coding Question | TCS and Infosys placement prepration

TCS and Infosys Coding Question | TCS and Infosys placement prepration

To find GCD of two numbers

#include <stdio.h>
int main(int argc,char *argv[])
{
int a,b,small,i;
a=atoi(argv[1]);
b=atoi(argv[2]);
if(a>b)
small=b;
else
small=a;
for(i=small;i>=1;i--)
{
if((a%i)==0&&(b%i)==0)
{
printf("%d",i);
break;
}
}
return 0;
}

To find the lcm of two numbers




#include <stdio.h>
int main(int argc,char *argv[])
{
int a,b,large;
a=atoi(argv[1]);
b=atoi(argv[2]);
if(a>b)
large=a;
else
large=b;
while(1)
{
if((large%a)==0&&(large%b)==0)
{
printf("%d",large);
break;
}
large++;
}
return 0;
}

To find the Factorial of a non negative number

#include <stdio.h>
int main(int argc,char *argv[])
{
int n,fact=1,i;
n=atoi(argv[1]);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("%d",fact);
return 0;
}

To find the area of a circle (area=3.14rr), when diameter is given.





#include <stdio.h>
#define PI 3.14
int main(int argc,char *argv[])
{
float dia,radius,area=0;
dia=atoi(argv[1]);
radius=0.5*dia;
area=PI*radius*radius;
printf("%.2f",area);
return 0;
}
To check whether the given year is Leap year or not.
#include <stdio.h>
int main(int argc,char *argv[])
{
int year;
year=atoi(argv[1]);
if(year%100==0)
{
if(year%400==0)
printf("Leap year");
else
printf("not leap year");
}
else
if(year%4==0)
printf("leap year");
else
printf("not leap year");
return 0;
}

To find the area of triangle when base and height is given.

#include <stdio.h>
int main(int argc,char *argv[])
{
float height,base,area;
height=atoi(argv[1]);
base=atoi(argv[2]);
area=0.5*base*height;
printf("%.2f",area);
return 0;
}

To print the Fibonacci series.
Input=6 Output=1 1 2 3 5 8

#include <stdio.h>
int main(int argc,char *argv[])
{
int n,first=1,sec=1,next,i;
n=atoi(argv[1]);
for (i=0;i<n;i++)
{
if (i<=1)
next=1;
else
{
next=first+sec;
first=sec;
sec=next;
}
printf("%d ",next);
}
return 0;
}





To check whether the given number is prime or not.

#include <stdio.h>
int main(int argc,char *argv[])
{
int n,i,count=0;
n=atoi(argv[1]);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
printf("prime number");
else
printf("not prime number ");
return 0;
}

check whether given number is strong number or
not.

#include<stdio.h>
int fact(int);
int main(int argc, char *argv[])
{
int num,d,n,res=0,i,count=0,x;
n=atoi(argv[1]);
num=n;
x=num;
while(n!=0)
{
n=n/10;
count++;
}
for(i=0;i<count;i++){
if(x>0)
{
d=x%10;
res=res+fact(d);
x=x/10;
}
}
if(res==num)
{
printf("strong number");
}
else printf("not strong number");
return 0;
}
int fact(int x)
{
if(x==0)
return 1;
else
return x*fact(x-1);
}

To check whether number is palindrome or not.

#include <stdio.h>
int main(int argc,char *argv[])
{
int num,rev=0,digit,orig;
num=atoi(argv[1]);
orig=num;
while(num>0){
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
if(orig==rev)
{
printf("palindrome");
}
else
printf("not palindrome");
return 0;
}

2 thoughts on “TCS and Infosys Coding Question | TCS and Infosys placement prepration”

Leave a Reply

Your email address will not be published. Required fields are marked *