You are on page 1of 30

Program #1

Purpose: WAP to read two integers from user and print both the numbers
with their sum.
Code:
//Program to read two integers from user and print both the numbers with
their sum
//Divyanshu Mawar EA
#include <stdio.h>
int main()
   {
    int x, y, sum;
    printf("\nInput the first integer: ");
    scanf("%d", &x);
    printf("\nInput the second integer: ");
    scanf("%d", &y);
    sum = x + y;
    printf("\nSum of the above two integers = %d\n", sum);
    return 0;
}
Program #2
Purpose: WAP to read numbers for five subjects and print their sum.
Code:
//Program to read numbers for five subjects and print their sum
//Divyanshu Mawar EA
#include<stdio.h>
 
int main() {
   int science, english, maths, biology, physics, sum, total = 500;
   float per;
 
   printf("\nEnter marks of 5 subjects : ");
   scanf("%d %d %d %d %d", &science, &english, &maths, &biology, &physics);
 
   sum = science + english + maths + biology + physics;
   printf("\nSum : %d", sum);
 
   return 0;
}
Program #3
Purpose: WAP to read two floating type numbers from user.
Calculate their sum, difference, product and average Print both the
numbers with their sum and average
Code:
//Divyanshu Mawar EA
#include<stdio.h>
int main()
{
    float num1,num2;
    printf("enter both numbers:");
    scanf("%f %f",&num1,&num2);
    printf("the two numbers are %f and %f\n",num1,num2);
    printf("the sum of two numbers is: %f\n",num1+num2);
    printf("the difference of two numbers is: %f\n",num1-num2);
    printf("the multiplication of two numbers is: %f\n",num1*num2);
    printf("the average of two numbers is: %f\n",(num1+num2) /2);
    return 0;
}
Program #4
Purpose: WAP to read Principal amount and time for a loan application. Take
Rate of interest as a symbolic constant. Calculate Simple interest and display
results
Code:
//Divyanshu Mawar EA
#include <stdio.h>    
int main() {  
    float principle, rate, time, simpleInterest;    
    printf("Enter Principle Amount\n");  
    scanf("%f", &principle);  
    printf("Enter Rate of Interest\n");  
    scanf("%f", &rate);  
    printf("Enter Time of Loan\n");  
    scanf("%f", &time);  
    simpleInterest = (principle * rate * time)/100;  
   
    printf("Simple Interest = %.2f", simpleInterest);  
   
    return 0;  
}
Program #5
Purpose: WAP to read temperature in Celsius and convert it to
Fahrenheit and vice-versa. Display the results of the program
Code:// conversion from celsius to fahrenheit
//Divyanshu Mawar EA
#include <stdio.h>

int main()

  float tempc, tempf;

  printf("enter temperature in celcius:");

    scanf("%f", &tempc);

    printf("enter temperature in farenheit:");

    scanf("%f", &tempf);

    printf("celcius to farenheit: %0.2f \n", 1.8 * tempc + 32);

    printf("farenheit to celcius: %0.2f", (tempf - 32) / 1.8);

    return 0;

}
Program #6
Purpose: To swap two numbers using third variable
Code:
Divyanshu Mawar EA
#include <stdio.h>
int main()
{
   int a, b, swap;    
   printf("Enter two numbers a and b ");
   scanf("%d %d", &a, &b);
   swap = a;
   a = b;
   b = swap;
  printf("\n After swapping \na = %d\nb = %d\n", a, b);
  return 0;
}
Program #7
Purpose: To swap two numbers without using third variable
Code:
//Divyanshu Mawar EA
#include <stdio.h>

// program to swap two numbers without using third variable//

int main()

    int num1, num2;

    printf("enter num1 and num2 respectively: ");

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

    // code to swap numbers//

    printf("num1 and num2 are %d and %d\n", num1, num2);

    num1 = num1 + num2;

    num2 = num1 - num2;

    num1 = num1 - num2;

    printf("after swapping num1 is %d and num2 is %d", num1, num2);

    return 0;
Program #8
Purpose: Convert time from -hours to seconds,-hours to minutes,-minutes to
seconds,-days to seconds

Code:
//Divyanshu Mawar EA
#include<stdio.h>
void main(){
    int h;
    int s;
    int m;
    int d;
    printf("Enter the hours to be converted in seconds:  ");
    scanf("%d",&h);
    s=h*3600;
    printf("hours into seconds: %d \n",s);
   
    printf("Enter the hours to be converted in minutes:  ");
    scanf("%d",&h);
   
    m=h*60;
    printf("hours into minutes: %d \n",m);
   
    printf("Enter the minutes to be converted into seconds: ");
    scanf("%d",&m);
   
    s=m*60;
    printf("minutes to seconds:  %d \n",s);
   
    printf("Enter the days to converted into seconds:  ");
    scanf("%d",&d);
   
    s=d*86400;
    printf("days into seconds: %d",s);      
}
Program #9
Purpose: To find area and perimeter of rectangle

