Sunday, November 29, 2020

Write a C program that reads two integers p and q, print p number of lines in a sequence of 1 to q in a line.

 /*Write a C program that reads 

two integers p and q, print 

p number of lines in a 

sequence of 1 to q in a line. 

Test Data :

Input number of lines: 5

Number of characters in a 

line: 6

Expected Output:

1 2 3 4 5 6

7 8 9 10 11 12

13 14 15 16 17 18

19 20 21 22 23 24

25 26 27 28 29 30

*/

#include <stdio.h>

int main()

{

  int num1,num2,i,j,k=1;

  printf("Input the number\

 number of lines: ");

  scanf("%d",&num1);

  printf("Number of character\

 in a line: ");

  scanf("%d",&num2);

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

  {

    for(j=0;j<num2;j++)

    {

      printf("%d ",k++);

    }

    j=0;

    printf("\n");

  }

  return 0;

}

Saturday, November 28, 2020

Write a C program to print 3 numbers in a line, starting from 1 and print n lines. Accept number of lines (n, integer) from the user.

/*Write a C program to print 

3 numbers in a line, starting
from 1 and print n lines.
Accept number of lines (n,
integer) from the user.
Test Data :
Input number of lines: 5
Expected Output:
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
*/
#include <stdio.h>
int main()
{
  int i,num,x=1,j;
  printf("Input number of \
line: ");
  scanf("%d",&num);
  for(i=1;i<=num;i++)
  {
    for(j=0;j<3;j++)
    {
      printf("%d \t",x++);
    }
    j=0;
    printf("\n");
  }
  return 0;
}

Write a C program to find all numbers which are dividing by 7 and the remainder is equal to 2 or 3 between two given integer numbers.

 /*Write a C program to find all 

numbers which are dividing by 

7 and the remainder is equal 

to 2 or 3 between two given 

integer numbers.

Test Data :

Input the first integer: 25

Input the second integer: 45

Expected Output:

30

31

37

38

44

*/

#include <stdio.h>

int main()

{

  int num1,num2,sum=0,i;

  printf("Input the first\

 number: ");

  scanf("%d",&num1);

  printf("Input the second\

 number: ");

  scanf("%d",&num2);

  if(num1>num2)

  {

    int temp=num1;

    num1=num2;

    num2=temp;

  }

  for(i=num1;i<num2;i++)

  {

    if(((i%7)==2)||((i%7)==3))

    {

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

    }

  } 

  return 0;

}

Thursday, November 26, 2020

Write a C program to read the coordinates(x, y) (in Cartesian system) and find the quadrant to which it belongs (Quadrant -I,Quadrant -II, Quadrant -III, Quadrant -IV). Note: A Cartesian coordinate system is a coordinate system that specifies each point uniquely in a plane by a pair of numerical coordinates. These are often numbered from 1st to 4th and denoted by Roman numerals: I (where the signs of the (x,y) coordinates are I(+,+), II (−,+), III (−,−), and IV (+,−).

 Write a C program to read 

the coordinates(x, y) (in 

Cartesian system) and find 

the quadrant to which it 

belongs (Quadrant -I,Quadrant 

-II, Quadrant -III, Quadrant 

-IV). 

Note: A Cartesian coordinate 

system is a coordinate system 

that specifies each point 

uniquely in a plane by a 

pair of numerical coordinates.

These are often numbered 

from 1st to 4th and denoted 

by Roman numerals: I (where 

the signs of the (x,y) 

coordinates are I(+,+), 

II (−,+), III (−,−), and 

IV (+,−).

Test Data :

Input the Coordinate(x,y):

x: 25

y: 15

Expected Output:

Quadrant-I(+,+)

*/

#include <stdio.h>

int main()

{

  int x,y,z=0;

  while(z=1)

  {

    printf("Input the Coord\

inate(x,y):\n");

    printf("x: ");

    scanf("%d",&x);

    printf("y: ");

    scanf("%d",&y);

    if(x>=0 && y>=0)

    {

      printf("quadrant -I(+,+)\n");

    }

    else if(x<=0 && y>=0)

    {

      printf("quadrant -II(-,+)\n");

    }

    else if(x<=0 && y<=0)

    {

      printf("quadrant -III(-,-)\n");

    }

    else if(x>=0 && y<=0)

    {

      printf("quadrant -IV(+,-)\n");

    }

    break;

  }

  return 0;

}

Write a C program to check whether two numbers in a pair is in ascending order or descending order.

 /*Write a C program to check 

whether two numbers in a pair 

is in ascending order or 

descending order.

Test Data :

Input a pair of numbers (for 

example 10,2 : 2,10):

Input first number of the 

pair: 10

Expected Output:

Input second number of the 

pair: 2

The pair is in descending 

order!

*/

#include <stdio.h>

int main()

{

  int num1,num2,i;

  printf("Input a pair of num\

bers\n(for example 10,2 : 2,\

10): \n");

  printf("Input first number\

 of the pair: ");

  scanf("%d",&num1);

  printf("Input second number\

 of the pair: ");

  scanf("%d",&num2);

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

  {

    if(num1>num2)

    {

      printf("The pair is in\

 descending order!\n"); 

    }

    else

    {

      printf("The pair is in\

 ascending order!\n");

      break;

    }

  }

  return 0;

}

Wednesday, November 25, 2020

Write a C program to print the numbers from the lowest to the highest (inclusive) and the sum of consecutive integers from a given pair of numbers.

 /*Write a C program to print 

the numbers from the lowest 

to the highest (inclusive) 

and the sum of consecutive 

integers from a given pair 

of numbers.

Test Data :

Input a pair of numbers (for 

example 10,2):

Input first number of the 

pair: 10

Input second number of the 

pair: 2

Expected Output:

List of odd numbers: 3

5

7

9

Sum=24

*/

#include <stdio.h>

int main()

{

  int num1,num2,i,sum=0;

  printf("Input a pair of\

 \nnumber (for example 10,2):\

  \n");

  

  printf("Input first number\

 of the pair: ");

  scanf("%d",&num1);

  printf("Input second number\

 of the pair: ");

  scanf("%d",&num2);

  if(num1<num2)

  {

    return printf("Please try\

 again.\n");

  }

  printf("List of odd number:\

   \n");

  for(i=num2;i<num1;i++)

  {

    if(i%2!=0)

    {

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

      sum=sum+i;

    }

  }

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

  return 0;

}

Tuesday, November 24, 2020

Write a C program to check whether a given integer is positive even, negative even, positive odd or negative odd. Print even if the number is 0.

 /*Write a C program to check 

whether a given integer is 

positive even, negative even, 

positive odd or negative odd. 

Print even if the number is 0. 

Test Data :

Input an integer: 13

Expected Output:

Positive Odd

*/

#include <stdio.h>

int main()

{

  int num,pos=0,neg=0;

  printf("Input an integer:\

  ");

  scanf("%d",&num);

  if(num==0)

  {

    printf("Positive.\n");

  }

  else if(num%2==0)

  {

    if(num>0)

    {

      pos++;

      printf("Positive even\n");

    }

    else

    {

      neg++;

      printf("Negative even\n");

    }     

  }

  else if(num%2!=0)

  {

    if(num>0)

    {

      pos++;

      printf("Positive odd\n");

    }

    else

    {

      neg++;

      printf("Negative odd\n");

    }

  }

  return 0;  

}


Monday, November 23, 2020

Write a C program to find and print the square of each one of the even values from 1 to a specified value.

 

/*Write a C program to find
and print the square of
each one of the even values
from 1 to a specified value.
Test Data :
List of square of each one
of the even values from 1
to a 4 :
Expected Output:
2^2 = 4
4^2 = 16
*/
#include <stdio.h>
int main()
{
  int i, result=1;
  printf("List of square of\
\nof the even values from\
  1 to a 4: ");
  for(i=1;i<5;i++)
  {
    if(i%2==0)
    {
        result=i*i;
        printf("\n\n%d^%d=%d"
         ,i,i,result);
    }
  }
  getchar();
}

Write a C program that read 5 numbers and counts the number of positive numbers and print the average of all positive values.

 /*Write a C program that read 

5 numbers and counts the 

number of positive numbers 

and print the average of all 

positive values. 

Test Data :

Input the first number: 5

Input the second number: 8

Input the third number: 10

Input the fourth number: -5

Input the fifth number: 25

Expected Output:

Number of positive numbers: 4

Average value of the said 

positive numbers: 12.00

*/

#include <stdio.h>

int main()

{

  int num[5],i,sum=0,

   positive=0;

  printf("Input the first\

 number: ");

  scanf("%d",&num[0]);

  printf("Input the second\

 number: ");

  scanf("%d",&num[1]);

  printf("Input the third\

 number: ");

  scanf("%d",&num[2]);

  printf("Input the fourth\

 number: ");

  scanf("%d",&num[3]);

  printf("Input the fifth\

 number: ");

  scanf("%d",&num[4]);

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

  {

    if(num[i]>=0)

    {

      positive++;

      sum=sum+num[i];

    }

    

  }

  printf("Number of positive\

 numbers: %d\n", positive);

 

  printf("Average value of\

 \nthe said positive numbers:\

  %.1f",(float)sum/4);

  getchar();

}

Write a C program to convert a given integer (in days) to years, months and days, assumes that all months have 30 days and all years have 365 days.

 /*Write a C program to convert

 a given integer (in days) to

  years, months and days,

   assumes that all months 

   have 30 days and all years

    have 365 days. 

Test Data :

Input no. of days: 2535

Expected Output:

6 Year(s)

11 Month(s)

15 Day(s)

*/

#include <stdio.h>

int main()

{

  int day,year, month,days,

  year1=365, month1=30;

  printf("Input no. of days:\

 ");

  scanf("%d",&day);

  

  year=day/year1;

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

  

  day=day-(year*year1);

  month=day/month1;

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

  

  day=day-(month*month1);

  days=day;

  printf("%d Day(s)",days);

  getchar();

}

Sunday, November 22, 2020

C Programming Basic

23. https://shakouathossen.blogspot.com/2020/11/24-write-c-program-that-reads-three.html

21. https://shakouathossen.blogspot.com/2020/11/write-c-program-that-reads-integer-and.html

20. https://shakouathossen.blogspot.com/2020/11/20-write-c-program-to-print-roots-of.html

Write a C program that reads three floating values and check if it is possible to make a triangle with them. Also calculate the perimeter of the triangle if the said values are valid.

 

/*Write a C program that
reads three floating values
and check if it is possible
to make a triangle with them.
Also calculate the perimeter
of the triangle if the said
values are valid.
Test Data :
Input the first number: 25
Input the second number: 15
Input the third number: 35
Expected Output:
Perimeter = 75.0
*/
#include <stdio.h>
int main()
{
  float num1,num2,num3,
  parameter,i;
  printf("Input the first\
number: ");
  scanf("%f",&num1);
  printf("Input the second\
number: ");
  scanf("%f",&num2);
  printf("Input the third\
number: ");
  scanf("%f",&num3);
  if(num1+num2>num3)
  {
    if(num2+num3>num2)
    {
      if(num1+num3>num1)
      {
        i=5;
      }
    }
  }
  if(i==5)
  {
    parameter=num1+num2+num3;
    printf("Parameter = %.1f",
  parameter);
  }
  else
  {
    printf("Triangle is not\
valid.");
  }
  getchar();
}

Write a C program to print the roots of Bhaskara’s formula from the given three floating numbers. Display a message if it is not possible to find the roots.

 

/*Write a C program to print
the roots of Bhaskara’s
formula from the given three
floating numbers. Display a
message if it is not possible
to find the roots.
Test Data :
Input the first number(a): 25
Input the second number(b): 35
Input the third number(c): 12
Expected Output:
Root1 = -0.60000
Root2 = -0.80000
*/
#include <stdio.h>
#include <math.h>
int main()
{
  float a,b,c,r1,r2,d;
  printf("Input the first num\
ber: ");
  scanf("%f",&a);
  printf("Input the second\
number: ");
  scanf("%f",&b);
  printf("Input the third\
number: ");
  scanf("%f",&c);
 
  d=sqrt((b*b)-(4*a*c));
 
  r1=(-b+d)/(2*a);
  r2=(-b-d)/(2*a);
 
  printf("Root1 = %.5f\n",r1);
  printf("Root2 = %.5f\n",r2);
}

Write a C program that reads an integer and check the specified range where it belongs. Print an error message if the number is negative and greater than 80. Specified Range: [0, 20], [21, 40], [41, 60], [61, 80]

 /*Write a C program that reads

an integer and check the

specified range where it

belongs. Print an error 

message if the number is 

negative and greater than 80.

Specified Range: [0, 20], 

[21, 40], [41, 60], [61, 80]

Test Data :

Input an integer: 15

Expected Output:

Range [0, 20]

*/

#include <stdio.h>

int main()

{

  int num1;

  printf("Input an integer: \

 ");

  scanf("%d",&num1);

  

  if(num1>=0 && num1<=20)

  {

    printf("Range [0, 20]");

  }

  else if(num1>=21 && num1<40)

  {

    printf("Range [21, 40]");

  }

  else if(num1>=41 && num1<60)

  {

    printf("Rnage [41, 60]");

  }

  else if(num1>=61 && num1<=80)

  {

    printf("Range [61, 80]");

  }

  else 

  {

    printf("Outside of Range.");

  }

  getchar();

}