Wednesday, April 20, 2022

C

 PART A

(1)

 

(C) Write the output of the following code [CODE] [07th-Sep-18]

 

#include<stdio.h>

int main()

{

    int i=10, j=15; float f=12.4; char ch ='V';

    printf("i=%d\n j=%d\n f=%d\n ch =%c",i,j,f,ch);

}

 

 

Right Answer:

 

#include<stdio.h>

int main()

{

    int i=10, j=15; float f=12.4; char ch ='V';

    printf("i=%d\n j=%d\n f=%f\n ch =%c",i,j,f,ch);

}

 

 

 

 

(2)

 

c) Write a program that calculates the area of a circle and triangle. Use the necessary inputs from keyboard. [07th-Sep-18]

Area of Triangle

#include<stdio.h>

int main()

{

      //Area of a triangle = 0.5*base*height     

       float base, height, area;

       printf("Enter the base and height value : ");

       scanf("%f %f", &base, &height);

       area = 0.5*base*height;

       printf("Area of triangle is: %.2f",area);

       return (0);

}

-------------------------------------------------------------------------------------------------------------------------------

Area of a Circle

#include<stdio.h>

#define pi 3.1416

int main()

{

       //Area of a circle = pi r*r

       float           radius, area;

       printf("Enter the radius value :  ");

       scanf("%f ", &radius);

 

       area = pi*radius*radius;

       printf("Area of a circle is: %.2f",area);

       return (0);

}

 

(3)

(a) Check whether the following code is correct or not. IF any error is found, rewrite the full code correctly [CODE] [08th-July-19]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(b) write a program in C that takes input an integer number and finds whether the number is divided by either 3 or 5 or not [08th-July-19]

 

#include<stdio.h>

void main()

{

         int num;

         printf ("Please enter the integer number :");

         scanf("%d",&num);

         if(num%3==0 && num%5==0)

          {

             printf("This number is divided by 3 and 5 ");

          }

         else

         {

               printf("This number is not divided by 3 and 5 ");

         }

return 0;

}

 

c) Write the output of the following program [CODE] [07th-Sep-18]

#include<stdio.h>

int main()

{

int a=-5, b=11, c, d; float e;

c=b++ -a++; d=--b -++a; e=c/d;

printf("Output: \n a=%d b=%d \ c=%d d=%d \n e=%0.2f",a,b,c,d,e);

}

 

a) Check whether the following code is correct or not. If any error is found, rewrite the full code correctly[CODE] [06th-JUL-18]

 

 

 

 

b) write a program in C that takes input three numbers and find the minimum of them [06th-JUL-18]

#include<stdio.h>

int main()

{

    int a, b, c;

    printf("Enter three numbers : ");

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

   

    if(a<b && a<c)

        printf("%d is the minimum number.", a);

    else if(b<a && b<c)

        printf("%d is the minimum number.", b);

    else

    printf("%d is the minimum number.", c);

   

    return 0;

}

(4)

 

(a) Write a program in C that takes input four numbers and find the 3rd minimum of them[08th-July-19]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(b) Write a program in C that takes input taka and converts it to dollar [we know 1 dollar=85.6 taka] [08th-July-19]

 

#include <stdio.h>

double convert(void);

int main(void){

 

    double dollar;

 

    dollar=convert();

    printf("In dollar= %0.2lf\n",dollar);

 

    return 0;

}

 

double convert(void)

{

    double taka,multi_value;

    multi_value=86.16;

 

    printf("enter taka amount: \n");

    scanf("%lf", &taka);

 

    return taka/multi_value;

 

}

 

Or

 

#include <stdio.h>
double convert(void);
int main(void){

    double taka;

    taka=convert();
    printf("In taka= %0.2lf\n",taka);

 

/*

%3.2f //(print as a floating point at least 3 wide and a precision of 2)
%0.2lf //(print as a floating point at least 0 wide and a precision of 2)
%.2lf //(print as a floating point at least 0(default) wide and a precision of 2)

*/

    return 0;
}

double convert(void)
{
    double dollars,multi_value;
    multi_value=80.0;

    printf("enter dollar amount: \n");
    scanf("%lf", &dollars);

    return dollars*multi_value;

}

 

 

 

 

 

c) Correct the error in the following program and write the output[CODE] [07th-Sep-18]

#include<stdio.h>

int main()

{

 

    int x=82, y=40;

 

    if ((x/y)==(x%y))

 

        printf("Success\n");

      

    else

        printf("Failed");

}

 

Error: if ((x/y)==(x%y)) ; (Semicolon)

Output: Success