Code: //To find the area of rectangle


//Divyanshu Mawar
#include <stdio.h>

// to find perimeter and area of rectangle

int main()

    float length, breadth;

    printf("enter the length and breadth of rectangle respectively:");

    scanf("%f %f", &length, &breadth);

    printf("the perimeter of rectangle is %f\n", 2 * (length + breadth));

    printf("the area of rectangle is %f", length * breadth);

    return 0;

Program #10
Purpose: To print perimeter and area of circle

Code:
//Divyanshu Mawar
#include <stdio.h>
#define PI 3.14f
int main()
{
    float rad,area, perm;
    printf("Enter radius of circle: ");
    scanf("%f",&rad);
    area=PI*rad*rad;
    perm=2*PI*rad;
    printf("Area of circle: %f \nPerimeter of circle: %f\n",area,perm);
    return 0;
}
Program #11
Purpose: To apply mathematical operation on ASCII value of variables

Code:
//Divyanshu Mawar EA
#include <stdio.h>

int main()

    char a, b, c;

    printf("enter two characters:");

    scanf("%c %c", &a, &b);

    printf("characters are %c and %c\n", a, b);

    printf("ascii value of characters is %d %d\n", a, b);

    c = a + b;

    printf("the sum of ascii values is %d\n", c);

    return 0;

Program #12
Purpose: Mathematical operation on character to get other character

Code:
//Divyanshu Mawar EA
#include <stdio.h>
int main()
{
    char a , b , c;
    printf("Enter two characters:");
    scanf("%c %c ",&a,&b);
    printf("characters are %c and %c\n",a,b);
    printf("ascii value of characters is %d %d\n",a,b);
    c=a+b;
    printf("the sum of ascii values is %d\n",c);
    printf(" %d is ascii value of %c\n",c,c);
    return 0;
}
Program #13
Purpose: To find maximum of two numbers by using if else statement

Code:
//Divyanshu Mawar EA
#include <stdio.h>
int main()
{
    int num1, num2;
    printf("enter two numbers:\n");
    scanf("%d %d", &num1, &num2);
    if (num1 > num2)
    {
        printf("%d is greater than %d", num1, num2);
    }
    else
    {
        printf("%d is greater than %d", num2, num1);
    }
    return 0;
}
Program #14
Purpose: To find maximum of three numbers by if else if statement

Code:
//Divyanshu Mawar EA
#include <stdio.h>  
int main()  {  
    int a, b, c, max;  
    printf("Enter Three Integers\n");  
    scanf("%d %d %d", &a, &b, &c);  
    if(a > b){
        if(a > c)
            max = a;
        else
            max = c;
    } else {
        if(b > c)
            max = b;
        else
            max = c;
    }
    printf("Maximum Number is = %d\n", max);  
    return 0;  
}
Program #15
Purpose: To find grades on the basis of marks, using if-else and relational
operators

Code:
//Divyanshu Mawar EA
#include<stdio.h>
int main()
{
   int score;
   char grade;
   printf("Enter score(0-100): ");
   scanf("%d",&score);
   if(score<0 || score>100) {
     printf("Invalid Score");
     return 0;
   }
   if(score>=90 && score<=100)
     grade = 'A';
   else if(score>=80)
     grade = 'B';
   else if(score>=70)
     grade = 'C';
   else if(score>=60)
     grade = 'D';
   else if(score>=50)
     grade = 'E';
   else
     grade = 'F';
   printf("Grade: %c\n", grade);
   return 0;
}
Program #16
Purpose: To find nature of roots of quadratic equations

Code:
#include <stdio.h>
#include <math.h>
int main()
{
    int A, B, C, discriminant;
    printf("quadratic equation is of type Ax^2+Bx+c\n");
    printf("enter the values of A,B and C:");
    scanf("%d %d %d", &A, &B, &C);
    discriminant = ((B * B) - 4 * A * C);
    if (sqrt(discriminant) == 0)
    {
        printf("roots are real and equal");
    }
    else if (sqrt(discriminant) >= 0)
    {
        printf("roots are real and unequal");
    }
    else
    {
        printf("roots are not real");
    }
    return 0;
}
Program #17
Purpose: WAP to read two integers and an operator (+,-,*,/,%). Use switch-
case statement to get result of operator on two integers.

Code:
//Divyanshu Mawar EA
#include <stdio.h>
int main()
{
    char ch;
    int a, b;
    printf("enter the operator for operation you want to perform:\n");
    scanf("%c", &ch);
    printf("enter the numbers on which you want to perform:\n");
    scanf("%d %d", &a, &b);
    switch (ch)
    {
    case '+':
        printf("sum of %d and %d is %d", a, b, a + b);
        break;
    case '-':
        printf("subtract of %d and %d is %d", a, b, a - b);
        break;
    case '*':
        printf("mutiplication of %d and %d is %d", a, b, a * b);
        break;
    case '/':
        printf("division of %d and %d is %d", a, b, a / b);
        break;
    case '%':
        printf("modulus of %d and %d is %d", a, b, a % b);
        break;
    default:
        printf("invaalid operator\n");
        break;
    }
    return 0;
}
Program #18
Purpose: WAP to print natural numbers till n using while loop. Also print
reverse counting from m to 1. Get m,n from user at runtime

