0% found this document useful (0 votes)
2 views18 pages

Program Code in C Language For TCS Preparations

The document contains a series of C programming exercises aimed at TCS preparations, covering various topics such as character checks, area calculations, sorting algorithms, and command line arguments. Each section provides a specific problem statement followed by a complete C code solution. The exercises include checking for alphabets, calculating areas, determining Armstrong numbers, sorting arrays, and more, demonstrating fundamental programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

Program Code in C Language For TCS Preparations

The document contains a series of C programming exercises aimed at TCS preparations, covering various topics such as character checks, area calculations, sorting algorithms, and command line arguments. Each section provides a specific problem statement followed by a complete C code solution. The exercises include checking for alphabets, calculating areas, determining Armstrong numbers, sorting arrays, and more, demonstrating fundamental programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Program In C Language For TCS Preparations

Q1 //to check whether a character is an alphabet or not


#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c",&c);
if( (c>='a' && c<='z') || (c>='A' && c<='Z'))
printf("%c is an alphabet.",c);
else
printf("%c is not an alphabet.",c);
return 0;
}

Q2. /* C program to find the area of a triangle, given three sides */


#include <stdio.h>
#include <math.h>
void main()
{
float s, a, b, c, area;

printf("Enter the values of a, b and c \n");


scanf("%f%f%f",&a,&b,&c);
/* compute s */
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("Area of a triangle = %f \n", area);
}

Q3. /*To Check Whether a Number is Armsstrong or Not*/


#include <stdio.h>
#include <math.h>
int main()
{
int number, originalNumber, remainder, result = 0, n = 0 ;

printf("Enter an integer: ");


scanf("%d", &number);
originalNumber = number;

while (originalNumber != 0)
{
originalNumber /= 10;
++n;
}
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += pow(remainder, n);
originalNumber /= 10;
}

if(result == number)
printf("%d is an Armstrong number.", number);
else
printf("%d is not an Armstrong number.", number);
return 0;
}

Q4. /*Program To sort an array(Bubble Sorting)*/


#include <stdio.h>
void main()
{
int array[10];
int i, j, num, temp;

printf("Enter the value of num less than 10 \n");


scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("\t %d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
for (i=0;i<num;i++)
printf("\t%d",array[i]);
}

Q5. /*counting of a Character in astring*/


#include <stdio.h>
int main()
{
char str[1000], ch;
int i, frequency = 0;
printf("Enter a string: ");
gets(str);

printf("Enter a character to find the frequency: ");


scanf("%c",&ch);

for(i = 0; str[i] != '\0'; ++i)


{
if(ch == str[i])
++frequency;
}

printf("Frequency of %c = %d", ch, frequency);


return 0;
}

Q6. /*LCM and GCD of two numbers*/


#include <stdio.h>

void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;

printf("Enter two numbers\n");


scanf("%d %d", &num1, &num2);
if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = numerator % denominator;
while (remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d\n", num1, num2, gcd);
printf("LCM of %d and %d = %d\n", num1, num2, lcm);
}

Q7. /*C Program To find Leap Year*/


#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);

if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);

return 0;
}

Q8. /*counting of vovels consonents, digit*/


#include <stdio.h>
int main()
{
char line[150];
int i, vowels, consonants, digits, spaces;
vowels = consonants = digits = spaces = 0;
printf("Enter a line of string: ");
scanf("%[^\n]", line);
for(i=0; line[i]!='\0'; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++spaces;
}
}

printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);

return 0;
}

Q9. /*To check whether Number is palindrome or not*/


#include<stdio.h>
#include<conio.h>
void main()
{
int num,rev=0,a,onum;
printf("Enter no");
scanf("%d",&num);
onum=num;
while (num>=1)
{
a=num%10;
rev=(rev*10)+a;
num =num/10;
}
printf("%d",rev);
if(rev==onum)
printf("palindrom");
else
printf(" not palindrom");
getche();
}

Q10. /* C Program to Check whether a Number is a perfect number*


/#include <stdio.h>
#include<stdlib.h>
#include <stdio.h>
int main()
{
int number, rem, sum = 0, i;
printf("Enter a Number\n");
scanf("%d", &number);
for (i = 1; i <= (number - 1); i++)
{
rem = number % i;
if (rem == 0)
{
sum = sum + i;
}
}
if (sum == number)
printf("Entered Number is perfect number");
else
printf("Entered Number is not a perfect number");
return 0;
}

Q11. /*To check Whether a String is aPalindrom or Not*/


