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

Techiez Online: C Programming Questions & Answers For Placements Basic To Advanced

This document provides 14 code examples of basic C programming problems and their solutions. The examples include programs to calculate the sum and product of two numbers, find the perimeter and area of rectangles and triangles, convert between units like Celsius to Fahrenheit and days to years/weeks/days, calculate square roots and more. The document encourages joining a Telegram channel for more C programming questions and answers.

Uploaded by

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

Techiez Online: C Programming Questions & Answers For Placements Basic To Advanced

This document provides 14 code examples of basic C programming problems and their solutions. The examples include programs to calculate the sum and product of two numbers, find the perimeter and area of rectangles and triangles, convert between units like Celsius to Fahrenheit and days to years/weeks/days, calculate square roots and more. The document encourages joining a Telegram channel for more C programming questions and answers.

Uploaded by

Rahul Trikoti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

JOIN TELEGRAM CHANNEL - @TechieZ_Online

C PROGRAMMING QUESTIONS & ANSWERS FOR PLACEMENTS

BASIC TO ADVANCED

A Product from
TechieZ Online
JOIN TELEGRAM CHANNEL - @TechieZ_Online 1|P a g e
JOIN TELEGRAM CHANNEL - @TechieZ_Online

List of all Basic Programs

1. Write a C program to enter two numbers and find their sum.