Code:
//Divyanshu Mawar EA
#include <stdio.h>
int main()
{
    int m, n, i = 1, j;
    printf("enter the number till you want to print counting:\n");
    scanf("%d", &m);
    printf("Enter the number from you want to print reverse counting to 1:\
n");
    scanf("%d", &n);
    j = n;
    printf("__________\n");
    while (i <= m)
    {
        printf("%d\n", i);
        i++;
    }
    printf("__________\n");
    while (j > 0)
    {
        printf("%d\n", j);
        j--;
    }
    return 0;
}
Program #19
Purpose: Program to read a number and print its individual digits

Code:
//Divyanshu Mawar EA
#include <stdio.h>
int main()
{
    int number, digit;
    printf("Enter any number:");
    scanf("%d", &number);
    while (number > 0)
    {
        digit = number % 10;
        printf("%d\n", digit);
        number /= 10;
    }
    return 0;
}
Program #20
Purpose: WAP to compute x n using while statement

Code:
#include <stdio.h>
int main()
{
    int m, n, pow = 1, i = 1;
    printf("Enter the value of base:");
    scanf("%d", &m);
    printf("Enter the value of power:");
    scanf("%d", &n);
    while (i <= n)
    {
        pow = pow * m;
        i++;
    }
    printf("%d\n", pow);
    return 0;
}
Program #21
Purpose: WAP to generate multiplication tables using nested do-while
statements

Code:
//Divyanshu Mawar EA

#include <stdio.h>

int main()

    int num, i = 1;

    printf("enter the number whose table has to be printed:\n");

    scanf("%d", &num);

    do

    {

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

        i++;

    } while (i <= 10);

    return 0;

}
Program #22
Purpose: WAP to print following patterns: triangle of '*', triangle of digits

Code:
//Divyanshu Mawar EA

#include <stdio.h>

int main()

    int num, i = 1, j = 1, k = 1, l = 1, m = 1, n = 1;

    printf("Enter the value of rows to be printed:\n");

    scanf("%d", &num);

    for (i; i <= num; i++)

    {

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

        {

            printf("*  ");

        }

        printf("\n");

    }

    for (k; k <= num; k++)

    {

        for (l = 1; l <= k; l++)

        {

            printf("%d ", k);

        }

        printf("\n");

    }

    for (m; m <= num; m++)

    {

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

        {
            printf("%d ", n);

        }

        printf("\n");

    }

    return 0;

}
Program #23
Purpose: Program To read an integer and print sum of its digits using while
loop. Construct and print reverse on n-digit number using do-while loop

Code:
//Divyanshu Mawar EA
#include <stdio.h>
int main()
{
    int num, digit = 0, sum = 0, reverse = 0, digit1 = 0;
    printf("Enter any number:");
    scanf("%d", &num);
    while (num > 0)
    {
        digit = num % 10;
        sum += digit;
        num /= 10;
    }
    printf("the sum of digits of number %d\n", sum);
    printf("Enter the number to be reverse:\n");
    scanf("%d", &num);
    do
    {
        digit1 = num % 10;
        reverse = reverse * 10 + digit1;
        num /= 10;
    } while (num > 0);
    printf("reverse of number is %d", reverse);
    return 0;
}
Program #24
Purpose: Program To determine if given number is prime or composite

Code:
//Divyanshu Mawar EA
#include <stdio.h>
int main()
{
    int num, count = 0, i;
    printf("enter the number to be checked:\n");
    scanf("%d", &num);
    for (i = 1; i <= num; i++)
    {
        if (num % i == 0)
        {
            count++;
        }
    }
    if (count == 2)
    {
        printf("%d is a prime number\n", num);
    }
    else
    {
        printf("%d is a composite number\n", num);
    }
    return 0;
}
Program #25
Purpose: To print sum of first n odd natural numbers

Code:
//Divyanshu Mawar EA
#include <stdio.h>
int main()
{
    int n, sum = 0, i;
    printf("enter the value of n:");
    scanf("%d", &n);
    for (i = 1; i <= 2 * n; i += 2)
    {
        sum += i;
    }
    printf("the sum of first %d odd natural number is %d\n", n, sum);
    return 0;
}
Program #26
Purpose: To print sum of series: 1+1/2+…..+1/n

Code:
//Divyanshu Mawar
#include <stdio.h>
int main()
{
    int num, i;
    float sum = 0;
    printf("Enter the value of n:\n");
    scanf("%d", &num);
    for (i = 1; i <= num; i++)
    {
        sum = sum + (1.0 / i);
    }
    printf("The sum of series till %d is %f\n", num, sum);
    return 0;
}

You might also like