a) write a program in C that takes input an integer number and finds whether the number is even or odd [06th-JUL-18]

#include <stdio.h>

int main() {

    int num;

    printf("Please enter an integer: ");

    scanf("%d", &num);

 

    // true if num is perfectly divisible by 2

    if(num % 2 == 0)

        printf("%d is even.", num);

    else

        printf("%d is odd.", num);

   

    return 0;

}

 

b) write a program in C that takes input a kilometer value and converts it to mile [we know 1 mile =1.61 kilometer] [06th-JUL-18]

//kilometer to miles = 0.621371*Kilometer

#include<stdio.h>

 

void main() {

  float km, miles;

              printf(" ----- Program to convert Kilometer to Miles -----  ");

              printf("\n\n Please enter Length in Kilometer  : ");

              scanf("%f", &km);

 

    miles = (0.621371*km);

 

    printf("%f Kilometer to Miles = %f  \n",km,miles);

 

}

 

Or

//miles to km = miles * 1.609344;

#include<stdio.h>

 

void main() {

 

  float km, miles;

 

              printf(" ----- Program to convert miles to km -----  ");

              printf("\n\n Please enter Length in miles  : ");

              scanf("%f", &miles);

 

    //km = (1.61*miles);

    km = miles * 1.609344;

    printf("%f miles to km = %f  \n",miles, km );

 

}

 

 

PART B

(1)

 

b) Write a program that calculates the following series using do-while loop 2*5*8*11*14 [07th-Sep-18]

#include <stdio.h>

int main()

{

 // Write C code here

//

    int i=2;

    int m=1;

    do {

        printf("%d,",i);

        m*=i;

        i+=3;

    }

   

    while (i<=14);

    printf("\n");

    printf("multiplication of series: %d",m);

 

    return 0;

}

 

c) Convert the following code sample from for loop to do-while. Finally write the output[CODE] [07th-Sep-18]

For Loop

 

#include<stdio.h>

int main()

{

    int x,y=95;

    for (x=100; x>y; x-=3);

        printf("%d is smaller than %d\n",x,y);

}

 

Do-While loop

#include<stdio.h>

int main()

{

    int x=100, y=95;

    do{

        printf("%d is smaller then %d\n",y,x);

        x-=3;

    } while(x>y);

    return 0;

 

}

 

 

 

 

 

 

 

 

 

 

 

b) write a program in C that takes input three numbers and find and seconds highest number of them. [06th-JUL-18]

#include <stdio.h>

 

int main()

{

    int a, b, c;

    printf("Values: ");

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

    if(a>b && a>c)

    {

        if(b>c)

            printf("2nd largest: %d", b);

        else

            printf("2nd largest: %d", c);

    }

    else if(b>c && b>a)

    {

        if(c>a)

            printf("2nd largest: %d", c);

        else

            printf("2nd largest: %d", a);

    }

    else if(a>b)

            printf("2nd largest: %d", a);

        else

            printf("2nd largest: %d", b);

    return 0;

}

 

(2)

(a) Write a program in C that computers the result of following series

1-5+9-………………+n [Input the values of n from key board] [08th-July-19]

 

#include <stdio.h>

int main()

{

int j=1;

int s=0;

int n=0;

printf("input the value of n:");

 scanf("%d", &n); // input value from keyboard

for(int i=1;i<=n;i+=4)

{

printf("%d,",i); s+=i; j++; }

printf("\n%d",s);

return 0;

}

 

 

 

 

b) Final all the mistakes from the following code and correct it. Write the output[CODE] [07th-Sep-18]

Wrong

#include<stdio.h>

int main()

{

    float y=10.5; int x=15;

    while(x<18);

    {

        x+== 2;

        y == x;

        printf("%d%f\n",x,y);

    }

}

 

c) write a program that generates the following output pattern using loops [07th-Sep-18]

#include <stdio.h>

void main()

{

   int i,j,rows;

   printf("Input number of rows : ");

   scanf("%d",&rows);

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

   {

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

                 printf("*");

              printf("\n");

   }

}

 

(a) Write down a program using for loop that gives output like below [CODE] [06th-JUL-18]

#include<stdio.h>

int main()

{

    int i;

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

    {

        printf("%d\n",i);

    }

    getch();

}

 

(3)

 

(b) Write a program in C that inputs 6 integer number to an array and prints the minimum of them [08th-July-19]

 

 

 

 

 

 

 

 

 

 

b) Write a program using a one–dimensional array that scans semester GPA of Five students and finds their averages GPA [07th-Sep-18

 

 

 

 

c) Write the code for printing the values of array B and write the output[CODE] [07th-Sep-18]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Write a program in C that computes the result of following series: [06th-JUL-18]

