0% found this document useful (0 votes)
70 views

Tcs Coding Questions

The document contains code snippets for various C programming problems: 1) Calculating the lowest common multiple (LCM) of two numbers. 2) Calculating the highest common factor (HCF) of two numbers. 3) Finding the nth term in recurring numeric series.

Uploaded by

Sandeep Chinna
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Tcs Coding Questions

The document contains code snippets for various C programming problems: 1) Calculating the lowest common multiple (LCM) of two numbers. 2) Calculating the highest common factor (HCF) of two numbers. 3) Finding the nth term in recurring numeric series.

Uploaded by

Sandeep Chinna
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

LCM:
=====
#include<stdio.h>

int main()
{
int a, b, max,lcm=0;

scanf("%d\n",&a);
scanf("%d\n",&b);

if(a > b)
max = a;
else
max = b;

while(1)

{
if(max%a == 0 && max%b == 0)
{
lcm = max;
break;
}

max ++;
}

printf("LCM is %d", lcm);


return 0;
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------------

2.HCF:
=====
#include<stdio.h>
int main()
{
int a, b, i, hcf;
scanf("%d",&a);
scanf("%d",&b);
for(i = 1; i <= a || i <= b; i++)
{
if( a%i == 0 && b%i == 0 )
hcf = i;
}

printf("HCF = %d", hcf);

return 0;
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------------------------------------------
3.Nth term in the series 0,0,7,6,14,12

#include <stdio.h>
int main()
{
int n;
scanf("%d\n",&n);
if(n%2==1)
{
printf("%d",7*(n/2));
}
else
{
printf("%d",6*((n-1)/2));
}
return 0;
-------OR--------
#include<stdio.h>
int main()
{
int i, n, a=0, b=0;
printf("enter number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
a = a + 7;
}
else
{
b = b + 6;
}
}
if(n%2!=0)
{
printf("%d",a-7);
}
else
{
printf("%d",b-6);
}
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------------------------------------

4.String Reverse:
============
#include<stdio.h>
#include<string.h>
int main()
{
char str1[100],str2[100];
int len,i=0,j;
scanf("%s",str1);
len=strlen(str1);
for(i=len-1;i>=0;i--)
{
str2[j]=str1[i];
j++;
}

printf("%s",str2);

return 0;
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------------------------------------------

5.String Palindrome:
==============
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i, len, flag;
flag = 0;

printf("\n Please Enter any String : ");


gets(str);

len = strlen(str);

for(i = 0; i < len; i++)


{
if(str[i] != str[len - i - 1])
{
flag = 1;
break;
}
}
if(flag == 0)
{
printf("\n %s is a Palindrome String", str);
}
else
{
printf("\n %s is Not a Palindrome String", str);
}

return 0;
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------------------------------------
6.Addition of two Numbers:
===================
#include<stdio.h>
int sum(int x,float y)
{
float ans;
ans=(float)x+y;
printf("%.2f",ans);
}
int main()
{
int a;
float b;
scanf("%d",&a);
scanf("%f",&b);
sum(a,b);
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------------
7.Avg of two Numbers:
================
#include<stdio.h>
int Avg(int x,float y)
{
float ans;
ans=((float)x+y)/2;
printf("%.2f",ans);
}
int main()
{
int a;
float b;
scanf("%d",&a);
scanf("%f",&b);
Avg(a,b);
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------------
8.String Pattern:
===========
#include<stdio.h>
#include<string.h>
int main()
{
char a[10], b[10], c[10];
scanf("%s",a);
scanf("%s",b);
scanf("%s",c);
int x=strlen(a);
int y=strlen(b);
int z=strlen(c);
for(int i=0;i<x;i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||
a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U')
a[i] = '%';
}
for(int i=0;i<y;i++)
{
if(!(b[i]=='a'||b[i]=='e'||b[i]=='i'||b[i]=='o'||b[i]=='u'||b[i]=='A'||
b[i]=='E'||b[i]=='I'||b[i]=='O'||b[i]=='U'))
b[i] = '#';
}
for(int i=0;i<z;i++)
{
if(c[i]>=97)
{
c[i]=c[i]-32;
}
}
printf("%s%s%s",a,b,c);
}

Input: Output:
==== =====
hi h%%#o#YOU
how
you

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------------
9.Nth term in the series 0,0,2,1,4,2,6,3,8,4,10,5,12,6,14,7,16,8:
============================================

#include<stdio.h>
#include<string.h>
int main()
{
int n,a,res;
scanf("%d",&n);
if(n%2==0)
{
a=n/2;
res=a-1;
printf("%d",res);
}
else
{
a=(n+1)/2;
res=a-1;
printf("%d",2*res);
}
}
--------------------OR-----------------------
#include<stdio.h>
int main()
{
int i, n, a=0, b=0;
printf("enter number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
a = a + 2;
}
else
{
b = b + 1;
}
}
if(n%2!=0)
{
printf("%d",a-2);
}
else
{
printf("%d",b-1);
}
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------------------------------------------
10.Nth term of the series 1,1,2,3,4,9,8,27,16,81,32,243,......
#include<stdio.h>
int main()
{
int n,a,res;
scanf("%d",&n);
if(n%2==0)
{
n=(n-1)/2;
int res=pow(3,n);
printf("%d",res);
}
else
{
n=n/2;
int res=pow(2,n);
printf("%d",res);
}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------------------------------------
11.Prime Number Check:
-------------------------
#include<stdio.h>
int main()
{
int n,count=0;
scanf("%d",&n);
for(int i=2;i<n;i++)
{
if(n%i==0)
count = count+1;
}
if(count==2)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
}
-------By calling Function-----------------------

#include<stdio.h>
int prime(int n)
{
int count=0;
for(int i=2;i<n;i++)
{
if(n%i==0)
count = count+1;
}
if(count==2)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
}
int main()
{
int n;
scanf("%d",&n);
if(n<0)
{
printf("Please enter a positive integer");
}
else
prime(n);
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------------------------------
12.Leap Year or Not:
==============
#include<stdio.h>
int leapyear(int year)
{
//checking divisibility by 4
if(year%4 == 0)
{
//checking divisibility by 100
if( year%100 == 0)
{
//checking divisibility by 400
if ( year%400 == 0)
printf("%d, the year entered happens to be a leap year", year);
else
printf("%d is surely not a leap year", year);
}
else
printf("%d, the year entered happens to be a leap year", year );
}
else
printf("%d is surely not a leap year", year);
return 0;
}
int main()
{
int year, val;
scanf("%d",&year);
val = leapyear(year);
return 0;
}

You might also like