C Programs Assignment 1
C Programs Assignment 1
Computer programs
1. Write a C program to swap the valuess of two variables using with and without third
variable
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;
}
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);
#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;
}
#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;
}
#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;
}
#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;
}
#include <stdio.h>
int main(){
int units;
float billAmount, surcharge, totalBill;
#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;
}
#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;
}
#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;
}
#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;
}
#include<stdio.h>
void main()
{
for(int i=1; i<=5; i++)
{
for(int j=1; j<=i; j++)
{
printf("%d",i);
}
printf("\n");
}
}
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;
}