1+5+9+……. +x n [Input the value of n from key board]

 

 

 

 

 

 

 

 

 

 

 

 

(4)

 

(a) Write down a program in C, that has a function named AVG that takes three integer values and returns the average of average of them and then print it form main function[08th-July-19]

 

(a) What a program in C? Write down a program in C, that has a function named AVG that takes three integer values and returns the average of average of them and then print it form main function[08th-July-19]

 

#include<stdio.h>

void avg(int, int, int);

void main()

{

    int x,y,z; //Declaring Variables

    printf("Please enter the three numbers ");

    scanf("%d %d %d",&x,&y,&z); //Input Numbers

 

    avg(x,y,z);  //Calling Function

}

void avg (int a, int b, int c)

{

    float Average;

    Average=(a+b+c)/3;

    printf("%.2f Average three number is ",Average);

}

Or

#include<stdio.h>

void average(int,int, int);

void main()

{

   int x, y, z;           //Declaring Variables

   printf("Enter Three Numbers : \n");

   scanf("%d %d %d",&x, &y, &z);     //Input Numbers

 

   average(x,y,z);  //Calling Function to find the Average of three numbers

 

}

void average(int a,int b, int c)

{

    float avg;

    avg=(a+b+c)/3.0;

    printf("Average of Three Numebers is : %f", avg);

}

 

Or

#include <stdio.h>

void main()

{

            int n1,n2,n3;

            float avg;

    printf("\n Please enter the three numbers: " );

            scanf("%d %d %d",&n1,&n2,&n3);

            avg=(n1+n2+n3)/3;

            printf("\n Avrages: %0.2f",avg);

}

 

OR

 

#include <stdio.h>

int main() {

    int n, i;

    float num[100], sum = 0.0, avg;

 

    printf("Enter the numbers of elements: ");

    scanf("%d", &n);

 

    while (n > 100 || n < 1) {

        printf("Error! number should in range of (1 to 100).\n");

        printf("Enter the number again: ");

        scanf("%d", &n);

    }

 

    for (i = 0; i < n; ++i) {

        printf("%d. Enter number: ", i + 1);

        scanf("%f", &num[i]);

        sum += num[i];

    }

 

    avg = sum / n;

    printf("Average = %.2f", avg);

    return 0;

}

 

(b) Write a program in C that computes the result of following series: 1x3x5x……x n

 [input the value of n from key board] [08th-July-19]

 

 

 

 

 

 

 

 

 

 

 

Write a program in C that computes the result of following series: [06th-JUL-18]

1x3x5x….x n [Input the value of n from key board]

 

 

 

 

 

 

 

 

 

 

Display the pattern like right angle using an asterisk

 

#include <stdio.h>

void main()

{

   int i,j,rows;

   printf("Input number of rows : ");

   scanf("%d",&rows);

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

   {

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

                 printf("*");

              printf("\n");

   }

}

 

 

 

Display the sum of n number of odd natural number

 

#include <stdio.h>

void main()

{

   int i,n,sum=0;

 

   printf("Input number of terms : ");

   scanf("%d",&n);

   printf("\nThe odd numbers are :");

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

   {

     printf("%d ",2*i-1);

     sum+=2*i-1;

   }

   printf("\nThe Sum of odd Natural Number upto %d terms : %d \n",n,sum);

}

 

Find the largest of three numbers

 

#include <stdio.h>

void main() {

    int num1, num2, num3,ctr;

 

    printf("Input the values of three numbers : ");

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

    printf("1st Number = %d,\t2nd Number = %d,\t3rd Number = %d\n", num1, num2, num3);

 

   if (num1 > num2 && num1 > num3) ctr=num1;

 

   if (num2 > num3 && num2 > num1) ctr=num2;

 

   if (num3 > num1 && num3 > num2) ctr=num3;

           printf("The %d is the greatest among three. \n",ctr);

 

}

 

 

OR

 

#include <stdio.h>

void main() {

    int num1, num2, num3;

 

    printf("Input the values of three numbers : ");

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

    printf("1st Number = %d,\t2nd Number = %d,\t3rd Number = %d\n", num1, num2, num3);

 

   if ((num1 > num2) && (num1 > num3))

            printf("The 1st Number is the greatest among three. \n");

 

   if ((num2 > num3) && (num2 > num1))

        printf("The 2nd Number is the greatest among three \n");

 

   if ((num3 > num1) && (num3 > num2))

           printf("The 3rd Number is the greatest among three. \n");

}

 

 

 

 

C Program to find Sum and Average of n Number using for Loop

 

