2024067359
Computer programs
1. Write a C program to swap the valuess of two variables using with and without third
variable
With 3rd variable:
#include <stdio.h>
int main()
{
 int a,b,c;
 printf("enter a value");
 scanf("%d",&a);
 printf("enter b value");
 scanf("%d",&b);
 c=b;
 b=a;
 a=c;
 printf("a value is %d \n",c);
 printf("b value is %d \n",b);
}
Without 3rd variable:
#include <stdio.h>
int main()
{
 int a,b;
 printf("enter a value");
 scanf("%d",&a);
 printf("enter b value");
 scanf("%d",&b);
 a=a+b;
 b=a-b;
 a=a-b;
 printf("a value is %d \n",a);
 printf("b value is %d \n",b);
}
2. Write a C program to evaluate the expression
 for a=9, b=13, c=3
x = a-b/3.0+c*2-1;
y = a-(float)b/(3+c)*(2-1);
z = a-((float)b/(3+c)*2)-1;
#include <stdio.h>
int main() {
 int a=9,b=13,c=3,x,y,z;
 x = a-b/3.0+c*2-1;
 y = a-(float)b/(3+c)*(2-1);
 z = a-((float)b/(3+c)*2)-1;
 printf("a-b/3.0+c*2-1=%d \n",x); //output x=9
 printf("a-(float)b/(3+c)*(2-1)= %d \n",y); //output y=6
 printf("a-((float)b/(3+c)*2)-1=%d \n",z); //output z=3
 return 0;
}
3. Write a C program to check whether a number is divisible by 5 and 11 or not.
#include <stdio.h>
int main() {
 int n;
 printf("enter a number:");
 scanf("%d",&n);
 if(n%5==0 && n%11==0){
 printf("number is divisible by 5 and 11\n");
 }
 else{
 printf("number is not divisible by 5 and 11\n");
 }
 return 0;
}
4. Write a C program to check whether a number is a palindrome or not.
#include <stdio.h>
int main() {
 int n, rev = 0, rem,org;
 printf("Enter n value");
 scanf("%d",&n);
 org=n;
 while (n != 0) {
 rem = n % 10;
 rev = rev * 10 + rem;
 n /= 10;
 }
 if (rev==org) {
 printf("It is a palindrome number = %d", rev);
 }
 else {
 printf("it not a palindrome number");
 }
 return 0;
}
5. Write a C program to check whether a year is leap year or not.
#include <stdio.h>
int main() {
 int year;
 printf("enter a year:");
 scanf("%d",&year);
 if(year%400==0) || (year%4==0 && n%100==0){
 printf("It is a leap year %d",year);
 }
 else{
 printf("it is not a leap year");
 }
 return 0;
}
6.Write a C program check whether it is vowel or consonant using switch
#include <stdio.h>
int main() {
 char choice;
 printf("enter an alphabet:");
 scanf("%c",&choice);
 switch(choice){
 case 'a':printf("it is a vowel");
 break;
 case 'e':printf("It is a vowel");
 break;
 case 'i':printf(" It is a vowel");
 break;
 case 'o':printf("It is a vowel");
 break;
 case 'u':printf("It is a vowel");
 break;
 default:printf("It is a consonant");
 }
 return 0;
}
7. Write a C program to input any character and check whether it is alphabet, digit or special
character.
#include <stdio.h>
int main() {
 char choice;
 printf("enter an alphabet:");
 scanf("%c",&choice);
 switch(choice){
 case 'a' ... 'z':printf("It is an alphabet");
 break;
 case 'A' ... 'Z':printf("It is an alphabet");
 break;
 case '1' ... '9':printf(" It is digit");
 break;
 default:printf("It is special character");
 }
 return 0;
}
8. Write a C program to check whether a character is uppercase or lowercase alphabet.
#include <stdio.h>
int main() {
 char choice;
 printf("enter an alphabet:");
 scanf("%c",&choice);
 switch(choice){
 case 'a' ... 'z':printf("It is lowercase alphabet");
 break;
 case 'A' ... 'Z':printf("It is uppercase alphabet");
 break;
 default:printf("Invalid information");
 }
 return 0;
}
9. Write a C program to input month number and print number of days in that month.
#include <stdio.h>
int main() {
 int n;
 printf("enter month number:");
 scanf("%d",&n);
 if(n==1 || n==3 || n==5 || n==7 || n==8 || n==10 || n==12){
 printf("31 Days");
 }
 else if(n==2){
 printf("29/28 Days");
 }
 else if(n==4 || n==6 || n==9 || n==11){
 printf("30 Days");
 }
 else
 {
 printf("Invalid month number");
 }
 return 0;
}
10. Write a C program to find all roots of a quadratic equation.
#include <stdio.h>
#include <math.h>
int main() {
 float a,b,c;
 float discri, root1,root2,real, imagine;
 printf("enter coefficients a,b,and c:");
 scanf("%f %f %f",&a,&b,&c);
 discri= b*b-4*a*c;
 if(discri>0){
 root1 = (-b + sqrt(discri)) / (2 * a);
 root2 = (-b - sqrt(discri)) / (2 * a);
 printf("Roots are real and different.\n");
 printf("Root 1 = %f\n", root1);
 printf("Root 2 = %f\n", root2);
 }
 else if (discri == 0) {
 root1 = root2 = -b / (2 * a);
 printf("Roots are real and the same.\n");
 printf("Root 1 = Root 2 = %f\n", root1);
 }
 else {
 real = -b / (2 * a);
 imagine = sqrt(-discri) / (2 * a);
 printf("Roots are complex and different.\n");
 printf("Root 1 = %f + %fi\n", real, imagine);
 printf("Root 2 = %f - %fi\n", real,imagine);
 }
 return 0;
}
11. Write a C program to calculate profit or loss.
#include <stdio.h>
int main() {
 int cp,sp,profit,loss;
 printf("enter cost price:");
 scanf("%d",&cp);
 printf("enter sale price:");
 scanf("%d",&sp);
 if(sp>cp){
 profit=sp-cp;
 printf("it is profit of %d ",profit);
 }
 else if(cp>sp){
 loss=cp-sp;
 printf("it is loss of %d",loss);
 }
 else{
 printf("There is no profit or loss");
 }
 return 0;
}
12. Write a C program to input marks of five subjects Physics, Chemistry, Biology,
Mathematics and Computer.
Calculate percentage and grade according to following:
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
#include <stdio.h>
int main() {
 int p,c,b,m,cs,total;
 float per;
 printf("enter physics marks:");
 scanf("%d",&p);
 printf("enter chemistry marks:");
 scanf("%d",&c);
 printf("enter biology marks:");
 scanf("%d",&b);
 printf("enter mathematics marks:");
 scanf("%d",&m);
 printf("enter computer science marks:");
 scanf("%d",&cs);
 total=p+c+b+m+cs;
 per=((float)total/500)*100;
 printf("Percentage= %f \n",per);
 if(per>=90){
 printf("Grade A");
 }
 else if(per>=80){
 printf("Grade B");
 }
 else if(per>=70){
 printf("Grade C");
 }
 else if(per>=60){
 printf("Grade D");
 }
 else if (per>=40){
 printf("Grade E");
 }
 else{
 printf("Grade F");
 }
 return 0;
}
13.Write a C program to input basic salary of an employee and
calculate its Gross salary according to following:
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%
#include <stdio.h>
int main() {
 int basic_salary,gross,hra,da;
 printf("enter basic salary:");
 scanf("%d",&basic_salary);
 if(basic_salary<=10000){
 hra=basic_salary*0.2; //hra=House Rent Allowance
 da=basic_salary*0.8; //da=Dearness Allowance
 }
 else if(basic_salary<=20000){
 hra=basic_salary*0.25;
 da=basic_salary*0.9;
 }
 else{
 hra=basic_salary*0.3;
 da=basic_salary*0.95;
 }
 gross=basic_salary+hra+da;
 printf("Gross salaty= %d",gross);
 return 0;
}
14. Write a C program to input electricity unit charges and
calculate total electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
#include <stdio.h>
int main(){
 int units;
 float billAmount, surcharge, totalBill;
 printf("Enter the total units consumed: ");
 scanf("%d", &units);
 if (units <= 50){
 billAmount = units * 0.50;
 }
 else if (units <= 150){
 billAmount = (50 * 0.50) + ((units - 50) * 0.75);
 }
 else if (units <= 250){
 billAmount = (50 * 0.50) + (100 * 0.75) + ((units - 150) * .)
 }
 else{
 billAmount = (50 * 0.50) + (100 * 0.75) + (100 * 1.20)+(units- 250) * 1.50)
 }
 surcharge = 0.20 * billAmount;
 totalBill = billAmount + surcharge;
 printf("Electricity Bill = Rs. %.2f\n", totalBill);
 return 0;
}
15. Write a C program to calculate sum of 10 natural numbers
#include <stdio.h>
int main() {
 int i,sum=0;
 for(i=1;i<=10;i+=1){
 sum+=i;
 }
 printf("sum of 10 natural numbers= %d",sum);
 return 0;
}
16. Write a C program to find the sum of series 1^2+2^2+3^2+.....+n^2
#include <stdio.h>
int main() {
 int n;
 printf("Enter a number:");
 scanf("%d",&n);
 n=(n*(n+1)*(2*n+1))/6;
 printf("sum of series= %d",n);
 return 0;
}
17. Display numbers in the following format
 12345
 12345
 12345
 12345
 12345