#include<stdio.h>
#include<string.h>
void main()
{
char str[20];
int flag=0;
printf(" Enter The string ");
scanf("%[^\n]",str);
// Start from leftmost and rightmost corners of str
int l = 0;
int h = strlen(str) - 1;
// Keep comparing characters while they are same
while (h > l)
{
if(str[l++] != str[h--])
{
flag=1;
break; }
}
if(flag==0)
printf("%s is palindrome",str);
else
printf("%s is Not Palindrome",str);
}
Q13. /*To calculate the sum of series 1 + 11 + 111+ 1111 + .....n terms*/
#include <stdio.h>
void main()
{
int n,i;
long sum=0;
long int t=1;
printf("Input the number of terms : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%ld ",t);
if (i<n)
{
printf("+ ");

}
sum=sum+t;
t=(t*10)+1;
}
printf("\nThe Sum is : %ld\n",sum);
}

Q14. /*C Program to Swap Two numbers without Using Third Variable*/
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
printf("Enter nos");
scanf("%d%d",&x,&y);
printf("x=%d y=%d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("x=%d y=%d",x,y);
getche();
}
Q15. /*to check whether given character is vovel or consonent*/
#include <stdio.h>
int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;
printf("Enter an alphabet: ");
scanf("%c",&c);

// evaluates to 1 (true) if c is a lowercase vowel


isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

// evaluates to 1 (true) if c is an uppercase vowel


isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
if (isLowercaseVowel || isUppercaseVowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}

Command Line Argument Program in C

Q1. /* Command Line Argument Program in C for conversion from Binary To


Decimal*/
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
int binary, decimal=0,orig,rem,i=0;
binary=atoi(argv[1]);
orig=binary;
while(binary>0)
{
rem=binary%10;
decimal =decimal+ rem*pow(2,i);
i++;
binary=binary/10;
}
printf("binary=%d decimal = %d",orig,decimal);
}

Q2. /*Command Line Argument program to print all command line


arguments*/
#include<stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{int i;
for(i=0;i<argc;i++)
printf("\t%s",argv[i]);
}

Q3. /* Command Line Argument Program in C for The average of Two


Numbers*/
#include<stdio.h>
#include<stdlib.h>
void main(int argc, char *argv[])
{ float a,b,avg;
a=atof(argv[1]);
b=atof(argv[2]);
avg=(a+b)/2;
printf("The average of %f and %f is %f",a,b,avg);

Q4. /*Command Line Argument Program in C for LCM & GCD of Two
numbers*/
#include <stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;

num1=atoi(argv[1]);
num2=atoi(argv[2]);
if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = numerator % denominator;
while (remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d\n", num1, num2, gcd);
printf("LCM of %d and %d = %d\n", num1, num2, lcm);
}
Q5. . /* Command Line Argument Program in C for Calculating Length of a
String without usinh strlen() */
#include <stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
int len;
char *ptr;

if ( argc != 2)
{
printf("\n Invalid arguments ");
exit(0);
}
else
{
ptr=argv[1];
len=0;
while ( *(ptr+len) != '\0')
{
printf("\t%c",*(ptr+len));
len++;
}
printf("\n Length is %d",len);
}
Q6. /*Command Line Argument Program to find Odd and even Numbers for
given number*/
#include<stdio.h>
#include<stdlib.h>
void main(int argc, char *argv[])
{ int i;;
for(i=1;i<argc;i++)
if(atoi(argv[i])%2==0)
printf("\n The %d is even",atoi(argv[i]));
else
printf("\n The %d is Odd",atoi(argv[i]));
}

Q7. /*Command Line Argument Program For String Palindrom*/


#include <stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
int len,i=0,flag=0;
char *ptr,reversal[20];

if ( argc != 2)
{
printf("\n Invalid arguments ");
exit(0);
}
else
{
ptr=argv[1];
len=0;
while ( *(ptr+len) != '\0')
{
printf("\t%c",*(ptr+len));
len++;
}
printf("\n Length is %d\n",len);
}
len--;
while(len>0)
{if(*(ptr+i)!=*(ptr+len))
{flag=1;
break;
}
i++;
len--;
}
if(flag==0)
printf("Palindrom");
else
printf("Not a Palindrome");
}