#include<stdio.h>

 

int main()

{

  int i,n,Sum=0,numbers;

  float Average;

 

  printf("\nPlease Enter How many Number you want?\n");

  scanf("%d",&n);

 

  printf("\nPlease Enter the elements one by one\n");

  for(i=0;i<n;++i)

   {

     scanf("%d",&numbers);

     Sum = Sum +numbers;

   }

 

  Average = Sum/n;

 

  printf("\nSum of the %d Numbers = %d",n, Sum);

  printf("\nAverage of the %d Numbers = %.2f",n, Average);

 

  return 0;

}

 

OR, While Loop

 

 

#include<stdio.h>

 

void main()

{

  int n, numbers, i=0,Sum=0;

  float Average;

 

  printf("\nPlease Enter How many Number you want?\n");

  scanf("%d",&n);

 

  printf("\nPlease Enter the elements one by one\n");

  while(i<n)

   {

     scanf("%d",&numbers);

     Sum = Sum +numbers;

     i++;

   }

 

  Average = Sum/n;

 

  printf("\nSum of the %d Numbers = %d",n, Sum);

  printf("\nAverage of the %d Numbers = %.2f",n, Average);

 

  return 0;

}

 

OR DO While

 

#include<stdio.h>

 

int main()

{

  int n, numbers, i=0,Sum=0;

  float Average;

 

  printf("\nPlease Enter How many Number you want?\n");

  scanf("%d",&n);

 

  printf("\nPlease Enter the elements one by one\n");

  do

   {

     scanf("%d",&numbers);

     Sum = Sum +numbers;

     i++;

   }while(i<n);

 

  Average = Sum/n;

 

  printf("\nSum of the %d Numbers = %d",n, Sum);

  printf("\nAverage of the %d Numbers = %.2f",n, Average);

 

  return 0;

}

 

 

Let consider the conversion rate of US Dollar and Taka are 1 dollar = 80.45 taka. Write a program that first takes the choice from the user what conversion (hint 1 for dollar to taka or 2 for taka to dollar) he/she wants. Then an amount is given as input. Convert the amount and output the result according to the given choice.

 

#include<stdio.h>

void main ()

{

      int a;

      float b;

      printf("press 1 for dollar to taka\n\n press 2 for taka to dollar\n\n");

      scanf("%d", &a);

 

    switch(a)

      {

      case 1:

            printf("enter value: ");

            scanf("%f", &b);

            printf("%.2f taka\n", b*88.45);

            break;

      case 2:

            printf("enter value: ");

            scanf("%f", &b);

            printf("%.2f dollar\n", b/88.45);

            break;

      }

}

 


How to Implement Leap Year C Program

#include <stdio.h>

int main(){

   int year;

   printf("Enter the year  : ");

   scanf("%d",&year);

   if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))

      printf("%d is a leap year", year);

   else

     printf("%d is not a leap year",year);

   return 0;

}

 

 

 

 

 

How to find max number

#include<stdio.h>

int main()

{

    int m,n,p;

    printf("Please enter the int number : ");

    scanf("%d %d %d",&m, &n, &p);

 

    if (m>n)

        if(m>p)

        {

            printf("%d number is max number ", m);

        }

        else

        {

            printf("%d number is max number ", p);

        }

    else

       if(n>p)

        {

            printf("%d number is max number ", n);

        }

        else

        {

            printf("%d number is max number ", p);

        }

}

 

How to mark and GPA

#include<stdio.h>

int main()

{

    int number;

    printf("Please enter the marks : ");

    scanf("%d", &number);

 

    if(number>=80)

        {

            printf("A+");

        }

    else if((number>=60) && (number<80))

        {

            printf("B");

        }

    else if((number>=40) && (number<59))

        {

            printf("B");

        }

 

    else

    {

        printf("F");

    }

 

}

 

Write a program in c that print 1,2,3,4

#include<stdio.h>

int main()

{

    int i;

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

    {

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

    }

}

 

Write a program in c odd number 20

#include<stdio.h>

int main()

{

    int i;

    for (i=1; i<=20; i=i+2)

    {

        printf("%d is odd number\n",i);

    }

}

 

Serias: 1-2+4-8+16…..+128

 

#include<stdio.h>

int main()

{

    int i, sum =0, sign = 1;

    for (i=1; i<=128; i=i*2)

    {

        sum=sum+i*sign;

        sign=sign*-1;

    }

    printf("Sum =%d",sum);

}

 

 

 

 

 

 

Write a program in c 1x2x3x….x10?

//1x2x3....x10?

#include<stdio.h>

int main()