#include <stdio.h>
int main() {
 int i,j,rows;
 printf("Enter no of rows:");
 scanf("%d",&rows);
 for(i=rows;i>=1;--i){
 for(j=1;j<=rows;j++){
 printf("%d ",j);
 }
 printf("\n");
 }
 return 0;
}
18. Display numbers in the following format
 1
 12
 123
 1234
 12345
#include <stdio.h>
int main() {
 int i,j,rows;
 printf("Enter no of rows:");
 scanf("%d",&rows);
 for(i=1;i<=rows;i++){
 for(j=1;j<=i;j++){#include
 printf("%d ",j);
 }
 printf("\n");
 }
 return 0;
}
19. Display numbers in the following format
 1
 22
 333
 4444
 55555
#include<stdio.h>
void main()
{
 for(int i=1; i<=5; i++)
 {
 for(int j=1; j<=i; j++)
 {
 printf("%d",i);
 }
 printf("\n");
 }
}
20. Write a c program to check whether given number is prime or not
#include <stdio.h>
int main() {
 int n,i,flag=1;
 printf("Enter a number:");
 scanf("%d",&n);
 for(i=2; i<=n/2; ++i){
 if(n%i==0){
 flag=0;
 }
 }
 if(flag==0){
 printf("It is not a prime number %d",n);
 }
 else{
 printf("It is a prime number %d",n);
 }
 return 0;
}