C Programs

1. WAP to find the area of the rectangle.

Program:
#include<stdio.h>
int main()
{
        float width, length, Area;
        printf("\nPlease Enter the Width of a Rectangle: ");
        scanf("%f", &width);      
        printf("\nPlease Enter the Length of a Rectangle: ");
        scanf("%f", &length);

        Area= length * width;
 
        printf("\n The Area of a Rectangle=%.2f", Area);
return 0;
}
Output:


2. WAP to find the reverse of a 4 digit number. 

Program:
#include<stdio.h>
int main()
{
int a, rev=0, rem;
printf("Enter the 4 digit number: ");
scanf("%d", &a);
while(a!=0)
{
rem = a%10;
rev = rev*10+rem;
a = a/10;
}
printf("Reverse of the number is: %d",rev);
return 0;
}
Output:



3. WAP to swap two variables with and without using 3rd variable. 
 Without using 3rd variable:
#include<stdio.h>
int main( )
{
int a=10, b=20;
printf("Before swap a=%d b=%d", a, b);
a=a+b; //a=30 (10+20)
b=a-b; //b=10 (30-20)
a=a-b; //a=20 (30-10)
printf("\nAfter swap a=%d b=%d", a, b);
return 0;
}

Output:


With Using 3rd Variable:
#include<stdio.h>
int main( )
{
int a, b, temp;
printf("Numbers before swapping: ");
scanf("%d %d", &a, &b);
temp=a;
a=b;
b=temp;
printf("\nNumbers after swapping: ");
printf("%d %d", a, b);
return 0;
}
Output:

4. WAP to find the sum of the digits of a 4 digit number. 

Program:
#include<stdio.h>
int main( )
{
int a, sum=0, rem;
printf("Enter the number: ");
scanf("%d", &a);
while(a!=0)
{
rem=a%10;
sum=sum+rem;
a=a/10;
}
printf("The sum of the number is: %d", sum);
return 0;
}

Output:

5. WAP to find whether the year is leap or not (Do this with conditional operators).
Program:
#include<stdio.h>
int main( )
{
int year;
printf("Enter the year: ");
scanf("%d", &year);
  
(year%4==0 && year%100!=0)?printf("%d is a Leap Year.", year):
(year%400==0)?printf("%d is a Leap Year.", year):
printf("%d is not a Leap Year.", year);
return 0;
}
Output:



6. If the age of A,B and C are input through the keyboard, write a program to determine the youngest of the three. 
Program:
#include<stdio.h>
int main( )
{
int A,B,C;
printf("Enter the age ofg A: ");
scanf("%d", &A);
printf("Enter the age of B: ");
scanf("%d", &B);
printf("Enter the age of C: ");
scanf("%d", &C);

if(A<B && A<C)
printf("Youngest is A");
if(B<A && B<C)
printf("Youngest is B");
if(C<A && C<B)
printf("Youngest is C");
return 0;
}

Output:

7. WAP that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For number which multiples of both three and five print "FizzBuzz". 

Program:
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=100; i++)
{
if((i%15)==0)
printf("FizzBuzz\t ");
else if((i%3)==0)
printf("Fizz\t ");
else if((i%5)==0)
printf("Buzz\t ");
else
printf("%d\t ", i);
}
return 0;
}

 Output:
Fizzbuzz program

8. WAP to find whether the entered number is Armstrong or not. 

Armstrong Number = sum of cubes of its digit

For example:
  •     153= (1)³ + (5)³ + (3)³
  •     370= (3)³ + (7)³ + (0)³
   
Program:
#include<stdio.h>
int main()
{
int num, temp, sum=0, r;
printf("Enter the number: ");
scanf("%d", &num);
temp=num;
while(num>0)
{
r=num%10;
sum=sum+(r*r*r);
num=num/10;
}
if(temp==sum)
printf("It is an Armstrong Number");
else 
printf("It is not an Armstrong Number");
return 0;
}

 Output:
Armstrong Number in C


9. WAP to find whether the entered number is prime or not. 

Prime Numbers are numbers which are only divisible by themselves or by 1.

For example:
           2,3,5,7,11

