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

String & Character Based Solved Program

The document contains C program code snippets to demonstrate various concepts like loops, conditional statements, functions, character operations etc. It includes programs to find ASCII value, check characters, numbers, loops to display characters and calculate products, averages with examples of break and continue statements.

Uploaded by

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

String & Character Based Solved Program

The document contains C program code snippets to demonstrate various concepts like loops, conditional statements, functions, character operations etc. It includes programs to find ASCII value, check characters, numbers, loops to display characters and calculate products, averages with examples of break and continue statements.

Uploaded by

vickroid007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ASCII value of characters in C programming:-

/* Source code to find ASCII value of a character entered by user */

#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c",&c); /* Takes a character from user */
printf("ASCII value of %c = %d",c,c);
return 0;
}

C Program to Display Character from A to Z Using Loop


/* C program to display character from A to Z using loops. */

#include <stdio.h>
int main()
{
char c;
for(c='A'; c<='Z'; ++c)
printf("%c ",c);
return 0;
}

/* C program to display character from A to Z using loops either in uppercase or lowercase


depending upon the data from user */

#include <stdio.h>
int main()
{
char c;
printf("Enter u to display characters in uppercase and l to display in lowercase: ");
scanf("%c",&c);
if(c=='U' || c=='u')
{
for(c='A'; c<='Z'; ++c)
printf("%c ",c);
}
if (c=='L' || c=='l')
{
for(c='a'; c<='z'; ++c)
printf("%c ",c);
}
if (c!='U' || c!='L' || c=='u' || c=='l')
printf("Error !!!");
return 0;
}

C Program to Check Whether a Character is an Alphabet or not


1
/* C programming code to check whether a character is 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;
}
CONTINUE STATEMENT:-
Write a C program to find the product of 4 integers entered by a user.
If user enters 0 skip it.
//program to demonstrate the working of continue statement in C programming

# include <stdio.h>
int main(){
int i,num,product;
for(i=1,product=1;i<=4;++i){
printf("Enter num%d:",i);
scanf("%d",&num);
if(num==0)
continue; / *In this program, when num equals to zero, it skips the statement
product*=num and continue the loop. */
product*=num;
}
printf("product=%d",product);
return 0;
}

BREAK STATEMENT :-
Write a C program to find average of maximum of n positive numbers entered by user.
But, if the input is negative, display the average (excluding the average of negative input)
and end the program.

/* C program to demonstrate the working of break statement by terminating a loop, if user


inputs negative number*/
# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
2
if(num<0.0)
break; //for loop breaks if num<0.0
sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
return 0;
}

C Program to Convert Binary Number to Octal and vice-versa


/* C programming source code to convert either binary to octal or octal to binary
according to data entered by user. */

#include <stdio.h>
#include <math.h>
int binary_octal(int n);
int octal_binary(int n);
int main()
{
int n;
char c;
printf("Instructions:\n");
printf("1. Enter alphabet 'o' to convert binary to octal.\n");
printf("2. Enter alphabet 'b' to convert octal to binary.\n");
scanf("%c",&c);
if ( c=='o' || c=='O')
{
printf("Enter a binary number: ");
scanf("%d",&n);
printf("%d in binary = %d in octal", n, binary_octal(n));
}
if ( c=='b' || c=='B')
{
printf("Enter a octal number: ");
scanf("%d",&n);
printf("%d in octal = %d in binary",n, octal_binary(n));
}
return 0;
}
int binary_octal(int n) /* Function to convert binary to octal. */
{
int octal=0, decimal=0, i=0;
while(n!=0)
{
decimal+=(n%10)*pow(2,i);
++i;
n/=10;
}

/*At this point, the decimal variable contains corresponding decimal value of binary number. */

3
i=1;
while (decimal!=0)
{
octal+=(decimal%8)*i;
decimal/=8;
i*=10;
}
return octal;
}
int octal_binary(int n) /* Function to convert octal to binary.*/
{
int decimal=0, binary=0, i=0;
while (n!=0)
{
decimal+=(n%10)*pow(8,i);
++i;
n/=10;
}
/* At this point, the decimal variable contains corresponding decimal value of that octal number.
*/
i=1;
while(decimal!=0)
{
binary+=(decimal%2)*i;
decimal/=2;
i*=10;
}
return binary;
}

C Program to Convert Binary Number to Decimal and vice-versa


/* C programming source code to convert either binary to decimal or decimal to binary
according to data entered by user. */

#include <stdio.h>
#include <math.h>
int binary_decimal(int n);
int decimal_binary(int n);
int main()
{
int n;
char c;
printf("Instructions:\n");
printf("1. Enter alphabet 'd' to convert binary to decimal.\n");
printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
scanf("%c",&c);
if (c =='d' || c == 'D')
{
printf("Enter a binary number: ");
scanf("%d", &n);
printf("%d in binary = %d in decimal", n, binary_decimal(n));

4
}
if (c =='b' || c == 'B')
{
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %d in binary", n, decimal_binary(n));
}
return 0;
}

int decimal_binary(int n) /* Function to convert decimal to binary.*/


{
int rem, i=1, binary=0;
while (n!=0)
{
rem=n%2;
n/=2;
binary+=rem*i;
i*=10;
}
return binary;
}

int binary_decimal(int n) /* Function to convert binary to decimal.*/

{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}

C Program to Check Prime or Armstrong Number Using User-defined Function

/* C program to check either prime number or Armstrong number depending upon the
data entered by user. */

#include <stdio.h>
int prime(int n);
int armstrong(int n);
int main()
{
char c;
int n,temp=0;
printf("Eneter a positive integer: ");
scanf("%d",&n);
printf("Enter P to check prime and A to check Armstrong number: ");
5
c=getche();
if (c=='p' || c=='P')
{
temp=prime(n);
if(temp==1)
printf("\n%d is a prime number.", n);
else
printf("\n%d is not a prime number.", n);
}
if (c=='a' || c=='A')
{
temp=armstrong(n);
if(temp==1)
printf("\n%d is an Armstrong number.", n);
else
printf("\n%d is not an Armstrong number.",n);
}
return 0;
}
int prime(int n)
{
int i, flag=1;
for(i=2; i<=n/2; ++i)
{
if(n%i==0)
{
flag=0;
break;
}
}
return flag;
}
int armstrong(int n)
{
int num=0, temp, flag=0;
temp=n;
while(n!=0)
{
num+=(n%10)*(n%10)*(n%10);
n/=10;
}
if (num==temp)
flag=1;
return flag;
}

You might also like