{

 

    int i, multi =1,n;

    printf("Please enter the int number : ");

    scanf("%d",&n);

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

    {

        multi=multi*i;

    }

    printf("Multipication  =%d",multi);

}

 

Write a program in c 1+2+3+….+10

#include<stdio.h>

int main()

{

int i,sum=0;

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

{

    sum =sum+i;

}

printf("Sum = %d",sum);

 

}

 

C program to calculate tax, given the following conditions

In this tutorial, we can learn C program to calculate tax. In this c program, we enter a number and calculate tax with the given following conditions.

1. if income is less thn, 1,50,000 then no tax
2. if taxable income is in the range 1,50,001-300,000 then charge 10% tax
3. if taxable income is in the range 3,00,001-500,000 then charge 20% tax
4. if taxable income is above 5,00,001 then charge 30% tax.

 

#include <Stdio.h>

 #include <conio.h>

 #define MIN_INCOME1 150001

 #define MAX_INCOME1 300000

 #define TAX_RATE1 0.10

 #define MIN_INCOME2 300001

 #define MAX_INCOME2 500000

 #define TAX_RATE2 0.20

 #define MIN_INCOME3 500001

 #define TAX_RATE3 0.30

 main()  {

    double income, taxable_income,tax;

    clrscr ();

    printf("\n Enter the income: ");

    scanf ("%lf", &income) ;

    taxable_income = income - 150000;

    if(taxable_income <= 0)  {

       printf ("\n NO TAX");

   }

   else if(taxable_income >= MIN_INCOME1 && taxable_income < MAX_INCOME1)  {

       tax = (taxable_income - MIN_INCOME1) *TAX_RATE1;

   }

  else if(taxable_income >= MIN_INCOME2 && taxable_income < MAX_INCOME2) {

    tax = (taxable_income - MIN_INCOME2) * TAX_RATE2;

  }

 else  {

    tax = (taxable_income - MIN_INCOME3) * TAX_RATE3;

    printf("\n TAX= %lf", tax);

  }

  getch();

  return 0;

 }

 

 

 

 

 

 

//1+2+3+....+n = sum

//1+3+5+....+n=sum

//2+4+6+....+n=sum

 

#include<stdio.h>

int main()

{

    int n,sum=0,i;

    printf("Please enter the last number of this serias : ");

    scanf("%d",&n);

    printf("2+4+6+.........+%d",n);

    for (i=2; i<=n; i=i+2)  //i=i+2 //i=i+1

    {

        sum=sum+i;

    }

    printf(" = %d",sum);

 

    getch();

}

//1+2+3+....+n = sum

//1+3+5+....+n=sum

//2+4+6+....+n=sum

#include<stdio.h>

int main()

{

    int n, sum=0, a=1;

    printf("Please enter the serais number : ");

    scanf("%d",&n);

    printf("1+3+5+.............+%d",n);

 

    while(a<=n)

    {

        sum=sum+a;  //sum+a

        a=a+2;    //a=a+1//a=a+2

    }

    printf(" = %d",sum);

    getch();

}

 

 

//1*1+2*2+3*3+..............+n1*n2 = sum

//1*1*1+2*2*2+3*3*3+..............+n1*n2*n3 = sum

 

#include<stdio.h>

int main()

{

    int n1,n2,sum=0,a=1,b=1;

    printf("Please enter the serias number n1 and n2 : ");

    scanf("%d %d",&n1, &n2);

 

    printf("1*1 + 2*2 + 3*3 +..............+ %d*%d", n1, n2);

    while(a<=n1 && b<=n2)   //a<=n1 && b<=n2 && c<=n3

    {

        sum =sum+a*b;

        a=a+1;

        b=b+1;

    }

    printf(" = %d",sum);

 

    getch();

 

}

 

 

 

 

 

//1...........n

 

#include<stdio.h>

int main()

{

    int n,i;

    printf("Please enter the numbers : ");

    scanf("%d",&n);

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

    {

        printf("%d\n",i);

    }

    getch();

}

 

//1...........n

//odd number

//even number  

#include<stdio.h>

int main()

{

    int n,i,sum=0;

    printf("Please enter the numbers : ");

    scanf("%d",&n);

    for(i=1;i<=n;i=i+2)

    {

        printf("%d\n",i);

        sum=sum+i;

    }

    printf("Sum of Number %d",sum);

    getch();

}

 

//1.5+2.5+3.5+............+n =sum

#include<stdio.h>

int main()