Q8. /*Command Line argument program for The Area of Triangle if base and
height is given*/
#include<stdio.h>
#include<stdlib.h>
void main(int argc, char *argv[])
{
float base,height,area;
base=atof(argv[1]);
height=atof(argv[2]);
area = (base*height)/2;
printf("\n The are of base = %f and Height = %f is %f",base,height,area);
}

Q9. /*Command Line Argument Program in C To Print Reverse of a string*/


#include <stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
int len,i;
char *ptr,reversal[20];

if ( argc != 2)
{
printf("\n Invalid arguments ");
exit(0);
}
else
{
ptr=argv[1];
len=0;
while ( *(ptr+len) != '\0')
{
printf("\t%c",*(ptr+len));
len++;
}
printf("\n Length is %d\n",len);
}
while(len>=0)
{
printf("%c",*(ptr+len));
len--;
}}

Q10. /*Command Line Argument C Program for square Root of Number


without sqrt()*/
#include<stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
float m,n;
float num;
n=0.0001; // This is taken small so that we can calculate upto decimal places also
num=atof(argv[1]);
for(m=0;m<num;m=m+n)
{
if((m*m)>num)
{
m=m-n; // This if() is used to calculate the final value as soon as the square of the
number exceeds
break; // the number then we deduct the value exceeded and stop the procedure using
break; this is our final value which is stored in m;
}
}
printf("The squareroot of %f is %.2f",num,m);
}

Q11. /*C Command Line Program to compare two strings without strcmp()*/
#include <stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
int len1=0,len2=0,i=0,j=0,flag=0;
char *ptr1,*ptr2;
if ( argc != 3)
{
printf("\n Invalid arguments ");
exit(0);
}
else
{
ptr1=argv[1];
ptr2=argv[2];

while (*(ptr1+len1) != '\0' )


{
printf("\t%c",*(ptr1+len1));
len1++;
}
printf("\n Length of I string is %d\n",len1);

while(*(ptr2+len2)!='\0')
{
printf("\t%c",*(ptr2+len2));
len2++;
}
printf("\n Length of II String is %d\n",len2);
}
len1--;
len2--;
while((len1>0) & (len2>0))
{if(*(ptr1+i)!=*(ptr2+j))
{flag=1;
break;
}
i++;
j++;
len1--;
len2--;
}
if((flag==0) & (i==j))
printf("Strings are Equal");
else
printf("Strings are Not Equal");
}
Q12. /*Command Line Argument Program to find sub-string within string
without using string functions*/
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char* argv[])
{
if(argc<2)
return;

char* c,*d;
c= argv[1];
d= argv[2];
int len1=0,len2=0,i,j,k,count=0,flag=0;
while(c[len1]!='\0')
len1++;
while(d[len2]!='\0')
len2++;

for(i=0;i<len1;i++)
{
k=i;
flag =0;
for(j=0;j<len2;j++)
{
if(c[k]!=d[j])
{
flag =1;
break;
}
k++;
}
if(flag==0)
count++;
}

printf("matches are %d",count);


return;
}

Q13. /*Command Line Program in C for Conversion of Binary To Decimal*/


#include<stdio.h>
#include<stdlib.h>
void main(int argc, char *argv[])
{
int number,orig,binary=0,rem;
number=atoi(argv[1]);
orig=number;
while(number>0)
{
rem=number%2;
binary=(binary*10)+rem;
number=number/2;
}
printf("original number=%d and binary = %d",orig,binary);
}

Q14. /* Command Line Argument Program in C To find Largest number in an


a array*/
#include<stdio.h>
#include<stdlib.h>
void main(int argc, char *argv[])
{
int largest =atoi(argv[1]),i;
for(i=2;i<argc;i++)
{
if(largest<atoi(argv[i]))
largest=atoi(argv[i]);
}

printf("largest = %d",largest);
}

Q15. /*Command Line Argument Program in C To calculate length of a String


without using strlen() function*/
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
int len;
char *ptr;
clrscr();
if ( argc != 2)
{
printf("\n Invalid arguments ");
exit(0);
}
else
{
ptr=argv[1];
len=0;
while ( *(ptr+len) != '\0')
{
len++;
}
printf("\n Length is %d",len);
}
}

Q16/*Command Line Argument Program of Swap of two numbers without using


third variable*/
#include<stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
int x,y;
x=atoi(argv[1]);
y=atoi(argv[2]);
printf("x=%d y=%d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("x=%d y=%d",x,y);
}

You might also like