## C Program for Sum of digits using recursive function

Below program first accept the positive number from user and then find out the sum of digits. for example if user enter 234 then sum of digit is 2+3+4 = 9

``````//Program Name: Write a function for Sum Of digits
// for example if user enter 121 then sum of digit is 1+2+1 = 4

#include<stdio.h>
#include<conio.h>
int sum_d(int n)
{
if(n==0)
return(0);
else
return(n%10+sum_d(n/10));
}//end of function

void main()
{     int no,sum;

printf("\n\tEnter a number to calculate digit sum:\t ");
scanf("%d",&no);

sum=sum_d(no);

printf(" \n\n\tSum of digit is =\t %d ",sum);

}//end of main``````

#### Output

``````
Enter a number to calculate digit sum:   543

Sum of digit is =        12

Enter a number to calculate digit sum:   2

Sum of digit is =        2

``````

## Palindrome Code in c

Palindrome meaning word, phrase that reads the same backwards as forwards. for example Madam, Kayak, radar, civic, refer. See below code to accept input from user and check.

``````/* Program Name: Check if given string is palindrome or not. ( Run Program in Turbo C)
*/
#include<stdio.h>
#include<string.h>
void main()
{
char str, str2;
printf("\n\nEnter String to Check if given string is palindrome or not:\t ");
scanf("%s",str);
//using strcpy function creating copy of string in string 2
strcpy(str2,str);
strrev(str2);
//This function is available in ANSI C compiler not in other.
//using strrev function reversing the string.

// now comparing both string to check if they are same or not.
if(strcmp(str,str2)==0)
printf("\n\nGiven String is Palindrome");
else
printf("\n\nGiven String is Not Palindrome");
}//main

``````

#### Output

``````

Enter String to Check if given string is palindrome or not: kayak

Given String is Palindrome

Enter String to Check if given string is palindrome or not: Software

Given String is Not Palindrome

``````

## Concat two strings without using Library or string function in C

In this program we are accepting two strings from user using Gets. After that by calculating length of first string program is using for loop to append each character of second string to first string.

``````/* Program Name: Concatenate Two String without using in built-in function concat.
*/
#include<stdio.h>
#include<string.h>
void main()
{
char string1, string2;
int str1Len,str2Len;

printf("\nEnter String 1 :\t");
gets(string1);
printf("\nEnter String 2 :\t");
gets(string2);

//identify entered string length using strlen function.
str1Len=strlen(string1);

// now using for loop we will add second string into first character by character.
for (str2Len=0;string2[str2Len]!='\0';str1Len++,str2Len++)
string1[str1Len]=string2[str2Len];
string1[str1Len]='\0';

printf("\n\nConcated string is :\t%s", string1);

}

``````

#### Output

``````
Enter String 1 :        Learning C Language

Enter String 2 :        Is Easy

Concated string is :    Learning C LanguageIs Easy``````

## Concatenating two strings in C with Function strcat

Concatenating means joining two strings for example first and last name.

strcat function belongs to string.h header file.

syntax for strcat function is char *strcat(char *stringOne, const char *stringTwo);

Below is code or example for strcat function.

``````
//Program Name: strcat function example in c

#include <stdio.h>
#include <string.h>