Program: 
#include<stdio.h>
int main()
{
int n, i;
printf("Enter number: ");
scanf("%d", &n);
for (i=2; i<=n/2; i++)
{
if(n%i==0)
{
break;
}
}
if(i>n/2)
printf("%d is a prime number", n);
else 
printf("%d is not a prime number", n);
return 0;
}

Output:
prime number program in c

10. WAP to find the factorial value of any number. 

Program:

#include<stdio.h>
int main()
{
void factorial(int);
int a;
printf("Enter the number: ");
scanf("%d", &a);
factorial(a);
}
void factorial(int a)
{
int b, c=1;
for(b=1; b<=a; b++)
{
c=c*b;
}
printf("Factorial of the number is %d", c);
}
Output: 
C Program to Find Factorial of a Number

11. Two numbers are entered through the keyboard. WAP to find the value of one number raised to the power of another. 
Program:
#include<stdio.h>
#include<math.h>
int main()
{
int a, b, c;
printf("Enter the power of integer: ");
scanf("%d", &b);
printf("Enter the integer: ");
scanf("%d", &a);
c=pow(a,b);
printf("Result: ", c);
printf("%d^%d=%d", a, b, c);
return 0;
}

Output:
Value of one number raised to the power of another

12. WAP to print all the Armstrong number between 1 to 500. 
Program:
#include<stdio.h>
int main()
{
int i, num, sum, digit;
for(i=0; i<500; i++)
{
num=i;
sum=0;
while(num>0)
{
digit=num%10;
sum+=digit*digit*digit;
num/=10;
}
if(sum==i)
{
printf("%d\t", i);
}
}
return 0;
}

Output:
armstrong number between 1 to 500 in c

13. WAP to generate all combinations of 1,2 and 3. 
Program:
#include<stdio.h>
int main()
{
    int i, j, k;
    for(i=1; i<=3; i++)
{
    for(j=1;j<=3;j++)
    {
        for(k=1;k<=3;k++)
        printf("%d %d %d\t", i, j, k);
    }
}
return 0;
}

Output:

c program to generate all combinations of 1, 2 and 3

14. WAP to print the multiplication table of the number entered by the user. 

Program:

#include<stdio.h>
int main()
{
       int i=1, num;
       printf("Enter number: ");
       scanf("%d", &num);
    
       while(i!=11)
       {
           printf("%d X %d = %d\n", num, i, num*i);
           i++;
       }
}

Output:
multiplication table of any number

15. WAP to enter numbers till the user want. At the end it should display the count of positive, negative and zeros entered. 

Program:

#include <stdio.h>
int main()
{
int a, neg=0, zero=0, pos=0;
char ch;

do{
printf("Enter numbers:");
scanf("%d", &a);

if(a>0)
{
pos++;
}
else if(a<0)
{
neg++;
}
else
{
zero++;
}
printf("Do you want to continue(y/n)? \n");
scanf(" %c", &ch);

} while(ch =='y'|| ch =='Y');

printf("Total positive number:%d\n", pos);
printf("Total zeros:%d\n", zero);
printf("Total negative number:%d", neg);
return 0;
}

Output:

16. WAP to find the range of a set of numbers.
Range is the difference between the smallest and the biggest number in the list.

 Program: 

#include<stdio.h>
int main()
{
int k, a[k], i, j, lg, sm;
printf("How many numbers you want to enter:");
scanf("%d", &k);
printf("Enter the values to find the range:");
for(j=0; j<k; j++)
{
scanf("%d", &a[j]);
}
lg= a[0];
sm= a[0];
for(i=0; i<k; i++)
{
if(a[i]>lg)
lg=a[i];
else if(a[i]<sm)
sm= a[i];
else
continue;
}
printf("Range between smallest & largest number is:%d", lg-sm);
return 0;
}

Output:


17. WAP to fill the entire screen with a smiling face. The smiling face has an ASCII value 1. 

 Program:

#include<stdio.h>
int main()
{
	int i, j;
	for(i=1, j=1; j<=8000; j++)
	{
		printf("%c", i);
	}
}

Output:


18. WAP to print all the prime number from 1-300. 

 Program:

