Program 2 WAP in C To Check Whether A Person Is Eligible To Vote or Not Using Conditional Operator
Program 2 WAP in C To Check Whether A Person Is Eligible To Vote or Not Using Conditional Operator
Code:
#include<stdio.h>
int main()
{
printf("Enter your age to find your eligibility to vote.\n");
int a;
scanf("%d", &a);
a>=18?printf("You are eligible to vote."):printf("You are not eligible to vote
.");
}
Output:
Program 3
WAP in C to find the greatest of the three numbers using conditional operator.
Code:
#include<stdio.h>
int main()
{
printf("Enter three numbers of your choice.\n");
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
int t=(a>b? a:b);
int j=(t>c? t:c);
printf("%d is the biggest number.", j);
}
Output:
Program 1
WAP in C to check whether a person is eligible to vote or not using if-else statement.
Code:
#include<stdio.h>
int main()
{
printf("Enter your age.- ");
int a;
scanf("%d", &a);
if(a>=18)
printf("You are eligible to vote.");
else
printf("You need to grow older to vote.");
}
Output:
Program 1
WAP in C to find the gross salary of an employee based on the given conditions. –
If basic salary <= 20000, HRA = 10% of BS and DA = 15% of BS
If 20000 < basic salary <= 40000, HRA = 15% of BS and DA = 20% of BS
If 40000 < basic salary <= 60000, HRA = 20% of BS and DA=25% of BS
Code:
#include<stdio.h>
int main()
{
printf("Enter your basic salary (in INR).- ");
float bs, hra, da;
scanf("%f", &bs);
if(bs<=20000)
{
hra=0.1*bs;
da=0.15*bs;
}
else if(bs<=40000 && bs>20000)
{
hra=0.15*bs;
da=0.2*bs;
}
else if(bs<=60000 && bs>40000)
{
hra=0.2*bs;
da=0.25*bs;
}
else
{
hra=0.25*bs;
da=0.3*bs;
}
float gs=bs+hra+da;
printf("Your total salary is INR %f.", gs);
}
Output:
Program 2
WAP in C to perform arithmetic operations using switch case.
Program 3
WAP in C to check whether if entered character is vowel or not using switch case.
Code:
#include<stdio.h>
int main()
{
printf("Enter a character.- ");
char c;
scanf("%c", &c);
switch(c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':printf("Entered character is a vowel.");
break;
default:printf("Entered character is not a vowel.");
}
}
Output:
Program 4
WAP in C to find the largest number in three using switch case.
Code:
#include<stdio.h>
int main()
{
printf("Enter three numbers.- ");
int a, b, c, d;
scanf("%d%d%d", &a, &b, &c);
switch(a>b)
{
case 0:
switch(b>c)
{
case 0: printf("c is the largest.");
break;
case 1: printf("b is the largest.");
break;
}
break;
case 1:
switch(a>c)
{
case 0: printf("c is the largest.");
break;
case 1: printf("a is the largest.");
break;
}
break;
}
}
Ouput:
Session 6
Program 1
WAP in C to print the table of 5 from 1 to 10.
Code:
#include<stdio.h>
int main()
{
int a, i;
for(i=1; i<=10; i++)
{
a=5*i;
printf("5 * %d = %d\n", i, a);
}
}
Output:
Program 2
WAP in C to print the sum of first 10 natural numbers.
Code:
#include<stdio.h>
int main()
{
int a=0, i;
for(i=1; i<=10; i++)
{
a+=i;
}
printf("Sum = %d", a);
}
Output:
Program 3
WAP in C to print the following pattern. –
*
**
***
****
*****
Code:
#include<stdio.h>
int main()
{
int a, i;
for(i=1; i<4; i++)
{
for(a=0; a<i; a++)
printf("*");
printf("\n");
}
}
Output:
Program 4
WAP in C to print the following pattern. –
1
12
123
1234
12345
Code:
Program 5
WAP in C to print the following pattern. –
12345
1234
123
12
1
Code:
#include<stdio.h>
int main()
{
int a, i, j;
printf("Enter the number of rows you want to print. -\n");
scanf("%d", &j);
for(i=j; i>0; i--)
{
for(a=1; a<=i; a++)
printf("%d ", a);
printf("\n");
}
}
Output:
Program 6
WAP in C to print the following pattern. –
54321
5432
543
54
5
Code:
#include<stdio.h>
int main()
{
int a, i;
for(i=1; i<=5; i++)
{
for(a=5; a>=i; a--)
printf("%d ", a);
printf("\n");
}
}
Output:
Program 7
WAP in C to print the following pattern. –
A
AB
ABC
ABCD
ABCDE
Code:
#include<stdio.h>
int main()
{
printf("Enter the number of rows.-\n");
int a, i, j;
char c;
scanf("%d", &a);
for(i=1; i<=a; i++)
{
c='A';
for(j=0; j<i; j++, c++)
printf("%c ", c);
printf("\n");
}
}
Output:
Program 8
WAP in C to print the following pattern. –
ABCDE
ABCD
ABC
AB
A
Code:
#include<stdio.h>
int main()
{
printf("Enter the number of rows.-\n");
int a, i, j;
char c;
scanf("%d", &a);
for(i=a; i>=1; i--)
{
c='A';
for(j=0; j<i; j++, c++)
printf("%c ", c);
printf("\n");
}
}
Output:
Program 9
WAP in C to print the following pattern. –
AAAAA
BBBB
CCC
DD
E
Code:
#include<stdio.h>
int main()
{
printf("Enter the number of rows.-\n");
int a, i, j;
char c;
scanf("%d", &a);
c='A';
for(i=a; i>=1; i--)
{
for(j=0; j<i; j++)
printf("%c ", c);
c++;
printf("\n");
}
}
Output:
Program 10
WAP in C to print the reverse of a number.
Code:
#include<stdio.h>
int main()
{
int a, i=0;
printf("Enter an integer. - ");
scanf("%d", &a);
while(a!=0)
{
i=(i*10)+(a%10);
a=a/10;
}
printf("Reverse of the entered integer = %d", i);
}
Output:
Program 11
WAP in C to print the sum of the digits of a number.
Code:
#include<stdio.h>
int main()
{
int a, i=0;
printf("Enter an integer. - ");
scanf("%d", &a);
while(a!=0)
{
i+=(a%10);
a=a/10;
}
printf("Sum of the digits = %d", i);
}
Output:
Program 12
WAP in C to check if the given number is palindrome or not.
Code:
#include<stdio.h>
int main()
{
int a, i=0;
printf("Enter an integer. - ");
scanf("%d", &a);
int b=a;
while(a!=0)
{
i=(i*10)+(a%10);
a=a/10;
}
if(i==b)
printf("Given number is a palindrome.");
else
printf("Given number is not a palindrome.");
}
Output:
Program 13
WAP in C to find if the given number is an Armstrong number or not.
Code:
#include<stdio.h>
#include<math.h>
int main()
{
int a, n=0, b, c, s=0;
printf("Enter a number. - ");
scanf("%d", &a);
b=a;
c=a;
while(a!=0)
{
++n;
a=a/10;
}
while(b!=0)
{
int q=b%10;
s+=pow(q, n);
b=b/10;
}
if(s==c)
printf("Entered number is armstrong.");
else printf("Entered number is not armstrong.");
}
Output:
Session 7
Program 1
WAP in C to find the sum of the elements of the array.
Code:
#include<stdio.h>
int main()
{
int n;
printf("Enter the number of elements. - ");
scanf("%d", &n);
int a[n];
printf("Enter the values.\n");
for(int i=0; i<n; i++)
scanf("%d", &a[i]);
int s=0;
for(int i=0; i<n; i++)
s+=a[i];
printf("Sum of elements of the array = %d", s);
}
Output:
Program 2
WAP in C to find the largest element in the array.
Code:
#include<stdio.h>
int main()
{
int n;
printf("Enter the number of elements. - ");
scanf("%d", &n);
int a[n];
printf("Enter the values.\n");
for(int i=0; i<n; i++)
scanf("%d", &a[i]);
int s=a[0];
for(int i=1; i<n; i++)
{
if(a[i]>s)
s=a[i];
}
printf("Largest Element = %d", s);
}
Output:
Program 3
WAP in C to print the number of odd and even numbers and print the numbers.
Code:
#include<stdio.h>
int main()
{
int n;
printf("Enter the number of elements. - ");
scanf("%d", &n);
int a[n];
printf("Enter the values.\n");
for(int i=0; i<n; i++)
scanf("%d", &a[i]);
int o=0, e=0;
for(int i=0; i<n; i++)
{
if(a[i]%2==0)
++e;
else ++o;
}
printf("No. of odd nos. = %d\n", o);
printf("Odd numbers = ");
for(int i=0; i<n; i++)
{
if(a[i]%2!=0)
printf("%d, ", a[i]);
}
printf("\nNo. of even nos. = %d\n", e);
printf("Even numbers = ");
for(int i=0; i<n; i++)
{
if(a[i]%2==0)
printf("%d, ", a[i]);
}
}
Output:
Program 4
WAP in C to find the factorial of the given number.
Code:
#include<stdio.h>
int fact(int f) {
int c=1;
if(f==0)
return 1;
else
{
while(f!=1)
{
c*=f;
--f;
}
return c;
}
}
int main() {
int a;
printf("Enter an integer. - ");
scanf("%d", &a);
int i = fact(a);
printf("factorial of the given no. = %d", i);
}
Output:
Session 8
Program 1
WAP in C to