int main () {
char stringOne, stringTwo;

printf ("\n\tEnter first String\t");
scanf("%s",stringOne);
printf("\n\tEnter Second String\t");
scanf("%s",stringTwo);

strcat(stringOne,stringTwo);

printf("\nHere is concatenated string after using stcat function\n\n %s",stringOne);

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

#### Output

``````

Enter first String	Learning

Enter Second String	-C

Here is concatenated string after using stcat function

Learning-C

``````

## C Program for Temperature conversion from Fahrenheit to Celsius and kelvin

To convert temperature from Fahrenheit to Celsius we are using formula as (temperature – 32) * (5/9)

To convert temperature from Fahrenheit to Celsius we are using formula as (temperature – 32) * (5/9) + 273.15

``````//Accept Temperature in fahrenheit (F) and print it in Celsius(C) acn Kelvin(K).
// fahrenheit to Celsius = (Temp°F − 32) × 5/9 = °C
// fahrenheit to kelvin = (Temp°F − 32) × 5/9 + 273.15 = 273.15K

#include<stdio.h>
#include<conio.h>

void main()
{
float F_Temp,C_Temp,K_Temp;// declaring variable as float as Temperature can be in decimal.
printf("\n\t Enter Temperature in Fahrenheit: ");
//Now converting F_Temp to Celsius and Kelvin
C_Temp=(F_Temp-32.0)*(5.0/9.0);
K_Temp=((F_Temp-32.0)*(5.0/9.0))+273.15;
printf("\n\t Temperature in Celsius= %.2f °C",C_Temp);
printf("\n\t Temperature in Kelvin = %.2f K",K_Temp);  //Printing results up to 2 decimal point

getch();
}``````

#### Output

``````

Enter Temperature in Fahrenheit: 67

Temperature in Celsius= 19.44 °C

Temperature in Kelvin = 292.59 K

``````

In Above program we can directly convert Celsius Temperature to Kelvin by adding 273.15k

## C Program to check upper or lower case character without Function.

Program is accepting any character from user. We compare user input with ASCII value of A to Z or a to z. ASCII value of lower case a to z start from 97 to 122. ASCII value of uppercase A to Z start from 65 to 90.

``````/******************************************************************
Program Name: Write program to check whether given character is a
digit or character in lowercase or uppercase alphabet without using any function
**********************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
//variable declaration to store user input
char ch;
//ASCII value of a to z is between 97 to 122
//ASCII value of A to Z is between 65 to 90
printf("\n\tEnter Character : ");
ch=getchar();
if((ch>=48)&&(ch<=57))
{
printf("\n\t %c is integer ",ch);
}
else
{
if((ch>=97)&&(ch<=122))
printf("\n\t '%c' is Lowercase alphabet character ",ch);
else
{
if((ch>=65)&&(ch<=90))
printf("\n\t '%c' is Uppercase alphabet character ",ch);
else
printf("\n\t '%c' is not alphabet or number",ch);
}
}
getch();
}``````

#### Output

``````Enter Character : D

'D' is Uppercase alphabet character ``````
``````    Enter Character : d
'd' is Lowercase alphabet character``````
``````
Enter Character : \$
'\$' is not alphabet or number``````
``````
Enter Character : 1

1 is integer

``````

## C Program to check character is vowel or consonant using command line argument

Below code will check whether character passed from command line is vowel or consonant.

“A”, “E”,”I”,”O”,”U” either in upper or lower case character are vowels. Other than “A”, “E”,”I”,”O”,”U” all characters between A to Z are consonant.

``````//Program Name: Accept a lower or Upper case character from the user from command line
//for example program name character (main.c a)
//and check whether the character is a vowel or consonant.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(int argc, char *argv[])
{
char ch;
//we are storing first character of second parameter in below way
ch=argv;

if((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z'))
{
if(ch=='a'||ch=='i'||ch=='o'||ch=='u'||ch=='e' ||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
printf("\n\n %c is Vowel ",ch);
else
printf("\n\n %c is Consonant ",ch);
}
else
printf("\n\n %c is not character",ch);

getch();
}``````

#### Output

Enter character to check for Vowel or Consonant: a

a is not character

a is Vowel

While running above code from command line need to pass two arguments like “program name” “character” for e.g main.c i and then press enter. Program will read second string of command line and capture first character of that string and store it in variable “ch”. In If condition it is validating whether entered character is in between lower/upper case A to Z. After successful validation it is comparing characters with vowels if not matched then it shows output as consonant.

Any character which does not belong to A to Z, Program is treating it as “invalid input”.

## C Program to Calculate final velocity.

Below Running code will calculate the final velocity based on user input.

Program is using formula v = u + at. The assumption is the user will enter values in decimal and time should not be negative.

``````//Program Name: Calculate Final Velocity.
//formula v= u + at
//u = initial velocity
//v = final velocity
//a = acceleration
//t = time

#include<stdio.h>
void main(){

//declare variable
float u,t, a, v;

//get value of initial velocity
printf("Enter the Value Of Initial Velocity  ");
scanf("%f",&u);

printf("Enter the Value Of Time  ");
scanf("%f",&t);

printf("Enter the Value Of acceleration  ");
scanf("%f",&a);

//Calculate only if time is not negative
if( t > 0 )
{
// formula to calculate final velocity
v = ( u + ( a * t ) );
printf("With the initial velocity %.2f, time %.2f and acceleration %.2f, \n The value of final velocity is %f m/s", u, t, a, v);
}
else
{
printf("Entered time is negative");
}
}``````

#### Output

Enter the Value Of Initial Velocity 27
Enter the Value Of Time 3
Enter the Value Of acceleration 2
With the initial velocity 27.00, time 3.00 and acceleration 2.00,
The value of final velocity is 33.000000 m/s

Enter the Value Of Initial Velocity 27
Enter the Value Of Time 0
Enter the Value Of acceleration 2
Entered time is negative

Users can enter velocity-time and acceleration in decimal values so we declare variables as a float. The program also shows how to display decimal points up to 2 digits by using %.2f

## C Program to check character is vowel or consonant

This C Language code will check whether entered character is vowel or consonant.

“A”, “E”,”I”,”O”,”U” either in upper or lower case character are vowels. Other than “A”, “E”,”I”,”O”,”U” all characters between A to Z are consonant.

``````
//Program Name: Accept a lower or Upper case character from the user
//and check whether the character is a vowel or consonant.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("\nEnter character to check for Vowel or Consonant: ");
ch=getchar();

if((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z'))
{
if(ch=='a'||ch=='i'||ch=='o'||ch=='u'||ch=='e' ||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
printf("\n\n %c is Vowel ",ch);
else
printf("\n\n %c is Consonant ",ch);
}
else
printf("\n\n %c is not character",ch);

getch();
}``````

#### Output

Enter character to check for Vowel or Consonant: p

p is Consonant

Enter character to check for Vowel or Consonant: e

e is Vowel

Enter character to check for Vowel or Consonant: 3

3 is not character

Program is accepting input as single character and storing it in variable “ch”. In If condition it is validating whether entered character is in between lower/upper case A to Z. After successful validation it is comparing characters with vowels if not matched then it shows output as consonant.

Any character which does not belong to A to Z, Program is treating it as “invalid input”.