Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
39 views

Programming C & C++ Record

The document provides 20 C programming examples covering topics such as: adding integers, printing integers, multiplying floating point numbers, finding ASCII values, computing quotients and remainders, swapping values with a temporary variable, checking even/odd numbers, checking vowels/consonants, finding the largest of three numbers, checking leap years, checking alphabetic characters, calculating sums of natural numbers, finding factorials, generating multiplication tables, displaying Fibonacci sequences, counting digits, reversing numbers, and checking for palindromes. Each example includes the full code and sample output.

Uploaded by

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

Programming C & C++ Record

The document provides 20 C programming examples covering topics such as: adding integers, printing integers, multiplying floating point numbers, finding ASCII values, computing quotients and remainders, swapping values with a temporary variable, checking even/odd numbers, checking vowels/consonants, finding the largest of three numbers, checking leap years, checking alphabetic characters, calculating sums of natural numbers, finding factorials, generating multiplication tables, displaying Fibonacci sequences, counting digits, reversing numbers, and checking for palindromes. Each example includes the full code and sample output.

Uploaded by

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

P Arun Reddy –Programming with C&C++

1. Write a C Program to add two integers.

#include<stdio.h>

#include<conio.h>

void main()

int num1, num2, sum;

printf(“enter two integers:” );

scanf(“%d %d", &num1,&num2);

sum= num1 + num2;

printf("%d + %d = %d", num1, num2, sum);

getch();

Output :

Enter two integers: 10

20

10 + 20 = 30.

2. Write a C Program to print an integer (entered by the user).

#include<stdio.h>

#include<conio.h>

int main()

int number;

printf("Enter an integer:");

scanf("%d", &number);

Feed back: arunpodduturi@gmail.com Page 1


P Arun Reddy –Programming with C&C++

printf(“You entered: %d", number);

getch();

return 0;

Output:

Enter an integer: 15

You entered: 15

3. Write a C Program to multiply two floating-point numbers.

#include <stdio.h>

#include<conio.h>

int main()

double a, b, product;

printf (“Enter two numbers:”);

scanf(“%lf %lf", &a, &b);

product=a * b;

printf(" Product = %lf", product);

getch();

return 0;

Output

Enter two numbers: 2.4 and 1.12

Product=2.6944.

Feed back: arunpodduturi@gmail.com Page 2


P Arun Reddy –Programming with C&C++

4. Write a C Program to find ASCII value of a character.

#include<stdio.h>

#include<conio.h>

int main()

char c;

printf("Enter a character: ");

scanf(“%c", &c);

printf("ASCII value of %c = %d", c, c);

getch();

return 0;

Output

Enter a character: G

ASCII value of G 71

5. Write a C Program to compute quotient and remainder

#include<stdio.h>

#include<conio.h>

int main()

int dividend, divisor, quotient, remainder;

printf("Enter dividend: ");

scanf("%d", &dividend);

printf("Enter divisor: ");

Feed back: arunpodduturi@gmail.com Page 3


P Arun Reddy –Programming with C&C++

scanf("%d", &divisor);

quotient= dividend / divisor;

remainder= dividend % divisor;

printf("Quotient= %d", quotient);

printf( Remainder= a", remainder);

getch();

return 0;

Output

Enter dividend: 25

Enter divisor: 4

Quotient = 6

Remainder = 1

6. Write a C Program to find the size of int, float, double and char.

#include<stdio.h>

#include<conio.h>

void main()

int intType;

float floatType;

double doubletype;

char charType;

printf(“size of int: %d bytes ",sizeof(inttype));

printf("Size of float:% d bytes ",sizeof(float type));

Feed back: arunpodduturi@gmail.com Page 4


P Arun Reddy –Programming with C&C++

printf("Size of double:%d bytes",sizeof(double 1ype);

printf"Size of char: %d bytes ",sizeof(char Type);

getch();

Output

Size of int: 4 bytes

Size of float: 4 bytes

Size of double: 8 bytes

Size of char: 1 byte

7. Write a C Program to swap two numbers using temporary variable.

#include<stdio.h>

#include<conio.h>

void main()

int first, second, temp;

printf("Enter first number: ");

scanf(“%d", &first);

printf( "Enter second number: ");

scanf("% d", &second);

temp= first;

first= second;

second= temp;

printf("\n After swapping, first Number = %d\n", first);

printf("After swapping, second Number= %d", second);

Feed back: arunpodduturi@gmail.com Page 5


P Arun Reddy –Programming with C&C++

getch();

Output

Enter first number: 5

Enter second number: 8After swapping, firstNumber = 8

After swapping, secondNumber = 5

8. Write a C Program to check whether a number is even or odd.

#include<stdio.h>

#include<conio.h>

void main()

int num;

printf(“Enter an integer: ");

scanf(“%d", &num);

if(num%2==0)

printf(“%d is even ", num);

else

printf(“%d is odd ",num);

getch();

return;

Output

Enter an integer: 25

25 is odd.

Feed back: arunpodduturi@gmail.com Page 6


P Arun Reddy –Programming with C&C++

9. Write a C Program to check odd or even using the ternary operator.

#include<stdio.h>

#include<conio.h>

int main()

intnum;

printf("Enter an integer: ");

scanf("d", &num);

(num%2==0)? printf("%d is even. ", num):printf("%d is odd. ",num);

getch();

return 0;

Output

Enter an integer: 20

20 is odd

10. Write a C Program to check whether a character is a vowel or


consonant.

#include<stdio.h>

#include<conio.h>

void main()

char c;

int lowercase, uppercase;

printf( "Enter an alphabet: ");

scanf(“%c",&c);

Feed back: arunpodduturi@gmail.com Page 7


P Arun Reddy –Programming with C&C++

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

uppercase=(c==’A’||c =='E’|| c ==’I’||c==’O’||c==’U’);

if(lowercase||uppercase)

printf(“%c is a vowel",c);

else

printf(“%c is a consonant",c);

getch();

Output:

Enter an alphabet.: G

G is a consonant.

11. Write a C Program to find the largest number among three


numbers.

#include<stdio.h>

#include<conio.h>

int main()

int n1, n2, n3;

printf( “Enter three numbers: ");

scanf("%d %d %d", &n1, &n2, &n3);

if (n1>= n2)

if (n1>= n3)

printf("%d is the largest number. ", n1);

Feed back: arunpodduturi@gmail.com Page 8


P Arun Reddy –Programming with C&C++

else

Printf("%d is the largest number: ", n3);

else

if (n2 >= n3)

printf("%d is the largest number. ", n2);

else

Printf("%d is the largest number ", n3);

getch();

Output

Enter three numbers: 4, 7, 3

7 is the largest number

Feed back: arunpodduturi@gmail.com Page 9


P Arun Reddy –Programming with C&C++

12. Write a C Program to check leap year

#include<stdio.h>

#include<conio.h>

void main()

int year;

printf("Enter a year: ");

scanf("%d", &year);

if(year%4==0)

printf("%d is a leap year", year);

else

printf("%d is not a leap year", year)

getch();

return 0;

Output :

Enter a year: 1900

1900 is not a leap year

13. Write a C Program to check whether a character is an alphabet or


not.

#include<stdio.h>

#include<conio.h>

int main()

char c;

Feed back: arunpodduturi@gmail.com Page 10


P Arun Reddy –Programming with C&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);

getch();

return 0;

Output :

Enter a character: *

*is not an alphabet

14. Write a C Program to calculate the sum of first 'n’ natural numbers.

#include<stdio.h>

#include<conio.h>

int main ()

int n, i, sum =0;

printf(“enter a positive integer: ");

scanf(“%d", &n);

i =1;

while(i<= n)

sum+= i;

Feed back: arunpodduturi@gmail.com Page 11


P Arun Reddy –Programming with C&C++

++i;

printf( "Sum = %d", sum);

getch();

return0;

Output:

Enter a positive integer: 10

Sum =55

15. Write a C Program to find factorial of a number.

#include <stdio.h>

#include<conio.h>

int main()

int n, i;

double fact =1;

printf("Enter an integer: ");

scanf("%lf", &n);

if(n <0)

printf(“Error! Factorial of a negative number doesn't exist. ");

else

Feed back: arunpodduturi@gmail.com Page 12


P Arun Reddy –Programming with C&C++

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

fact*= i;

printf("Factorial of %d=%lf", n, fact);

getch();

return 0;

Output:

Enter an integer: 10

Factorial of 10- 362880016

16. Write a C Program to generate multiplication table of a given


number

#include<stdio.h>

#include<conio.h>

int main()

int n, i;

printf(“ Enter an integer: ");

scanf("%d", &n);

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

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

getch();

Feed back: arunpodduturi@gmail.com Page 13


P Arun Reddy –Programming with C&C++

return 0;

Output:

Enter an integer: 9

9*1 = 9

9 *2 = 18

9*3= 27

9*4=36

9 * 5 = 45

9* 6 = 54

9*7= 63

9 *8 =72

9*9 = 81

9 * 10 = 90

17. Write a C Program to display Fibonacci sequence up to 'n’


numbers.

#include<stdio.h>

#include<conio.h>

void main()

int i, n, t1 =0, t2 =1, nextnum;

printf("enter the number of terms: ");

scanf"%d", &n);

printf("fibonacci series: ");

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

Feed back: arunpodduturi@gmail.com Page 14


P Arun Reddy –Programming with C&C++

printf(“%d”, t1);

nextnum=t 1+t2;

t1=t2;

t2=nextnum;

getch();

output

enter the umber of terms: 10

fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

18. Write a C Program to count number of digits in an integer.

#include<stdio.h>

#include<conio.h>

void main()

int n;

int count =0;

printf( "Enter an integer: ");

scanf(“%d”, &n);

while (n!=0)
{

n=n/10;

++count;

Feed back: arunpodduturi@gmail.com Page 15


P Arun Reddy –Programming with C&C++

Printf( "Number of digits: %d”, count);

Output

Enter an integer: 3452

Number of digits: 4

19. Write a C Program to reverse a number.

#include<stdio.h>

#include<conio.h>

int main()

int n, rev =0, remainder;

printf("Enter an integer: ");

scanf("%d",&n);

while(n !=0)

remainder= n %0;

rev= rev*10+ remainder;

n/=10;

printf(” Reversed number %d", rev);

getch();

return 0;

Output:

Feed back: arunpodduturi@gmail.com Page 16


P Arun Reddy –Programming with C&C++

Enter an integer: 2345

Reversed number = 5432

20. Write a C Program to check whether' a number is palindrome or


not.

#include<stdio.h>

#include<conio.h>

int main()

int n, temp =0, remainder, num;

printf("Enter an integer: ");

scanf("%d", &n);

num= n;

while (n !=0)

remainder= n %10;

temp= temp *10+ remainder;

n/=10;

if(num==temp)

printf("d is a palindrome. ", num);

else

printf("%d is not a palindrome", num);

getch();

return 0;

Output

Feed back: arunpodduturi@gmail.com Page 17


P Arun Reddy –Programming with C&C++

Enter an integer: 343

343 is a palindrome.

21. Write a C Program to check whether a number is prime or not.

#include<stdio.h>

#include<conio.h>

void main()

int n, i, j, flag =0;

printf(“enter a positive integer: ");

scanf("%d", &n);

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

if(n% i== 0)

flag=1;

break;

if(n==1)

Printf("1 is neither prime nor composite. ");

Else

if(flag==0)

Feed back: arunpodduturi@gmail.com Page 18


P Arun Reddy –Programming with C&C++

printf("%d is a prime number", n);

else

printf("%d is not a prime number” , n);

getch();

Output

Enter a positive integer: 29

29 is a prime number.

22. Write a C Program to check whether the given number is an


Armstrong number

#include<stdio.h>

#include<conio.h>

void main()

int num, originalNum, remainder, result =0;

printf(" Enter a three-digit integer: ");

scanf("%d", &num);

originalNum=num;

while(originalNum!=0)

remainder=originalNum%10;

result+= remainder*remainder * remainder;

originalNum/=10;

Feed back: arunpodduturi@gmail.com Page 19


P Arun Reddy –Programming with C&C++

if(result==num)

printf("%d is an Armstrong number. ", num);

else

printf("%d is not an Armstrong number ",num);

getch();

Output

Enter a three-digit integer: 371

371 is an Armstrong number

23. Write a C Program to make a simple ealeulator using switch


case.

#include<stdio.h>

#include<conio.h>

int main()

int num1, num2;

float result;

char ch;

printf("Enter first number:”);

scanf("%d, &num1);

printf("Enter second number:” );

scanf("%d", &num2);

printf("Choose operation to perform (+,-,*,%): ");

scanf("%c", &ch);

Feed back: arunpodduturi@gmail.com Page 20


P Arun Reddy –Programming with C&C++

result=0;

switch(ch)

case’+’:

result=num1 +num2;

break;

case’-':

result=num1-num2;

break;

case’*’:

result=num1 *num2;

break;

case'/':

result=(float)num1/(float)num2;

break:

case ‘%’:

result=num1%num2;

break;

default:

printf("Invalid operation\n”);

printf("Result: %d %c %d =f\n ",numl, ch, num2, result);

getch();

return 0;

Output

Feed back: arunpodduturi@gmail.com Page 21


P Arun Reddy –Programming with C&C++

Enter first number: 10

Enter second number: 20

Choose operation to perform (+,-,*,%): +

Result: 10 + 20 = 30.000000

24. Write a C Programming Code to create pyramid and pattern.

#include<stdio.h>

#include<conio.h>

int main()

int i, j, rows;

printf( "Enter number of rows: ");

scanf("%d, &rows);

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

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

printf("* ");

printf("\n");

return 0;

Output

12

Feed back: arunpodduturi@gmail.com Page 22


P Arun Reddy –Programming with C&C++

123

1234

12345

25. Write a C program to reverse a sentence using recursion.

#include<stdio.h>

#include<conio.h>

void reverseSentence():

int main()

Prinf(“Enter a sentence: ):

reverseSentence();

getch();

return 0;

void reverseSentence()

char c;

scanf("%c",&c);

if(c !=’\n’)

reverseSentence();

print(“%c", c);

getch();

Feed back: arunpodduturi@gmail.com Page 23


P Arun Reddy –Programming with C&C++

Output

Enter a sentence: arun reddy

reddy arun

26. Write a C Program to display prime numbers between intervals


using function.

#include<stdio.h>

#include<conio.h>

int PrimeNumber(int n);

int main()

int n1, n2, i, flag;

printf("Enter two positive integers: “);

scanf("% d %d", &n1, &n2);

printf(" Prime numbers between %d and %d are: ",nl, n2);

for(i = n1 +1; i<n2;++i)

flag=Prime Number(i);

if(flag==l)

printf(“%d",i );

getch();

return 0;

Feed back: arunpodduturi@gmail.com Page 24


P Arun Reddy –Programming with C&C++

int PrimeNumber(int n)

int j, flag =1;

for(j=2; j <= n /2;++j)

if(n % j ==0)

flag=0;

break;

return flag;

Output

Enter two positive integers: 12

30

Prime numbers between 12 and 30 are: 13 17 19 23 29

27. Write a C Program to, convert binary number to decimal and vice
versa.

#include<math.h>

#include<stdio.h>

#include<conio.h>

int convert(longlong n);

int main()

Feed back: arunpodduturi@gmail.com Page 25


P Arun Reddy –Programming with C&C++

longlong n;

printf("Enter a binary number: ");

scanf("%lld", &n);

printf("%lld in binary = %d in decimal", n, convert(n));

return 0;

int convert (longlong n)

int dec=0, i =0, rem;

while(n !=0){

rem= n %10;

n=n/10;

dec+= rem*pow(2, i);

++i;

return dec;

Output

Enter a binary number: 110110111

11011011l in binary=430

28. Write a C Program to check prime or Armstrong number using


user-defined function.

#include <math.h>

#include<stdio.h>

#include<conio.h>

Feed back: arunpodduturi@gmail.com Page 26


P Arun Reddy –Programming with C&C++

int PrimeNumber(int n);

int ArmstrongNumber(int n);

int main()

int n, flag:

printf(“ Enter a positive integer: ");

scanf("%d", &n);

flag=PrimeNumber(n)

if(flag ==1)

printf("%d is a prime number: \n",n);

else

printf(“%d is not a prime number \n",n);

flag=ArmstrongNumber(n);

if(flag ==1)

printf("a is an Armstrong number ", n);

else

printf("a is not an Armstrong number.", n);

return 0;

int PrimeNumber(int n)

int i, flag =1;

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

if(n%i==0)

Feed back: arunpodduturi@gmail.com Page 27


P Arun Reddy –Programming with C&C++

flag=0;

break;

return flag;

int ArmstrongNumber(int num)

int original, rem, result=0, n=0, flag;

original=num;

while(original !=0)

original/=10;

++n;

Original=num;

while(original! =0)

rem= original %10;

result+=pow(rem, n);

original/=10;

if(result==num)

flag=1;

else

Feed back: arunpodduturi@gmail.com Page 28


P Arun Reddy –Programming with C&C++

flag=0;

getch();

return flag;

Output

Enter a positive integer: 371

371 is not a prime number.

371 is an Armstrong number

29. Write a C program to calculate the power using recursion

#include<stdio.h>

#include<conio.h>

int power(int n1,int n2);

void main()

int base, a, result;

printf("enter base number: ");

scanf("%d", &base);

printf("Enter power number(positive integer):” );

scanf(“%d”,&a);

result= power(base, a);

printf("%d^%d =%d", base, a, result);

getch();

int power(int base, int a)

Feed back: arunpodduturi@gmail.com Page 29


P Arun Reddy –Programming with C&C++

if(a!=0)

return(base* power (base, a -1));

else

return 1;

Output:

Enter base number: 3

Enter power number(positive integer): 4

3^4 =81

30. Write a C Program to find GCD using recursion.

#include<stdio.h>

#include<conio.h>

int hcf(int n1,int n2);

int main()

int n1, n2;

printf(“ Enter two positive integers: ");

scanf("%d %d", &n1, &n2);

printf("GC.D of %d and %d is %d", n1, n2,hcf(n1, n2));

getch();

return 0;

int hcf(int n1,int n2)

if(n2!=0)

Feed back: arunpodduturi@gmail.com Page 30


P Arun Reddy –Programming with C&C++

return hcf(n2, n1 % n2);

else

return n1;

Output

Enter two positive integers: 366

60

GC.D of 366 and 60 is 6.

31. Write a C Program to calculate average using arrays.

#include<stdio.h>

#include<conio.h>

int main()

int n, i;

float num[100], sum =0.0,avg;

printf("enter the numbers of elements: ");

scanf("%d", &n);

while(n >100|| n <1)

printf("error! number should in range of (1 to 100).\ n”);

printf("enter the number again: ");

scanf(“ %d", &n);

Feed back: arunpodduturi@gmail.com Page 31


P Arun Reddy –Programming with C&C++

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

printf(“%d enter number: ",i+1);

scanf(“%d”,&num[i]);

sum+num[i];

avg= sum/n;

printf(“average = %d",avg);

getch();

return 0;

Output

Enter the numbers of elements: 6

1. Enter number: 2

2. Enter number: 3

3. Enter number: 4

4. Enter number: 5

5. Enter number: 6

6. Enter number: 10

Average = 5

32. Write a C Program to find largest element in an array.

#include<stdio.h>

#include<conio.h>

int main()

Feed back: arunpodduturi@gmail.com Page 32


P Arun Reddy –Programming with C&C++

int i, n;

int arr[100];

printf("Enter the number of elements (1 to 100): ");

scanf("%d", &n);

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

printf(“ Enter number %d: ", i +1);

scanf(“%d”,&arr[i]);

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

if(arr[0]<arr[i])

arr[0]=arr[i];

printf(“largest element=%d", arr[0]);

getch();

return 0;

Output

Enter the number of elements (1 to 100): 3

Enter number1: 3

Enter number2: 56

Feed back: arunpodduturi@gmail.com Page 33


P Arun Reddy –Programming with C&C++

Enter number3: 43

Largest element = 56

33. Write a C Program to add two matrices using multi-


dimensional arrays.

#include<stdio.h>

#include<conio.h>

int main()

int r, c, a[100][100], b[100][100], sum[100] [100], i, j;

printf("enter the number of rows (between 1 and 100): “);

scanf("d", &c);

printf( "\n enter the number of columns (between 1 and 100):” );

scanf("%d”, &c);

printf("\n enter elements of 1st matrix: in );

for(i =0; i<r:++i)

for(j =0; j < c;++j)

printf( "enter element a %d %d: ", i +1, j +1);

scanf("%d", &a[i][j]);

}}

printf( "enter elements of 2nd matrix: \n ");

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

for(j =0; j < c;++j)

Feed back: arunpodduturi@gmail.com Page 34


P Arun Reddy –Programming with C&C++

printf("enter element a%d%d: ", i +1, j +1);

scanf("%d", &b[i] [j]);

}}

for(i =0; i<r;++t)

for(j =0;j< c;++t)

sum[i] [j]=a[i][j]+ b[i][j];

printf("\n sum of two matrices: \n");

for(i =0; i<r;++t)

for(j =0;j< c;++t)

printf("%d\t”, sum[i][j]);

printf(“\n”);

getch();

return 0;

Output

Enter the number of rows (between l and 100): 2

Feed back: arunpodduturi@gmail.com Page 35


P Arun Reddy –Programming with C&C++

Enter the number of columns (between I and 100): 3

Enter elements of 1st matrix:

Enter element a11: 2

Enter element a12: 3

Enter element a13: 4

Enter element a21: 5

Enter element a22: 2

Enter element a23: 3

Enter elements of 2nd matrix:

Enter element a11: -4

Enter element a12: 5

Enter element a13: 3

Enter element a21: 5

Enter element a22: 6

Enter element a23: 3

Sum of two matrices:

-2 8 7

10 8 6

34. Write a C Program to find the length of a string.

#include<stdio.h>

#include<conio.h>

int main()

Char s[]="badruka college";

Feed back: arunpodduturi@gmail.com Page 36


P Arun Reddy –Programming with C&C++

int i;

for(i =0; s[i]!=’\0’; ++i);

printf("Length of the string: %d", );

getch();

return 0;

Output

Length of the string: 15

35. Write a C Program to concatenate two strings.

#include<stdio.h>

#include<conio.h>

int main(){

char s1[100]="hello ”,s2[]=”badruka”;

int i, j;

for(i =0; s1[i]!=’\0',++i)

Printf("i = %d\n",i );

For(j =0; s2[j]!=’\0',++j, ++i)

s1[i]= s2[j];

s1[i]=’\0’;

printf("After concatenation: ");

Feed back: arunpodduturi@gmail.com Page 37


P Arun Reddy –Programming with C&C++

puts(s1);

getch();

return 0;

Output

After concatenation: hello badruka

36. Write a C Program to copy string without using strcpy().

#include<stdio.h>

#include<conio.h>

int main()

Char s1[100), s2[100], i;

printf("enter string s1: ");

gets(s1);

for(i=0; s1[i]!=’\0’;++i)

s2[i]=s1[i];

s2[i]=’\0’;

printf(“sting s2:%s”,s2);

getch();

return 0;

Output

Feed back: arunpodduturi@gmail.com Page 38


P Arun Reddy –Programming with C&C++

Enter string s1: arun reddy

String s2: arun reddy

37. Write a C Program to count the number of vowels, consonants


and so on.

#include<stdio.h>

#include<conio.h>

int main()

char line[150];

int vowels, consonant, digit, space;

vowels=constant=digit= space =0;

printf("Enter a line of string: ");

fgets(line, sizeof(line), stdin);

for(int 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[iJ==’E’|| line[i]==’T’|| line[i]== '0'|| line[i]==’U’)

++vowels;

Else if(line[i]>= 'a'&& line[i]<='z’)||(line[i]>= 'A'&& line[i] <=Z))

++consonant;

Else if(line[i]>="0'&& line[i]<=’9’)

Feed back: arunpodduturi@gmail.com Page 39


P Arun Reddy –Programming with C&C++

++digit;

Else if(line[i]==’O’&& line[i]<’9’)

++space;

printf(“Vowels:%d", vowels);

printf(“\n Consonants %d” , consonant);

printf(“\nDigits: %d”, digit);

printf("\nWhitespaces:%d", space);

getch();

return 0;

Output

Enter a line of string: adfslkj34 34Ikj343 341k

Vowels: 1

Consonants: 11

Digits: 9

Whitespaces: 2

38. Write a C Program to find the frequeney of characters in a


string.

#include<stdio.h>

#include<conio.h>

int main()

Feed back: arunpodduturi@gmail.com Page 40


P Arun Reddy –Programming with C&C++

char str[1000],ch;

int count =0;

printf("Enter a string: ");

fgets(str,sizeof(str), stdin);

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

scanf("%c", &ch);

for(int i=0;str[i]!=’\O’;++i)

if(ch==str[i)

++count;

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

return 0;

Output

Enter a string: This book is awesome.

Enter a character to find its frequency: o

Frequency of o = 3

39. Write a C Program to access array elements using pointer

#include<stdio.h>

#include<conio.h>

int main()

Feed back: arunpodduturi@gmail.com Page 41


P Arun Reddy –Programming with C&C++

int arr[5];

printf("enter elements: ");

for(int i=0; i<5; ++i)

scanf(“%d”,&arr+ i);

printf(“You entered: \n ");

for(int i =0; i<5, ++i)

printf("%d\n",*(arr+ i));

getch();

return 0;

Output

Enter elements: 1 2 3 4 5 6

You entered:1 2 3 4 5 6

40. Write a C program to create, initialize, assign and access a


pointer variable.

#incude<stdio.h>

#include<conio.h>

int main()

int num;

int *pnum;

pnum=&num;

Feed back: arunpodduturi@gmail.com Page 42


P Arun Reddy –Programming with C&C++

num=100;

printf("using variable num: \n ");

printf("value of num:% d\n address of num: %u\n ", num, &num);

printf("using pointer variable:\n ");

printf("value of num: %d \naddress of num: %u”,*pnum,pnum");

getch();

return 0;

Output

Using variable num:

value of num: 100

address of num: 2764564284

Using pointer variable:

value of num: 100

address of num: 2764564284

41. Write a C program to swap two numbers using pointers.

#include<stdio.h>

#include<conio.h>

void swap(int *a, int *b);

int main()

int num1, num2;

printf"Enter value of num1: ");

scanf("%d",&num1);

printf( “Enter value of num2: ");

Feed back: arunpodduturi@gmail.com Page 43


P Arun Reddy –Programming with C&C++

scanf("%d", &num2);

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

swap(&numl, &num2);

printf("After Swapping: num1=%d,num2=%d ",num1,num2);

return 0;

void swap(int "a, int *b)

int t

t=*a;

*a=*b;

Output:

Enter value of num1: 10

Enter value of num2: 20

Before Swapping: num1=10, num2=20

After Swapping: num1=20, num2=10

42. Write a C program to count vowels and consonants in a string


using pointer.

#inciude <stdio.h>

#include<conio.h>

int main()

char str[100];

char *ptr;

Feed back: arunpodduturi@gmail.com Page 44


P Arun Reddy –Programming with C&C++

int V,C;

printf( "Enter a string: ");

gets(str);

ptr=str;

V=C=0;

while("ptr!="\0)

If(*ptr=’A’||"ptr==’E’||ptr==’T’||ptr==’O’||ptr==’U’||ptr=’a'|lptr==’e'||ptr=’I’||

ptr==’o’||ptr==’u’)

V++;

else

C++;

ptr++;

Printf("Total number of VOWELS: %d,CONSONANT: %d\n ",V,C);

return0;

Output:

Enter a string: This is a test string

Total number of VOWELS:5 , CONSONANT: 16

43. Write a C Program to store information of u student using structure.

#include<stdio.h>

#include<conio.h>

struct student

Feed back: arunpodduturi@gmail.com Page 45


P Arun Reddy –Programming with C&C++

char name[50];

int roll;

int marks;

}s;

int main()

printf("Enter information: \n ");

printf(“ Enter name: "):

gets(s.name, sizeof(s.name, stdin);

printf Enter roll number: ");

scanf(“%d”, &s.roll);

printf("Enter marks: ");

scanf("%d", &s.marks);

printf("Displaying Information: \n ");

printf("Name: ");

printf(“%s", s.name);

printf("Roll number: %d\n",s. roll);

printf("Marks: %d\n",s.marks);

getch();

return 0;

Output

Enter information.

Enter name: arun

Enter roll number: 1234

Feed back: arunpodduturi@gmail.com Page 46


P Arun Reddy –Programming with C&C++

Enter marks: 95

Displaying Information:

Name: arun

Roll number: 1234

Marks: 95

44. Write a C Program 10 add two distances (in inch-feet system)


using structures.

#include <stdio.h>

#include<conio.h>

struct distance

int feet;

float inch;

}d1, d2, sumofdistances;

int main()

printf ("enter information for 1st distance \n ");

printf("enter feet:” );

scanf("%d", &dl.feet);

printf( "enter inch: ");

scanf (" %d”,&d1.inch);

printf("\n enter information for 2nd disiance \n” );

printf(“enter feet:” );

scanf(“%d", &d2.feet);

pring(“enter inch: ");

Feed back: arunpodduturi@gmail.com Page 47


P Arun Reddy –Programming with C&C++

scanf(“%d”,&d2.inch);

sumofdistances.feet=d1.feet+d2.feet;

sumofdistances.inch=d1.inch +d2.inch;

if(sumofdistances.inch>12.0)

sumofdistances.inch= sumofdistances.inch-12.0;

++sumofdistances.feet;

printf(“\n sum of distances = %d\%lf\",sumofdistances.feet,sumofdistances.inch);

return0;

Output

Enter information for 1st distance

Enter feet: 23

Enter inch: 8.6

Enter information for 2nd distance

Enter feet: 34

Enter inch: 2.4

Sum of distances = 57-11.0"

45. Write a C Program to store information of students using


structure.

#include<stdio.h>

#include<conio.h>

struct student

Feed back: arunpodduturi@gmail.com Page 48


P Arun Reddy –Programming with C&C++

char firstName[50];

int roll;

float marks;

}s[10];

int main()

int i,

printf("enter information of students:\n ");

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

s[i].roll= i+1;

printf("\n for roll mumber %d,\n", s[i]..roll);

printf(“enter first name:”);

scanf(“%s", s[i].firstname);

printf("enter marks: ");

scanf("%f, &s[i].marks);

printf"Displaying Information: \n \n ");

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

printf("\n Roll number: %d\n", i +1);

printf("first name: ");

puts(s[i].firstName):

printf("Marks: %lf”, s[i].marks);

printf("\n ");

Feed back: arunpodduturi@gmail.com Page 49


P Arun Reddy –Programming with C&C++

getch();

return 0;

Output

Enter information of students:

For roll number1,

Enter name: Daniel

Enter marks: 98

For roll number2,

Enter name: Karthik

Enter marks: 89

Displaying Information:

Roll number: 1

Name: Daniel

Marks: 98

roll number:2

name: Karthik

marks: 89

46. write a C program to declare and initialize an Union

#include<stdio.h>

#include<conio.h>

union pack
{

Feed back: arunpodduturi@gmail.com Page 50


P Arun Reddy –Programming with C&C++

char a;

int b;

double c;

int main()

pack p;

printf(“\nOccupied size by union pack: ad",sizeof (pack));

p.a=’A’;

printf("n Value of a:%c ",p.a);

p.b=10;

printf("nValue of b:%d",p.b);

p.c=12345.6790;

printf("\n Value of c:%f",p.c);

p.a=’A';

p.b=10;

p.c=12345.6790;

printf("\n Value of a:hc, b:%d, c:4",p.a.p.b.p.c);

getch();

return 0;

Output

Occupied size by union pack: 8

Value of a:A

Value of b:10

Feed back: arunpodduturi@gmail.com Page 51


P Arun Reddy –Programming with C&C++

Value of c:12345.679000

Value of a: b:-377957122, c:12345.679000

47. Write a C++ program to implement function overloading.

#include<iostream.h>

int absolute(int);

float absolute (float);

int main()

int a=-5;

float b=5.5;

cout<<"Absolute value of "<< a <<" = "<< absolute(a) <<endl;

cout<<"Absolute value of "<< b <<" = "<< absolute (b);

return 0;

int absolute (int var)

if (var<0)

var= -var;

return var;

float absolute (float var)

if(var<0.0)

var=-var;

getch();

Feed back: arunpodduturi@gmail.com Page 52


P Arun Reddy –Programming with C&C++

return var;

Output

Absolute value of -5 = 5

Absolute value of 5.5 = 5.5

48. Write a C++ progranm to calculate an area of rectangle using


encapsulation.

#include<iostream.h>

Class Rectangle

int length;

int breadth;

public:

void setDimension(int l, int b)

Length=l ;

Breadth=b;

Int getArea()

return length* breadth;

int main()

Int x,y;

Feed back: arunpodduturi@gmail.com Page 53


P Arun Reddy –Programming with C&C++

Rectangle rt;

Cout<<"Enter the length of the rectangle:”;

cin>>x;

cout<<"Enter the breadth of the rectangle:”

cin>>y;

rt.setDimension(7, 4);

cout<<rt.getArea()<<endl;

return 0;

Output

Enter the length of the rectangle: 30

Enter the breadth of the rectangle:20

600

49. Write a C++ program to add. two numbers using data


abstraction.

#include<iostream.h>

Class Sum

private:

int x, y z;

public:

void add()

cout<<"Enter two numbers: ";

cin>>x>>y;

z=x+y;

Feed back: arunpodduturi@gmail.com Page 54


P Arun Reddy –Programming with C&C++

cout<<"Sum of two number is: "<<z<<endl;

}}

int main()

Sum sm;

sm.add();

return 0;
}

Output:

Enter two numbers:

Sum of two number is: 9

50. Write a C++ program to overload binary operators.

#include<iostream.h>

Class A

int x:

public:

A()

{}

A(int i)

x=i;

void operator+(A);

Feed back: arunpodduturi@gmail.com Page 55


P Arun Reddy –Programming with C&C++

void display();

};

void A ::operator+(A a)

int m=x+a.x;

cout<<"The result of the addition of two objects is :”<<m;

int main()

Int x,y;

cout<<"Enter the First number: "

cin>>x;

cout<<"Enter the Second number: ";

cin>>y;

A al(5);

A a2(4);

A1+a2;

return 0;

Output:

Enter First Number: 3

Enter Second Number: 6

The result of the addition of two numbers is:9

Feed back: arunpodduturi@gmail.com Page 56

You might also like