{

    float n, sum=0, i;

    printf("Please enter the number : ");

    scanf("%f",&n);

    printf("1.5+2.5+3.5+............+%.1f",n);

 

    for (i=1.5;i<=n; i++)

    {

       // printf("%.1f",i);

        sum = sum+i;

    }

    printf(" = %.1f",sum);

    getch();

}

 

//1-2+3-4+5-6 .............+n sum

#include<stdio.h>

int main()

{

    int n,i,even=0, odd=0;

    printf("Please enter the last number : ");

    scanf("%d",&n);

 

    printf("1-2+3-4+5-6+.........+%d",n);

 

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

    {

        if (i%2==0)

            even = even+i;

        else

            odd = odd+i;

    }

    printf(" = %d",odd-even);

 

    //getch();

}

 

//1^2 + 2^2 +....+ n^2

#include<stdio.h>

int main()

{

    int n,i, sum=0;

    printf("Enter the n value : ");

    scanf("%d",&n);

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

    {

        sum= sum+i*i;

 

    }

    printf("sum of %d",sum);

    getch();

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Sir Lecture

 

(1)Write a program in C to find out the root of a quadratic equation?

(2)Write a program in C to calculate factorial number.

(3)Write a program in order to calculate the fabric consumption to produce a standard shirt.

(4)Write a program to calculate ten Fibonacci numbers.

(5)Write a program to calculate the sum of series. ( odd number and even number)

(6)Write a program to sort the numbers of an array elements in ascending order using C language.

(7)Write a program to sort the numbers of an array elements in descending order using C language.

(8)Write a program to show the triangle of asterisks.

(9)Write a program to convert a given number of days into months and days.

 

 

1.

/* C Program to find roots of a quadratic equation when coefficients are entered by user. */
/* Library function sqrt() computes the square root. */
 
#include<stdio.h>
#include<conio.h>
#include<math.h> /* This is needed to use sqrt() function.*/
void main()
{
  float a, b, c, determinant, r1,r2, real, imag;
  printf("Enter coefficients a, b and c: ");
  scanf("%f%f%f",&a,&b,&c);
  determinant=b*b-4*a*c;
  if (determinant>0)
  {
      r1= (-b+sqrt(determinant))/(2*a);
      r2= (-b-sqrt(determinant))/(2*a);
      printf("Roots are: %.2f and %.2f",r1 , r2);
  }
  else if (determinant==0)
  {
    r1 = r2 = -b/(2*a);
    printf("Roots are: %.2f and %.2f", r1, r2);
  }
  else
  {
    real= -b/(2*a);
    imag = sqrt(-determinant)/(2*a);
    printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
  }
}
output
Enter coefficients a, b and c: 2.3
4
5.6
Roots are: -0.87+1.30i and -0.87-1.30i

 

 

 

 

 

 

 

 

 

 

2./* C program to display factorial of an integer if user enters non-negative integer. */

 

#include <stdio.h>

#include<conio.h>

void main()

{

    int n, count;

    int factorial=1;        

    printf("Enter an integer: ");

    scanf("%d",&n);

    if ( n< 0)

        printf("Error!!! Factorial of negative number doesn't exist.");

    else

    {

       for(count=1;count<=n;++count)    /* for loop terminates if count>n */

       {

          factorial*=count;              /* factorial=factorial*count */

       }

    printf("Factorial = %d",factorial);

    }

}

 

Output 2

Enter an integer: 10
Factorial = 3628800
 
 
4./* Displaying Fibonacci sequence up to nth term where n is entered by user.*/
 
#include<stdio.h>
#include<conio.h>
void main()
{
  int count, n, t1=0, t2=1, display=0;
  printf("Enter number of terms: ");
  scanf("%d",&n);
  printf("Fibonacci Series: %d+%d+", t1, t2); /*Displaying first two terms */
  count=2;    /* count=2 because first two terms are already displayed. */
  while (count<n)  
  {
      display=t1+t2;
      t1=t2;
      t2=display;
      ++count;
      printf("%d+",display);
  }
}

Output:

Enter number of terms: 10
Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+
 

5 Write a program to calculate the sum of series. ( odd number and even number)

 

#include<stdio.h>

int main ()

{

    int n,i,odd=0, even=0;

    printf("Enter the number : ");

    scanf("%d",&n);

 

    printf("1+2+3+...........+%d",n);

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

    {

        if (i%2==0)

            even = even+i;

        else

            odd = odd+i;

    }

    printf(" = sum of even = %d, sum of odd = %d, Sum %d",even,odd, even-odd);

    return 0;

}

 
6. 
/* C program to accept N numbers and arrange them in an ascending order */
#include <stdio.h>
 
void main()
{
    int i, j, a, n, number[30];
 
    printf("Enter the value of N \n");
    scanf("%d", &n);
    printf("Enter the numbers \n");
    for (i = 0; i < n; ++i)
        scanf("%d", &number[i]);
    for (i = 0; i < n; ++i)
    {
        for (j = i + 1; j < n; ++j)
        {
            if (number[i] > number[j])
            {
                a =  number[i];
                number[i] = number[j];
                number[j] = a;
            }
        }
    }
  printf("The numbers arranged in ascending order are given below \n");
    for (i = 0; i < n; ++i)
        printf("%d\n", number[i]);
}
 
7. 

/* Write a C program to arrange the given numbers in descending order */

void main ()

{
  int i,j,a,n,number[30];
  printf ("Enter the value of N\n");
  scanf ("%d", &n);
  printf ("Enter the numbers \n");
  for (i=0; i<n; ++i)
  scanf ("%d",&number[i]);
  for (i=0; i<n; ++i)
  {
    for (j=i+1; j<n; ++j)
      { if (number[i] < number[j])
  { a        = number[i];
   number[i] = number[j];
   number[j] = a;
  }
      }
  }
  printf ("The numbers arrenged in ascending order are given below\n");
  for (i=0; i<n; ++i)
  printf ("%10d\n",number[i]);
  }

 
 
/* C program to check whether a number is prime or not. */
 
If else
 

#include<stdio.h>

int main()

{

    int n;

    printf("Enter the number :   ");

    scanf("%d",&n);

 

    if (n%2==0)

        printf("%d is a prime number",n);

    else

        printf("%d is not a prime number",n);

 

    return 0;

}

 

 

 

 

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

 

Enter a positive integer: 29
29 is a prime number.

 

 
/* C program to check whether a number entered by user is Armstrong or not. 

A positive integer is called an Armstrong number if the sum of cubes of individual digit is equal to that number itself. For example:

153 = 1*1*1 + 5*5*5 + 3*3*3  // 153 is an Armstrong number.
12 is not equal to 1*1*1+2*2*2  // 12 is not an Armstrong number.
 
*/
 
#include <stdio.h>
int main()
{
  int n, n1, rem, num=0;
  printf("Enter a positive  integer: ");
  scanf("%d", &n);
  n1=n;
  while(n1!=0)
  {
      rem=n1%10;
      num+=rem*rem*rem;
      n1/=10;
  }
  if(num==n)
    printf("%d is an Armstrong number.",n);
  else
    printf("%d is not an Armstrong number.",n);
}

 

 

 

 

9 Write a program to show the triangle of asterisks.

#include <stdio.h>

void main()

{

   int i,j,rows;

   printf("Input number of rows : ");

   scanf("%d",&rows);

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

   {

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

                 printf("*");

              printf("\n");

   }

}

 

 

Source Code to Display Largest Element of an array

 

#include <stdio.h>

int main(){

    int i,n;

    float arr[100];

    printf("Enter total number of elements(1 to 100): ");

    scanf("%d",&n);

    printf("\n");

    for(i=0;i<n;++i)  /* Stores number entered by user. */

    {

       printf("Enter Number %d: ",i+1);

       scanf("%f",&arr[i]);

    }

    for(i=1;i<n;++i)  /* Loop to store largest number to arr[0] */

    {

       if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/

           arr[0]=arr[i];

    }

    printf("Largest element = %.2f",arr[0]);

    return 0;

}

Output:

Enter total number of elements (1 to 100): 8
 
Enter Number 1: 23.4
Enter Number 2: -34.5
Enter Number 3: 50
Enter Number 4: 33.5
Enter Number 5: 55.5
Enter Number 6: 43.7
Enter Number 7: 5.7
Enter Number 8: -66.5

 

 

/* C Program to find largest number using nested if...else statement */
 
#include <stdio.h>
int main(){
      float a, b, c;
      printf("Enter three numbers: ");
      scanf("%f %f %f", &a, &b, &c);
      if(a>=b && a>=c)
         printf("Largest number = %.2f", a);
      else if(b>=a && b>=c)
         printf("Largest number = %.2f", b);
      else
         printf("Largest number = %.2f", c);
      return 0;
}

 

Output

 
Enter three numbers: 12.2
13.452
10.193
Largest number = 13.45

 

/* Write a program to convert a given number of days into months and days. */

 

#include<stdio.h>

int main()

{

    int month, day;

    printf("Enter the days : ");

    scanf("%d",&day);

 

    month=day/30;

    day=day%30;

    printf("%d Month and %d days " , month,day);

    return 0;

}

 

OR

#include<stdio.h>

int main()

{

   int days, years, weeks, months;

   printf("Enter Days:");

   scanf("%d",&days);

   years=days/365;

   weeks=days/7;

   months=days/30;

   printf("Days to years %d",years);

   printf("\nDays to weeks %d",weeks);

   printf("\nDays to months %d",months);

}

 

 

Pattern

#include<stdio.h>

void main()

{

    int n, row, col;

    printf("Enter N : ");

    scanf("%d",&n);

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

    {

        for (col=1;col<=row; col++)

        {

            printf("%d",col); // "* "//%c col+64 (capital)//%c col+96(small) //col%2 //row%2 //col

        }

        printf("\n");

    }

}

 

 

 

#include<stdio.h>

void main()

{

    int n, row, col;

    printf("Enter N : ");

    scanf("%d",&n);

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

    {

        for (col=1;col<=row; col++)

        {

            printf("%d",row%2);

        }

        printf("\n");

    }

}

 

 

 

#include<stdio.h>

void main()

{

    int n, row, col;

    printf("Enter N : ");

    scanf("%d",&n);

    for (row=n;row>=1;row--)  //change it

    {

        for (col=1;col<=row; col++)

        {

            printf("* "); // "* "//%c col+64 (capital)//%c col+96(small) //col%2 //row%2 //col

        }

        printf("\n");

    }

}

 

 

 

#include<stdio.h>

void main()

{

    int n, row, col;

    printf("Enter N : ");

    scanf("%d",&n);

    for (row=1;row<=n;row++)  //change it

    {

        for (col=1;col<=row; col++)

        {

            printf("* "); // "* "//%c col+64 (capital)//%c col+96(small) //col%2 //row%2 //col

        }

        printf("\n");

    }

 

       for (row=n;row>=1;row--)  //change it

    {

        for (col=1;col<=row; col++)

        {

            printf("* "); // "* "//%c col+64 (capital)//%c col+96(small) //col%2 //row%2 //col

        }

        printf("\n");

    }

}

 

 

#include<stdio.h>

void main()

{

    int n, row, col;

    printf("Enter N : ");

    scanf("%d",&n);

    for (row=1;row<=n;row++)  //change it

    {

        for (col=1;col<=row; col++)

        {

            printf("* "); // "* "//%c col+64 (capital)//%c col+96(small) //col%2 //row%2 //col

        }

        printf("\n");

    }

 

       for (row=n-1;row>=1;row--)  //change it

    {

        for (col=1;col<=row; col++)

        {

            printf("* "); // "* "//%c col+64 (capital)//%c col+96(small) //col%2 //row%2 //col

        }

        printf("\n");

    }

}

 

 

 

#include<stdio.h>

void main()

{

    int n, row, col;

    printf("Enter N : ");

    scanf("%d",&n);

    for (row=1;row<=n;row++)  //change it

{

    //printspace

    for (col=1; col<=n-row;col++)

    {

        printf(" ");

    }

    // printing number

 

        for(col=1;col<=row; col++)

        {

            printf("*");//"%d",col

        }

        printf("\n");

 

}

}

 

 

#include<stdio.h>

void main()

{

    int n, row, col;

    printf("Enter N : ");

    scanf("%d",&n);

    for (row=1;row<=n;row++)  //change it

{

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

    {

        printf("*");

    }

        printf("\n");

 

}

}

 

 

 

#include<stdio.h>

void main()

{

    int n, row, col;

    printf("Enter N : ");

    scanf("%d",&n);

    for (row=1;row<=n;row++)  //change it

{

    //printspace

    for (col=1; col<=n-row;col++)

    {

        printf(" ");

    }

 

    for (col=1; col<=2*row-1;col++)

    {

        printf("*");

    }

        printf("\n");

 

}

}

 

 

#include<stdio.h>

void main()

{

    int n, row, col;

    printf("Enter N : ");

    scanf("%d",&n);

    for (row=1;row<=n;row++)  //change it

{

    //printspace

    for (col=1; col<=n-row;col++)

    {

        printf(" ");

    }

 

    for (col=1; col<=2*row-1;col++)

    {

        printf("*");

    }

        printf("\n");

 

}

 

    for (row=n;row>=1;row--)  //change it

{

    //printspace

    for (col=1; col<=n-row;col++)

    {

        printf(" ");

    }

 

    for (col=1; col<=2*row-1;col++)

    {

        printf("*");

    }

        printf("\n");

 

}

}

 

No comments:

Post a Comment

To generate a PDF using JavaScript in Oracle APEX from a collection

  To generate a PDF using JavaScript in Oracle APEX from a collection, you can follow these steps: 1. Create a button or link on your APEX p...