#include<stdio.h>
int main() {
int a, b, sum;
printf("\nEnter two no: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum : %d", sum);
return(0);

2. Write a C program to enter two numbers and perform all arithmetic


operations.

#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting

printf("Sum = %d\n", add);


printf("Difference = %d\n", subtract);
printf("Multiplication = %d\n", multiply);
printf("Division = %.2f\n", divide);

return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 2|P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

3. Write a C program to enter length and breadth of a rectangle and find


its perimeter.

#include <stdio.h>
int main()
{
float length, width, perimeter;
/*
* Input length and width of rectangle from user
*/
printf("Enter length of the rectangle: ");
scanf("%f", &length);
printf("Enter width of the rectangle: ");
scanf("%f", &width);
/* Calculate perimeter of rectangle */
perimeter = 2 * (length + width);
/* Print perimeter of rectangle */
printf("Perimeter of rectangle = %f units ", perimeter);
return 0;
}

4. Write a C program to enter length and breadth of a rectangle and find


its area.

#include <stdio.h>
int main()
{
float length, width, area;
/*
* Input length and width of rectangle
*/
printf("Enter length of rectangle: ");
scanf("%f", &length);
printf("Enter width of rectangle: ");
scanf("%d", &width);
/* Calculate area of rectangle */
area = length * width;
/* Print area of rectangle */
printf("Area of rectangle = %f sq. units ", area);
return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 3|P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

5. Write a C program to enter radius of a circle and find its diameter,


circumference and area.
#include <stdio.h>
int main()
{
float radius, diameter, circumference, area;
/*
* Input radius of circle from user
*/
printf("Enter radius of circle: ");
scanf("%f", &radius);

/Calculate diameter, circumference and area


diameter = 2 * radius;
circumference = 2 * 3.14 * radius;
area = 3.14 * (radius * radius);

printf("Diameter of circle = %.2f units \n", diameter);


printf("Circumference of circle = %.2f units \n", circumference);
printf("Area of circle = %.2f sq. units ", area);
return 0; }

6. Write a C program to enter length in centimeter and convert it into


meter and kilometer.
#include <stdio.h>
int main()
{
float cm, meter, km;
/* Input length in centimeter from user */
printf("Enter length in centimeter: ");
scanf("%f", &cm);
/* Convert centimeter into meter and kilometer */
meter = cm / 100.0;
km = cm / 100000.0;
printf("Length in Meter = %.2f m \n", meter);
printf("Length in Kilometer = %.2f km", km);

return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 4|P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

7. Write a C program to enter temperature in °Celsius and convert it into


°Fahrenheit.

#include <stdio.h>
int main()
{
float celsius, fahrenheit;

/* Input temperature in celsius */


printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);

/* celsius to fahrenheit conversion formula */


fahrenheit = (celsius * 9 / 5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);
return 0;
}
8. Write a C program to convert days into years, weeks and days.

#include <stdio.h>
#define DAYSINWEEK 7
void main()
{
int ndays, year, week, days;

printf("Enter the number of days\n");


scanf("%d", &ndays);
year = ndays / 365;
week = (ndays % 365) / DAYSINWEEK;
days = (ndays % 365) % DAYSINWEEK;
printf ("%d is equivalent to %d years, %d weeks and %d daysn",
ndays, year, week, days);
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 5|P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

9. Write a C program to find power of any number xy (x^y).

#include <stdio.h>
int main()
{
int base, exponent;
long long power = 1;
int i;
/* Input base and exponent from user */
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
/* Multiply base, exponent times*/
for(i=1; i<=exponent; i++)
{
power = power * base;
}
printf("%d ^ %d = %lld", base, exponent, power);
return 0;
}

10. Write a C program to enter any number and calculate its square root.

#include <stdio.h>
#include <math.h>
int main()
{
double num, root;
/* Input a number from user */
printf("Enter any number to find square root: ");
scanf("%lf", &num);
/* Calculate square root of num */
root = sqrt(num);
/* Print the resultant value */
printf("Square root of %.2lf = %.2lf", num, root);
return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 6|P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

11. Write a C program to enter two angles of a triangle and find the third
angle.

#include <stdio.h>
int main()
{
int a, b, c;

/* Input two angles of the triangle */


printf("Enter two angles of triangle: ");
scanf("%d%d", &a, &b);
/* Compute third angle */
c = 180 - (a + b);
/* Print value of the third angle */
printf("Third angle of the triangle = %d", c);
return 0;
}

12. Write a C program to enter base and height of a triangle and find its
area.

#include <stdio.h>
int main()
{
float base, height, area;
/* Input base and height of triangle */
printf("Enter base of the triangle: ");
scanf("%f", &base);
printf("Enter height of the triangle: ");
scanf("%f", &height);
/* Calculate area of triangle */
area = (base * height) / 2;
/* Print the resultant area */
printf("Area of the triangle = %.2f sq. units", area);
return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 7|P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

13. Write a C program to calculate area of an equilateral triangle.


#include <stdio.h>
#include <math.h> // Used for sqrt() function
int main()
{
float side, area;
/* Input side of equilateral triangle */
printf("Enter side of an equilateral triangle: ");
scanf("%f", &side);

/* Calculate area of equilateral triangle */


area = (sqrt(3) / 4) * (side * side);

/* Print resultant area */


printf("Area of equilateral triangle = %.2f sq. units", area);
return 0;
}

14. Write a C program to enter marks of five subjects and calculate total,
average and percentage.
#include <stdio.h>
int main()
{
float eng, phy, chem, math, comp;
float total, average, percentage;

/* Input marks of all five subjects */


printf("Enter marks of five subjects: \n");
scanf("%f%f%f%f%f", &eng, &phy, &chem, &math, &comp);

/* Calculate total, average and percentage */


total = eng + phy + chem + math + comp;
average = total / 5.0;
percentage = (total / 500.0) * 100;

/* Print all results */


printf("Total marks = %.2f\n", total);
printf("Average marks = %.2f\n", average);
printf("Percentage = %.2f", percentage);
return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 8|P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

15. Write a C program to enter P, T, R and calculate Simple Interest.


#include <stdio.h>
int main()
{
float principle, time, rate, SI;

/* Input principle, rate and time */


printf("Enter principle (amount): ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);

/* Calculate simple interest */


SI = (principle * time * rate) / 100;

/* Print the resultant value of SI */


printf("Simple Interest = %f", SI);
return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 9|P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

List of all Conditional operator Programs


1. Write a C program to find maximum between two numbers using
conditional/ternary operator.
#include <stdio.h>
int main()
{
int num1, num2, max;

/Input two number from user


printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
max = (num1 > num2) ? num1 : num2;
printf("Maximum between %d and %d is %d", num1, num2, max);
return 0;
}

2. Write a C program to find maximum between three numbers using


conditional/ternary operator.

#include <stdio.h>
int main()
{
int num1, num2, num3, max;

/Input three numbers from user


printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);

max = (num1 > num2 && num1 > num3) ? num1 :


(num2 > num3) ? num2 : num3;
printf("\nMaximum between %d, %d and %d = %d", num1, num2,
num3, max);
return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 10 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

3. Write a C program to check whether a number is even or odd using


conditional/ternary operator.
#include <stdio.h>
int main()
{
int num;
printf("Enter any number to check even or odd: ");
scanf("%d", &num);
(num%2 == 0)
? printf("The number is EVEN")
: printf("The number is ODD");
return 0;
}

4. Write a C program to check whether year is leap year or not using


conditional/ternary operator.
#include <stdio.h>
int main()
{
int year;
printf("Enter any year: ");
scanf("%d", &year);

(year%4==0 && year%100!=0) ? printf("LEAP YEAR") :


(year%400 ==0 ) ? printf("LEAP YEAR") : printf("COMMON
YEAR");

return 0;
}

5. Write a C program to check whether character is an alphabet or not


using conditional/ternary operator.
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
(ch>='a' && ch<='z') || (ch>='A' && ch<='Z')
? printf("It is ALPHABET")
: printf("It is NOT ALPHABET"); return 0; }

JOIN TELEGRAM CHANNEL - @TechieZ_Online 11 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

List of all Looping Programs

1. Write a C program to print all natural numbers from 1 to n. - using


while loop
#include <stdio.h>
int main()
{
int i, end;
printf("Print all natural numbers from 1 to : ");
scanf("%d", &end);

i=1;
while(i<=end)
{
printf("%d\n", i);
i++;
}
return 0;
}
2. Write a C program to print all natural numbers in reverse (from n to 1).
- using while loop
#include <stdio.h>

int main()
{
int n;
printf("Enter value of n: ");
scanf("%d", &n);

while(n>=1)
{
printf("%d\n", n);
n--;
}
return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 12 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

3. Write a C program to print all alphabets from a to z. - using while loop


#include <stdio.h>
int main()
{
char ch = 'a';

printf("Alphabets from a - z are: \n");


while(ch<='z')
{
printf("%c\n", ch);
ch++;
}
return 0;
}
4. Write a C program to print all even numbers between 1 to 100. - using
while loop
#include <stdio.h>
int main()
{
int i, n;

// Input upper limit of even number from user


printf("Print all even numbers till: ");
scanf("%d", &n);

printf("All even numbers from 1 to %d are: \n", n);

/Starts loop counter from 1, increments by 1 till i<=n


i=1;
while(i<=n)
{
/* Check even condition before printing */
if(i%2==0)
{
printf("%d\n", i);
}
i++;
}

return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 13 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

5. Write a C program to print sum of all even numbers between 1 to n.

#include <stdio.h>
int main()
{
int i, n, sum=0;

/* Input upper limit from user */


printf("Enter upper limit: ");
scanf("%d", &n);

for(i=2; i<=n; i+=2)


{
/* Add current even number to sum */
sum += i;
}
printf("Sum of all even number between 1 to %d = %d", n, sum);

return 0;
} }
6. Write a C program to print sum of all odd numbers between 1 to n.
#include <stdio.h>
int main()
{
int i, n, sum=0;

/* Input range to find sum of odd numbers */


printf("Enter upper limit: ");
scanf("%d", &n);

/* Find the sum of all odd number */


for(i=1; i<=n; i+=2)
{
sum += i;
}

printf("Sum of odd numbers = %d", sum);

return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 14 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

7. Write a C program to print table of any number.

#include <stdio.h>
int main()
{
int i, num;

/* Input a number to print table */


printf("Enter number to print table: ");
scanf("%d", &num);

for(i=1; i<=10; i++)


{
printf("%d * %d = %d\n", num, i, (num*i));
}

return 0;
}

8. Write a C program to find first and last digit of any number.

#include <stdio.h>
int main()
{
int n, lastDigit;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &n);

/* Get the last digit */


lastDigit = n % 10;

printf("Last digit = %d", lastDigit);

return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 15 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

9. Write a C program to count number of digits in any number.


#include <stdio.h>
int main()
{
long long num;
int count = 0;
/* Input number from user */
printf("Enter any number: ");
scanf("%lld", &num);
/* Run loop till num is greater than 0 */
while(num != 0)
{
/* Increment digit count */
count++;
/* Remove last digit of 'num' */
num /= 10;
}
printf("Total digits: %d", count);
return 0;
}

10.Write a C program to calculate sum of digits of any number.

#include <stdio.h>
int main()
{
int num, sum=0;
/* Input a number from user */
printf("Enter any number to find sum of its digit: ");
scanf("%d", &num);
/* Repeat till num becomes 0 */
while(num!=0)
{
/* Find last digit of num and add to sum */
sum += num % 10;

/* Remove last digit from num */


num = num / 10;
}
printf("Sum of digits = %d", sum);
return 0; }

JOIN TELEGRAM CHANNEL - @TechieZ_Online 16 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

11.Write a C program to calculate product of digits of any number.

#include <stdio.h>
int main()
{
int num;
long long product=1;
/* Input number from user */
printf("Enter any number to calculate product of digit: ");
scanf("%d", &num);
product = (num == 0 ? 0 : 1);
/* Repeat the steps till num becomes 0 */
while(num != 0)
{
/* Get the last digit from num and multiplies to product */
product = product * (num % 10);
/* Remove the last digit from n */
num = num / 10;
}
printf("Product of digits = %lld", product);
return 0;
}

12.Write a C program to swap first and last digits of any number.


#include <stdio.h>
#include <math.h>
int main()
{
int num, swappedNum;
int firstDigit, lastDigit, digits;
/* Input number from user */
printf("Enter any number: ");
scanf("%d", &num);
/* Find last digit */
lastDigit = num % 10;
/* Find total number of digit - 1 */
digits = (int)log10(num);
/* Find first digit */
firstDigit = (int)(num / pow(10, digits));
swappedNum = lastDigit;
swappedNum *= (int) pow(10, digits);
swappedNum += num % ((int) pow(10, digits));
swappedNum -= lastDigit;
swappedNum += firstDigit;
printf("Original number = %d", num);
printf("Number after swapping first and last digit: %d", swappedNum);

return 0; }

JOIN TELEGRAM CHANNEL - @TechieZ_Online 17 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

13.Write a C program to enter any number and print its reverse.

#include <stdio.h>
int main()
{
int i, start;
/* Input start range from user */
printf("Enter starting value: ");
scanf("%d", &start);
for(i=start; i>=1; i--)
{
printf("%d\n", i);
}
return 0;
}
14.Write a C program to enter any number and check whether the number
is palindrome or not.

#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
// reversed integer is stored in variable
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);
return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 18 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

15.Write a C program to find frequency of each digit in a given integer.


#include <stdio.h>
#define BASE 10 /* Constant */
int main()
{
long long num, n;
int i, lastDigit;
int freq[BASE];
printf("Enter any number: ");
scanf("%lld", &num);
for(i=0; i<BASE; i++)
{
freq[i] = 0;
}
n = num;
while(n != 0)
{
lastDigit = n % 10;
n /= 10;
freq[lastDigit]++;
}
printf("Frequency of each digit in %lld is: \n", num);
for(i=0; i<BASE; i++)
{
printf("Frequency of %d = %d\n", i, freq[i]);
}
return 0;
}
16.Write a C program to enter any number and print it in words.

#include <stdio.h>
int main()
{
int n, num = 0;
/* Input number from user */
printf("Enter any number to print in words: ");
scanf("%d", &n);
/* Store reverse of n in num */
while(n != 0)
{
num = (num * 10) + (n % 10);

JOIN TELEGRAM CHANNEL - @TechieZ_Online 19 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

n /= 10;
}
while(num != 0)
{
switch(num % 10)
{
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
}
num = num / 10;
}
return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 20 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

17.Write a C program to print all ASCII character with their values

#include <stdio.h>
int main()
{
int i;
/* Print ASCII values from 0 to 255 */
for(i=0; i<=255; i++)
{
printf("ASCII value of character %c = %d\n", i, i);
}
return 0;
}

18.Write a C program to enter any number and print all factors of the
number.

#include <stdio.h>
int main()
{
int i, num;

printf("Enter any number to find its factor: ");


scanf("%d", &num);
printf("All factors of %d are: \n", num);

for(i=1; i<=num; i++)


{
if(num % i == 0)
{
printf("%d, ",i);
}
}

return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 21 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

19.Write a C program to enter any number and calculate its factorial.

int main()
{
int i, Number;
long Factorial = 1;
printf("\n Please Enter any number to Find Factorial\n");
scanf("%d", &Number);
for (i = 1; i <= Number; i++)
{
Factorial = Factorial * i;
}
printf("\nFactorial of %d = %d\n", Number, Factorial);
return 0;
}

20.Write a C program to find HCF (GCD) of two numbers..

#include <stdio.h>
int main()
{
int i, num1, num2, min, hcf=1;
/* Input two numbers from user */
printf("Enter any two numbers to find HCF: ");
scanf("%d%d", &num1, &num2);
/* Find minimum between two numbers */
min = (num1<num2) ? num1 : num2;
for(i=1; i<=min; i++)
{
/* If i is factor of both number */
if(num1%i==0 && num2%i==0)
{
hcf = i;
}
}

printf("HCF of %d and %d = %d\n", num1, num2, hcf);


return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 22 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

21.Write a C program to find LCM of two numbers.

#include <stdio.h>
int main()
{
int n1, n2, minMultiple;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);

minMultiple = (n1>n2) ? n1 : n2;


// Always true

while(1)
{
if( minMultiple%n1==0 && minMultiple%n2==0 )
{
printf("The LCM of %d and %d is %d.", n1, n2,minMultiple);
break;
}
++minMultiple;
}
return 0;
}

22.Write a C program to check whether a number is Prime number or not.

#include <stdio.h>
int main()
{
int low, high, i, flag;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Prime numbers between %d and %d are: ", low, high
while (low < high)
{
flag = 0;
for(i = 2; i <= low/2; ++i)
{
if(low % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", low);
++low;
}
return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 23 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

23.Write a C program to check whether a number is Armstrong number


or not.
#include <stdio.h>
int main()
{
int number, originalNumber, remainder, result = 0;
printf("Enter a three digit integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
return 0;
}

24.Write a C program to check whether a number is Perfect number or


not.
#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;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 24 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

25.Write a C program to check whether a number is Strong number or


not.

#include <stdio.h>
int main()
{
int i, originalNum, num, lastDigit, sum;
long fact;
/* Input a number from user */
printf("Enter any number to check Strong number: ");
scanf("%d", &num);
/* Copy the value of num to a temporary variable */
originalNum = num;
sum = 0;
/* Find sum of factorial of digits */
while(num > 0)
{
/* Get last digit of num */
lastDigit = num % 10;
/* Find factorial of last digit */
fact = 1;
for(i=1; i<=lastDigit; i++)
{
fact = fact * i;
}
/* Add factorial to sum */
sum = sum + fact;
num = num / 10;
}
/* Check Strong number condition */
if(sum == originalNum)
{
printf("%d is STRONG NUMBER", originalNum);
}
else
{
printf("%d is NOT STRONG NUMBER", originalNum);
}
return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 25 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

26.Write a C program to print Fibonacci series up to n terms.

#include <stdio.h>
int main()
{
int a, b, c, i, terms
/* Input number from user */
printf("Enter number of terms: ");
scanf("%d", &terms);
/* Fibonacci magic initialization */
a = 0;
b = 1;
c = 0;

printf("Fibonacci terms: \n");


/* Iterate through n terms */
for(i=1; -i<=terms; i++)
{
printf("%d, ", c);
a = b; // Copy n-1 to n-2
b = c; // Copy current to n-1
c = a + b; // New term
}
return 0;
}

List of all array Programs:

1. Write a C program to read and print elements of array. - using


recursion.

#include <stdio.h>
#define MAX_SIZE 100
/* Function declaration */
void printArray(int arr[], int start, int len);
int main()
{
int arr[MAX_SIZE];
int N, i;
/* Input size and elements in array */
printf("Enter size of the array: ");

JOIN TELEGRAM CHANNEL - @TechieZ_Online 26 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

scanf("%d", &N);
printf("Enter elements in the array: ");
for(i=0; i<N; i++)
{
scanf("%d ", &arr[i]);
}
/* Prints array recursively */
printf("Elements in the array: ");
printArray(arr, 0, N);
return 0;
}
2. Write a C program to find sum of all array elements. - using recursion.

#include <stdio.h>
#define MAX_SIZE 100

/* Function declaration to find sum of array */


int sum(int arr[], int start, int len);

int main()
{
int arr[MAX_SIZE];
int N, i, sumofarray;

/* Input size and elements in array */


printf("Enter size of the array: ");
scanf("%d", &N);
printf("Enter elements in the array: ");
for(i=0; i<N; i++)
{
scanf("%d", &arr[i]);
}
sumofarray = sum(arr, 0, N);
printf("Sum of array elements: %d", sumofarray);
return 0;
}

/**
* Recursively find the sum of elements in an array.

JOIN TELEGRAM CHANNEL - @TechieZ_Online 27 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

*/
int sum(int arr[], int start, int len)
{
/* Recursion base condition */
if(start >= len)
return 0;
return (arr[start] + sum(arr, start + 1, len));
}

3. Write a C program to find maximum and minimum element in an array.


- using recursion.

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array
/* Function declarations */
int maximum(int array[], int index, int len);
int minimum(int array[], int index, int len);
int main()
{
int array[MAX_SIZE], N, max, min;
int i;

/* Input size and elements of array */


printf("Enter size of the array: ");
scanf("%d", &N);
printf("Enter %d elements in array: ", N);
for(i=0; i<N; i++)
{
scanf("%d", &array[i]);
}

max = maximum(array, 0, N);


min = minimum(array, 0, N);

printf("Minimum element in array = %d\n", min);


printf("Maximum element in array = %d\n", max);

return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 28 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

/**
* Recursive function to find maximum element in the given array.
*/
int maximum(int array[], int index, int len)
{
int max;

/*
* Only last and second last element are left
*/
if(index >= len-2)
{
if(array[index] > array[index + 1])
return array[index];
else
return array[index + 1];
}

/* Recursively call maximum to find maximum element in


* right side of the array from current index.*/
max = maximum(array, index + 1, len);
/* Compare the current array element with maximum
* element on its right side*/
if(array[index] > max)
return array[index];
else
return max;
}

/**
* Recursive function to find minimum element in the array.
*/
int minimum(int array[], int index, int len)
{
int min;
if(index >= len-2)
{
if(array[index] < array[index + 1])
return array[index];
else
return array[index + 1];

JOIN TELEGRAM CHANNEL - @TechieZ_Online 29 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

}
min = minimum(array, index + 1, len);
if(array[index] < min)
return array[index];
else
return min;
}

4. Write a C program to find second largest element in an array.

#include <stdio.h>
#include <limits.h> // For INT_MIN
#define MAX_SIZE 1000 // Maximum array size
int main()
{
int arr[MAX_SIZE], size, i;
int max1, max2;
/* Input size of the array */
printf("Enter size of the array (1-1000): ");
scanf("%d", &size);
/* Input array elements */
printf("Enter elements in the array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
max1 = max2 = INT_MIN;
/*
* Check for first largest and second
*/
for(i=0; i<size; i++)
{
if(arr[i] > max1)
{
/*
* If current element of the array is first largest
* then make current max as second max
* and then max as current array element

JOIN TELEGRAM CHANNEL - @TechieZ_Online 30 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

*/
max2 = max1;
max1 = arr[i];
}
else if(arr[i] > max2 && arr[i] < max1)
{
/*
* If current array element is less than first largest
* but is greater than second largest then make it
* second largest
*/
max2 = arr[i];
}
}
printf("First largest = %d\n", max1);
printf("Second largest = %d", max2);
return 0;
}

5. Write a C program to copy all elements from an array to another array.

#include <stdio.h>
#define MAX_SIZE 100

int main()4
{
int source[MAX_SIZE], dest[MAX_SIZE];
int i, size;

/* Input size of the array */


printf("Enter the size of the array : ");
scanf("%d", &size);

/* Input array elements */


printf("Enter elements of source array : ");
for(i=0; i<size; i++)

JOIN TELEGRAM CHANNEL - @TechieZ_Online 31 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

{
scanf("%d", &source[i]);
}6666

/*
* Copy all elements from source array to dest array
*/
for(i=0; i<size; i++)
{
dest[i] = source[i];
}

/*
* Print all elements of source array
*/
printf("\nElements of source array are : ");
for(i=0; i<size; i++)
{
printf("%d\t", source[i]);
}

/*
* Print all elements of dest array
*/
printf("\nElements of dest array are : ");
for(i=0; i<size; i++)
{
printf("%d\t", dest[i]);
}

return 0;
}
Learn how to copy array elements using pointers.
Output
Enter the size of the array : 10
Enter elements of source array : 10 20 30 40 50 60 70 80 90 100

Elements of source array are :


10 20 30 40 50 60 70 80 90 10
0

JOIN TELEGRAM CHANNEL - @TechieZ_Online 32 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

Elements of dest array are :


10 20 30 40 50 60 70 80 90 10
0

6. Write a C program to insert an element in an array.

#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int i, size, num, pos;
/* Input size of the array */
printf("Enter size of the array : ");
scanf("%d", &size);
/* Input elements in array */
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
/* Input new element and position to insert */
printf("Enter element to insert : ");
scanf("%d", &num);
printf("Enter the element position : ");
scanf("%d", &pos);
/* If position of element is not valid */
if(pos > size+1 || pos <= 0)
{
printf("Invalid position! Please enter position between 1 to %d",
size);
}
else
{
/* Make room for new array element by shifting to right */
for(i=size; i>=pos; i--)
{
arr[i] = arr[i-1];
}
/* Insert new element at given position and increment size */
arr[pos-1] = num;

JOIN TELEGRAM CHANNEL - @TechieZ_Online 33 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

size++;
/* Print array after insert operation */
printf("Array elements after insertion : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}
}
return 0;
}
7. Write a C program to delete an element from an array at specified
position.

#include <stdio.h>
#define MAX_SIZE 100

int main()
{
int arr[MAX_SIZE];
int i, size, pos;

/* Input size and element in array */


printf("Enter size of the array : ");
scanf("%d", &size);
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

/* Input element position to delete */


printf("Enter the element position to delete : ");
scanf("%d", &pos);

/* Invalid delete position */


if(pos < 0 || pos > size)
{
printf("Invalid position! Please enter position between 1 to %d",
size);
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 34 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

else
{
/* Copy next element value to current element */
for(i=pos-1; i<size-1; i++)
{
arr[i] = arr[i + 1];
}

/* Decrement array size by 1 */


size--;
}

/* Print array after deletion */


printf("\nElements of array after delete are : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}
8. Write a C program to print all unique elements in the array.

#include <stdio.h>
#define MAX_SIZE 100

int main()
{
int arr[MAX_SIZE], freq[MAX_SIZE];
int size, i, j, count;

/* Input size of array and elements in array */


printf("Enter size of array: ");
scanf("%d", &size);
printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
freq[i] = -1;
}

/* Find frequency of each element */

JOIN TELEGRAM CHANNEL - @TechieZ_Online 35 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

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


{
count = 1;
for(j=i+1; j<size; j++)
{
if(arr[i] == arr[j])
{
count++;
freq[j] = 0;
}
}

if(freq[i] != 0)
{
freq[i] = count;
}
}

/* Print all unique elements of array */


printf("\nUnique elements in the array are: ");
for(i=0; i<size; i++)
{
if(freq[i] == 1)
{
printf("%d ", arr[i]);
}
}

return 0;
}
Output

9. Write a C program to print all negative elements in an array.

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main()
{

JOIN TELEGRAM CHANNEL - @TechieZ_Online 36 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

int arr[MAX_SIZE]; // Declare array of MAX_SIZE


int i, N;

/* Input size of the array */


printf("Enter size of the array : ");
scanf("%d", &N);

/* Input elements in the array */


printf("Enter elements in array : ");
for(i=0; i<N; i++)
{
scanf("%d", &arr[i]);
}

printf("\nAll negative elements in array are : ");


for(i=0; i<N; i++)
{
/* If current array element is negative */
if(arr[i] < 0)
{
printf("%d\t", arr[i]);
}
}

return 0;
} }
10.Write a C program to count total number of even and odd elements in
an array.

#include <stdio.h>
#define MAX_SIZE 100 //Maximum size of the array
int main()
{
int arr[MAX_SIZE];
int i, size, even, odd;
/* Input size of the array */
printf("Enter size of the array: ");
scanf("%d", &size);
/* Input array elements */
printf("Enter %d elements in array: ", size);
for(i=0; i<size; i++)

JOIN TELEGRAM CHANNEL - @TechieZ_Online 37 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

{
scanf("%d", &arr[i]);
}
/* Assuming that there are 0 even and odd elements */
even = 0;
odd = 0;

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


{
/* If the current element of array is even then increment even
count */
if(arr[i]%2 == 0)
{
even++;
}
else
{
odd++;
}
}
printf("Total even elements: %d\n", even);
printf("Total odd elements: %d", odd);
return 0;
}
11.Write a C program to count total number of negative elements in an
array.

#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[MAX_SIZE]; // Declares array of size 100
int i, size, count = 0;

/* Input size of array */


printf("Enter size of the array : ");
scanf("%d", &size);

/* Input array elements */


printf("Enter elements in array : ");
for(i=0; i<size; i++)

JOIN TELEGRAM CHANNEL - @TechieZ_Online 38 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

{
scanf("%d", &arr[i]);
}
/*
* Count total negative elements in array
*/
for(i=0; i<size; i++)
{
/* Increment count if current array element is negative */
if(arr[i] < 0)
{
count++;
}
}
printf("\nTotal negative elements in array = %d", count);
return 0;
}

Output
Enter size of the array : 10
Enter elements in array : 10 -2 5 -20 1 50 60 -50 -12 -9

Total negative elements in array = 5

12.Write a C program to count total number of duplicate elements in an


array.

#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[MAX_SIZE];
int i, j, size, count = 0;
/* Input size of array */
printf("Enter size of the array : ");
scanf("%d", &size);
/* Input elements in array */
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);

JOIN TELEGRAM CHANNEL - @TechieZ_Online 39 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

/*
* Find all duplicate elements in array
*/
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
/* If duplicate found then increment count by 1 */
if(arr[i] == arr[j])
{
count++;
break;
}
}
}
printf("\nTotal number of duplicate elements found in array = %d",
count);

return 0;
}

Output
Enter size of the array : 10
Enter elements in array : 1 10 20 1 25 1 10 30 25 1

Total number of duplicate elements found in array = 5

13.Write a C program to delete all duplicate elements from an array.

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array
int main()
{
int arr[MAX_SIZE]; // Declares an array of size 100
int size; // Total number of elements in array
int i, j, k; // Loop control variables
/* Input size of the array */
printf("Enter size of the array : ");
scanf("%d", &size);

JOIN TELEGRAM CHANNEL - @TechieZ_Online 40 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

/* Input elements in the array */


printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

/*
* Find duplicate elements in array
*/
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
/* If any duplicate found */
if(arr[i] == arr[j])
{
/* Delete the current duplicate element */
for(k=j; k<size; k++)
{
arr[k] = arr[k + 1];
}

/* Decrement size after removing duplicate element */


size--;

/* If shifting of elements occur then don't increment j */


j--;
}
}
}

/*
* Print array after deleting duplicate elements
*/
printf("\nArray elements after deleting duplicates : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);

JOIN TELEGRAM CHANNEL - @TechieZ_Online 41 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

return 0;
}

Output
Enter size of the array : 10
Enter elements in array : 10 20 10 1 100 10 2 1 5 10

Array elements after deleting duplicates : 10 20 1 100 2 5

14.Write a C program to count frequency of each element in an array.

#include <stdio.h>
int main()
{
int arr[100], freq[100];
int size, i, j, count;
/* Input size of array */
printf("Enter size of array: ");
scanf("%d", &size);

/* Input elements in array */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);

/* Initially initialize frequencies to -1 */


freq[i] = -1;
}

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


{
count = 1;
for(j=i+1; j<size; j++)
{
/* If duplicate element is found */
if(arr[i]==arr[j])
{

JOIN TELEGRAM CHANNEL - @TechieZ_Online 42 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

count++;
/* Make sure not to count frequency of same element again
*/
freq[j] = 0;
}
}

/* If frequency of current element is not counted */


if(freq[i] != 0)
{
freq[i] = count;
}
}

/*
* Print frequency of each element
*/
printf("\nFrequency of all elements of array : \n");
for(i=0; i<size; i++)
{
if(freq[i] != 0)
{
printf("%d occurs %d times\n", arr[i], freq[i]);
}
}

return 0;
}

Output
Enter size of array: 10
Enter elements in array: 5 10 2 5 50 5 10 1 2 2

Frequency of all elements of array :


5 occurs 3 times
10 occurs 2 times
2 occurs 3 times
50 occurs 1 times
1 occurs 1 times

15.Write a C program to merge two array to third array.

JOIN TELEGRAM CHANNEL - @TechieZ_Online 43 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array
int main()
{
int arr1[MAX_SIZE], arr2[MAX_SIZE], mergeArray[MAX_SIZE
* 2];
int size1, size2, mergeSize;
int index1, index2, mergeIndex;
int i;
/* Input size of first array */
printf("Enter the size of first array : ");
scanf("%d", &size1);
/* Input elements in first array */
printf("Enter elements in first array : ");
for(i=0; i<size1; i++)
{
scanf("%d", &arr1[i]);
}
/* Input size of second array */
printf("\nEnter the size of second array : ");
scanf("%d", &size2);
/* Input elements in second array */
printf("Enter elements in second array : ");
for(i=0; i<size2; i++)
{
scanf("%d", &arr2[i]);
}

mergeSize = size1 + size2;

/*
* Merge two array in ascending order
*/
index1 = 0;
index2 = 0;
for(mergeIndex=0; mergeIndex < mergeSize; mergeIndex++)
{
/*
* If all elements of one array

JOIN TELEGRAM CHANNEL - @TechieZ_Online 44 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

* is merged to final array


*/
if(index1 >= size1 || index2 >= size2)
{
break;
}

if(arr1[index1] < arr2[index2])


{
mergeArray[mergeIndex] = arr1[index1];
index1++;
}
else
{
mergeArray[mergeIndex] = arr2[index2];
index2++;
}
}
/*
* Merge remaining array elements
*/
while(index1 < size1)
{
mergeArray[mergeIndex] = arr1[index1];
mergeIndex++;
index1++;
}
while(index2 < size2)
{
mergeArray[mergeIndex] = arr2[index2];
mergeIndex++;
index2++;
}
/*
* Print merged array
*/
printf("\nArray merged in ascending order : ");
for(i=0; i<mergeSize; i++)
{
printf("%d\t", mergeArray[i]);

JOIN TELEGRAM CHANNEL - @TechieZ_Online 45 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

return 0;
}
16.Write a C program to find reverse of an array.

#include <stdio.h>
#define MAX_SIZE 100 // Defines maximum size of array

int main()
{
int arr[MAX_SIZE];
int size, i;

/* Input size of array */


printf("Enter size of the array: ");
scanf("%d", &size);

/* Input array elements */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

/*
* Print array in reversed order
*/
printf("\nArray in reverse order: ");
for(i = size-1; i>=0; i--)
{
printf("%d\t", arr[i]);
}

return 0;
}

17.Write a C program to search an element in an array.

#include <stdio.h>

JOIN TELEGRAM CHANNEL - @TechieZ_Online 46 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;

/* Input size of array */


printf("Enter size of array: ");
scanf("%d", &size);

/* Input elements of array */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nEnter element to search: ");


scanf("%d", &toSearch);

/* Assume that element does not exists in array */


found = 0;

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


{
/*
* If element is found in array then raise found flag
* and terminate from loop.
*/
if(arr[i] == toSearch)
{
found = 1;
break;
}
}

/*
* If element is not found in array
*/
if(found == 1)

JOIN TELEGRAM CHANNEL - @TechieZ_Online 47 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array", toSearch);
}

return 0;
} }
18.Write a C program to sort array elements in ascending order.

#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE];
int size;
int i, j, temp;

/* Input size of array */


printf("Enter size of array: ");
scanf("%d", &size);

/* Input elements in array */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

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


{
/*
* Place currently selected element array[i]
* to its correct place.
*/
for(j=i+1; j<size; j++)
{
/*

JOIN TELEGRAM CHANNEL - @TechieZ_Online 48 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

* Swap if currently selected array element


* is not at its correct position.
*/
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

/* Print the sorted array */


printf("\nElements of array in ascending order: ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}

return 0;
}
19.Write a C program to sort array elements in descending order

List of C pointers Programs

1. Program to create, initialize, assign and access a pointer variable.

#include <stdio.h>
int main()
{
int num = 10;
printf("Value of num = %d\n", num);
/* &num gets the address of num. */
printf("Address of num = %d\n", &num);
printf("Address of num in hexadecimal = %x", &num);
return 0;
}
2. Program to swap two numbers using pointers.

#include <stdio.h>
// function to swap the two numbers

JOIN TELEGRAM CHANNEL - @TechieZ_Online 49 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

void swap(int *x,int *y)


{
int t;
t = *x;
*x = *y;
*y = t;
}
int main()
{
int num1,num2;
printf("Enter value of num1: ");
scanf("%d",&num1);
printf("Enter value of num2: ");
scanf("%d",&num2);

//displaying numbers before swapping


printf("Before Swapping: num1 is: %d, num2 is:
%d\n",num1,num2);

//calling the user defined function swap()


swap(&num1,&num2);

//displaying numbers after swapping


printf("After Swapping: num1 is: %d, num2 is:
%d\n",num1,num2);
return 0;
}
3.
3. Program to change the value of constant integer using pointers
#include <stdio.h>

int main()
{
const int a=10; //declare and assign constant integer
int *p; //declare integer pointer
p=&a; //assign address into pointer p

printf("Before changing - value of a: %d",a);

//assign value using pointer


*p=20;

JOIN TELEGRAM CHANNEL - @TechieZ_Online 50 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

printf("\nAfter changing - value of a: %d",a);


printf("\nWauuuu... value has changed.");

return 0;
}

4. Program to print a string using pointer.


#include <stdio.h>
int main()
{
char str[100];
char *p;
printf("Enter any string: ");
fgets(str, 100, stdin);

/* Assigning the base address str[0] to pointer


* p. p = str is same as p = str[0]
*/
p=str;
printf("The input string is: ");
//'\0' signifies end of the string
while(*p!='\0')
printf("%c",*p++);
return 0;
}

5. Program to count vowels and consonants in a string using pointer.

#include <stdio.h>
int main()
{
char str[100];
char *p;
int vCount=0,cCount=0;

printf("Enter any string: ");


fgets(str, 100, stdin);

//assign base address of char array to pointer


p=str;

JOIN TELEGRAM CHANNEL - @TechieZ_Online 51 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

//'\0' signifies end of the string


while(*p!='\0')
{
if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U'
||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u')
vCount++;
else
cCount++;
//increase the pointer, to point next character
p++;
}

printf("Number of Vowels in String: %d\n",vCount);


printf("Number of Consonants in String: %d",cCount);
return 0;
}

6. Program to read array elements and print with addresses.

int main()
{
int arr[10]; //declare integer array
int *pa; //declare an integer pointer
int i;

pa=&arr[0]; //assign base address of array

printf("Enter array elements:\n");


for(i=0;i < 10; i++){
printf("Enter element %02d: ",i+1);
scanf("%d",pa+i); //reading through pointer
}

printf("\nEntered array elements are:");


printf("\nAddress\t\tValue\n");
for(i=0;i<10;i++){

JOIN TELEGRAM CHANNEL - @TechieZ_Online 52 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

printf("%08X\t%03d\n",(pa+i),*(pa+i));
}

return 0;
}

JOIN TELEGRAM CHANNEL - @TechieZ_Online 53 | P a g e


JOIN TELEGRAM CHANNEL - @TechieZ_Online

JOIN TELEGRAM CHANNEL - @TechieZ_Online 54 | P a g e

You might also like