#include<stdio.h>
int main()
{
	int num, max=300, i, flag;
	for(num=1; num<=max; num++)
	{
		flag=0;
		for(i=2;i<=num/2;i++)
		{
			if(num%i==0)
			{
				flag=1;
				break;
			}
		}
		if(flag==0 & num!=1)
		printf("%d\t", num);
	}
}

Output:

19. WAP to print all  the ASCII values and their equivalent characters using a while loop. (ASCII value vary 0 to 255) 

Program:

#include<stdio.h>
int main()
{
	int i=0;
	while(i<=255)
	{
		printf("\nASCII value of %c:%d", i, i);
		i++;
	}
	return 0;
}

 Output:



20. Pyramid Programs with stars, numbers and alphabets. 
Pyramid program with stars:
#include<stdio.h>
int main()
{
	int n, m;
	printf("Enter the number of rows=");
	scanf("%d", &n);
	m=n;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=m-1; j++)
		{
			printf(" ");
		}
		for(int k=1; k<=2*i-1; k++)
		{
			printf("*");
		}
		m--;
		printf("\n");
	}
	return 0;
}
Output:


➤Pyramid programs with number:
#include<stdio.h>
int main()
{
	int n, r, c, k, a;
	printf("Enter the number of rows=");
	scanf("%d", &n);
	
	for(r=1; r<=n; r++)
	{
		for(c=1; c<=n-r; c++)
		{
			printf(" ");
		}
		for(k=1; k<=2*r-1; k++)
		{
			if(k<r)
			{
				printf("%d",k);
			} 
			else if(k==r)
			{
			 	printf("%d", k);
				a=k;
			}
			else printf("%d", --a);
		}
		printf("\n");	
	}
	return 0;
}
Output:

Pyramid programs with alphabets:
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int ch=65;
	int i, j, k, m;
	system("cls");
	
	for(i=1; i<=5; i++)
	{
		for(j=5; j>=i; j--)
			printf(" ");
		for(k=1; k<=i; k++)
		
			printf("%c", ch++);
			ch--;
		for(m=1; m<i; m++)
		printf("%c", --ch);
		printf("\n");
		ch=65;	
	}
	return 0;
}
Output:

21. WAP to swap the values of two variables using call by value and call by reference. 
 *Call by Value  
#include<stdio.h>
void swap(int, int);
int main()
{
	int a=10;
	int b=20;
	printf("Before swapping the values in main a=%d, b=%d\n", a, b);
	swap(a,b);
	printf("After swapping values in main a=%d, b=%d\n", a, b);
}
void swap(int a, int b)
{
	int temp;
	temp=a;
	a=b;
	b=temp;
	printf("After swapping values in function a=%d, b=%d\n", a, b);
}
Output:

*Call by reference 
#include<stdio.h>
void swap(int *, int *);
int main()
{
	int a=10;
	int b=20;
	printf("Before swapping the values in main a=%d, b=%d\n", a, b);
	swap(&a,&b);
	printf("After swapping values in main a=%d, b=%d\n", a, b);
}
void swap(int *a, int *b)
{
	int temp;
	temp=*a;
	*a=b;
	*b=temp;
	printf("After swapping values in function a=%d, b=%d\n", *a, *b);
}
Output:

22. WAP to find the largest number in an array. 
 Program: 
#include<stdio.h>
int main()
{
	int size, i, largest;
	printf("Enter the size of the array: ");
	scanf("%d", &size);
	int array[size];
	
	printf("Enter %d elements of an array: ", size);
	
	for(i=0; i<size; i++)
	{
		scanf("%d", &array[i]);
	}
	largest= array[0];
	for(i=1; i<size; i++)
	{
		if(largest<array[i])
		largest=array[i];
	}
	printf("\nLargest element present in the given array is: %d", largest);
	return 0;
}
Output:

23. WAP that interchanges the odd and even elements of an array.
 Program: 
#include<stdio.h>
int main()
{
	int a[]= {1,2,3,4,5,6}, i, temp;
	for(i=0; i<=5; i=i+2)
	{
		temp = a[i];
		a[i] = a[i+1];
		a[i+1] = temp;
	}
	for(i=0; i<=5; i++)
	{
		printf("%d\n", a[i]);
	}
	return 0;
}
Output:

Post a Comment

If you have any doubts, Please let me know

Previous Post Next Post