Saturday, 19 September 2020

Program to check whether the number is divisible by 5 or not.





/*Program to check whether the number is divisible by 5 or not.*/
#include<stdio.h>
int main()
{
int num;
printf("Enter the number you want to check: ");
scanf("%d",&num);
if(num%5==0)
{
    printf("%d is divisible by 5",num);
}
else
{
         printf("%d is not divisible by 5",num);
}
return 0;
}

OUTPUT:


DESCRIPTION:
In this program we asked the user to input a number then we divide it by 5.If the remainder is 0 it display the number is divisible else yhe number is not divisible.

VARIABLES USED
num:-this variable is used to store the number given by the user.

Watch also the python tutorial .




Friday, 18 September 2020

A program that reads time in second and convert it into hours,minutes and seconds




/*A program that reads time in second  and convert it into hours,minutes and seconds*/
#include<stdio.h>
int main()
{
int number_of_sec, hours, minute, sec ;
printf("Enter the number of seconds:");
scanf("%d",&number_of_sec);
hours=number_of_sec/3600;
minute=(number_of_sec%3600)/60;
sec=(number_of_sec%3600)%60;
printf("%d seconds= %d hours,%d minutes,%d second",number_of_sec,hours,minute,sec);
return 0;
}
OUTPUT:

conversion of time into hour,minute,second

DESCRIPTION:
In this program, we asked the user to input the num of second  and we assign the value to the variable .Then we divide the num i.e.user input number and divide it by 3600 to find the number of hours..And similarly we calculate the number of minute and seconds.

VARIABLE USED:

number_of_sec :-used to store the number of seconds which is given by user.
hours-used to store the number of hours calculated.
minute-used to store the number of minute calculated.
sec-used to store the number of second calculated.

CALCULATION PROCESS:

number_of_sec=86399
hours=number_of_sec/3600=23(since both are integer.)
minutes=(number_of_sec%3600)/60=(500%365)/60=59
sec=(number_of_sec%3600)%60=59




Program to convert given number of days into year,week,day.


Conversion of  days into month ,weak,days


#include<stdio.h>
int main()
{
int year,days,week,num;
printf("Enter number of day:");
scanf("%d",&num);
year=num/365;

week=(num%365)/7;
days=(num%365)%7;

printf("%d days=%d year ,%d week,%  days",num,year,week,days);
      return 0;
}

OUTPUT:


DESCRIPTION:
In this program, we asked the user to input the num of days and we assign the value to num variable .Then we divide the num i.e.user input number and divide it by 365 to find the number of year.And similarly we calculate the number of weeks and days.

VARIABLE USED:

num-used to store the number of days which is given by user.
year-used to store the number of years calculated.
week-used to store the number of week used.
days-used to store the number of days used.

CALCULATION PROCESS:

num=500
year=num/365=1(since both are integer.)
week=(num%365)/7=(500%365)/7=19
days=(num%365)%7=2






Monday, 14 September 2020

A program to find the quadrants of a given co-ordinates.


//program to find the quadrants of the coordinates//

#include<stdio.h>

int main()

{

int x,y;

printf("Enter the co-ordinates:");

scanf("%d %d",&x ,&y);

if(x>0&&y>0)

{

printf("Co-ordinates (%d,%d) lies in 1st quadrant",x,y);

}

if(x<0 y="">0)

{

printf("Co-ordinates (%d,%d) lies in 2nd quadrant",x,y);

}

if(x<0 p="" y="">

{

printf("Co-ordinates (%d,%d) lies in 3rd quadrant",x,y);

}

if(x>0&&y<0 p="">

{

printf("Co-ordinates (%d,%d) lies in 4th quadrant",x,y);

}

if(x=0&&y=0)

{

printf("Co-ordinates(%d,%d) lies in origin",x,y);

}

return 0;

}


OUTPUT:


DESCRIPTION:
At first, we ask the user to input the co-ordinates .We check either the co-ordinates are positive or negative and display the quadrants according to that.And the co-ordinates(0,0) are the origin point.

For example:
(-4,-6)
Here both the x and y co-ordinates are negative.So,the output is 3rd quadrant.

Saturday, 12 September 2020

A Program to print given number in reverse order

 


Program to print given number in reverse order

#include<stdio.h>
#include<conio.h>

int main() {
int num, x, rev = 0, dummy;

printf("Enter a number: ");
scanf("%d", &num);
        dummy = num;

while(num>0) {
x = num%10;
rev = rev * 10 + x;
num = num/10;
}
printf("The reverse of %d is %d", dummy, rev);
return 0;
}

Output




Variables Used:

  1. num: num is used to store the number given by the user.
  2. x: x is used to store num temporarily after finding modulus of num with 10 and dividing num by 10.
  3. rev: rev is used to store the reversed value of the given number (final output) whose value is initially 0.
  4. dummy: dummy is used to store the value of num just for showing the value of num in the output screen
            note: num value is changed again and again inside the while block so it doesn't 
                      stores it's actual value that was given by user. (dummy stores it).

Description

  1. Firstly we ask the value of num to the user.
  2. Then, we store the value of num in dummy just to use it at the last while printing the previous value of num.
  3. Then, if x value is greater than 0 we go through while loop
  4. In the first line inside while loop x = num % 10, x stores modulus of num after dividing it to 10.
  5. In the second line inside while loop rev = rev * 10 + x, here rev stores value after multiplying 10 with rev then adding x. likely it stores the final value.
  6. In the third line inside the while loop num = num/10, it divides num/10 and num stores int value of reminder.
  7. Lastly printf prints the final reversed value of given number.

working Module:


if num = 456, the
  • firstly dummy stores the value of num then it goes to while loop and checks condition if num is greater than 0 or not. Here num 456 is greater than 0 so this condition returns true so it goes inside while loop. Compiler checks the first line of the block where the logic is x = num % 10 = 456%10 = 6. It calculates the modulus of num and gets the value as 6 and stores it in x variable then it goes to second line and the logic is rev = rev * 10 + x. In this logic variable x is treated as 6 just because x stores 6. Then it performs the calculation as rev = 0 * 10 + 6 = 6 ( rev is initially 0), now rev stores 6 then it goes to third line and checks the logic num = num/10. This logic is performed as num = 456/10 = 45 . Now num stores 45 (it doesn't store 45.6 because it's data type is int (integer)). Then again it goes to while loop and checks condition (num>0), yes num is 45 now and it is still greater so the condition returns true again. It goes to first line again and performs same task as before. this time x stores x = num % 10 = 45 % 10 = 5. Now compiler goes to second line and now rev stores rev = rev * 10 + x = 6 * 10 + 5 = 65. Then it goes to third line and now num stores num = num / 10 = 45/10 = 4. Now it again checks the condition inside while loop (num>0) yes num = 4 is greater than 0 which is true so it again goes inside while loop and read the first line x = num % 10 = 4%10 = 4. Then it goes to second line rev = rev * 10 + x = 65 * 10 + 4 = 654. Then it compiler checks third line and calculate num as num = num/10 = 4/10 = 0. again it goes to while loop and checks condition (num>0) but this time it returns false because num is smaller than 0. Now finally rev stores 654 which is the reversed value of 456 then we print it using printf function.





Thursday, 10 September 2020

Program to print the fibonacci series.


Program to print Fibonacci Number Series


#include<stdio.h>
int main()
{
int a=0,b=1,c,count,num;
printf("Enter the number upto which you want to obtain the fibonacci series:");
scanf("%d",&num);
printf(" The fibonacci series is :");
printf(" %d %d",a,b);
count=2;
while(count
{
c=a+b;
a=b;
b=c;
printf(" %d",c);
count++;
}
return 0;
}

OUTPUT:



VARIABLE USED:

1. a-It is initially  used to store vale '0' and as loop executes its value is replaced by 'b'

2. b-It is initially used to store value '1' and as loop executes its value ir replaced according to 'c'.

3. c- It is used to store the sum of a and b.

4. count-It is used for storing the number of iteration that the loop has undergone.

5. num-It is used to store the number given by the user.

DESCRIPTION:
In this program, we ask the user to input a number and we print 0 and 1 at first as they are the starting of the fibonacci series.Under the while loop we use the variable 'c' which contains the value of a+b.Finally the loop is executed and the series is displayed as above.



Wednesday, 9 September 2020

Program to check whether the number is armstrong or not.

/*Program to check whether the number is armstrong or not.*/ 
#include <stdio.h>
int main()
{
int sum=0,num,temp,i;
printf("Enter the number to check:");
scanf("%d",&num);
temp=num;
while(num>0)
  {
i=num%10;
sum+=i*i*i;
num=num/10;
  }
if (sum==temp)
printf("%d is an armstrong number as its cube sum equals to %d",temp,sum);
else
printf("%d is not an armstrong number",temp);
return 0;
}

OUTPUT:



DESCRIPTION:


In this variable we ask the user to input a number and we take out the each digit of number and cube it and is stored in a variable. In this way,we finally compare the value of num and sum then we display the result.


USED VARIABLE:

1.sum=it is used to store the value of the sum of cube of each digit.
2.i=it is used to store the last digit of the number.
3.temp=it is used to store the value for future need.
4.num=used to store the user input value.

Making a simple calculator using C


Program to make calculator using C


#include <stdio.h>
int main()
{
char choice;
float num1,num2,result;
int flag=1;
printf("Enter +,-,/,* for knowing the result ");
scanf("%c",&choice);
printf("Enter first number ");
scanf("%f",&num1);
printf("Enter second number ");
scanf("%f",&num2);
switch(choice)
{
case '+':result=num1+num2;
break;
case '-':result=num1-num2;
break;
case '/':
{
if(num2==0)
{
flag=0;

}
else
{
result=num1/num2;

}
break;

}
case '*': result=num1*num2;
break;
default:printf("Error");
break;

}
if(flag==1)
{
printf("%f %c %f = %f",num1,choice,num2,result);

}
else
{
printf("%f %c %f = undefined ",num1,choice,num2);

}

}
Output:
Output of simple addition


Output when num2=0
Description:
In this program,we ask the user to input two numbers along with the operator which they want to do.And using the switch case function, we calculate the value as user wants.



Tuesday, 8 September 2020

Alternative method to find the multiplication table.

/*Program to find the multiplication table*/
#include<stdio.h>

int main()
{
  int a,b, n, end, mul;
  printf("5 * 10, where 5 is prfix and 10 is suffix! ");
  printf("Enter the prefix number upto which  you want table: ");
  scanf("%d", &n);
  printf("Enter the suffix number till where you want table: ");
  scanf("%d", &end);
  printf("The multiplication table of 1 to %d is: ",n);
  for(a = 1; a<=n; a++)
   {
  for(b = 1; b <=end; b++)
       {
mul = a*b;
printf("%d * %d = %d ",a,b,mul);
}
printf("\n\n");
  }
return 0;
}
OUTPUT:



Description:
This program will take two inputs from user .First number will represent the number till where you want multiplication table and second number will multiply first numbers till you given it's value.

Example-If you want to get the table upto 5 at once then just keep the value of prefix 5 and if you need it up to 10 times then keep the value of suffix 10.

Program to find the multiplication table of a number

/*A program to find multiplication table*/
#include <stdio.h>
int main()
{
  int num, result,i;
  printf("Enter the number of which you want to get a multiplication table:");
  scanf("%d",&num);
  for(i=1;i<=10;i++)
  {
  result=num*i;
  printf("%d*%d=%d",num,i,result);
}
}

Output:



Description:
In this program, we ask the user to input a number and using the for loop we initialize the value of i and we calculate the table and display the result.


Try it yourself:
Write a program to check weather the person is eligible to vote or not.

Program to check weather the number is palindrome or not.

/*A program to check weather the number is palindrome or not*/

#include<stdio.h>

int main()
{
    int n,rev=0,x,num;
    printf("Enter the number");
    scanf("%d",&n);
    num=n;
    while(n>0)
    {
x=n%10;
rev=rev*10+x;
n=n/10;
     }
     if(num==rev)
     printf("%d is a palindrome number",num);
     else
printf("%d is not a palindrome number",num);

return 0;
}

OUTPUT:

Sunday, 6 September 2020

Program to check weather the given number is odd or even.

/*Program to check weather the given number is prime or not*/

#include

int main()
{
int i,n,c;
       c=0;
       printf("Enter number:");
       scanf("%d",&n);

        for(i=1;i<=n;i++)
       {
if (n%i==0)
{
c++;
}
        }
        if (c==2)
       {
printf("%d is a prime                             number",n);
         }
        else
        printf("%d is not a prime                    number",n);

return 0;
}

OUTPUT:


Program to find the factorial of a number

/*A program to find the factorial of a given number*/
#include<stdio.h>

int main()
{
int i,a,fact;
fact=1;
printf("Enter the number");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
fact=fact*i;
}
{printf(" %d!=%d",a,fact);
}
return 0;
}

OUTPUT:

Meaning of factorial:
2!=2*1=2
4!=4*3*2*1=24
5!=5*4*3*2*1=120
DESCRIPTION:
Here in this program we ask the user to input a number and by the use of the for loop, we calculate the factorial of the number.


Program to check weather the number is odd or even

 /*Program to check weather the number is odd or even*/


#include <stdio.h>

int main ()

{

int a;

printf("Enter the number:");

scanf("%d", &a);

if (a%2==0)

{

printf("the number is even");

}

else 

{

printf("the number is odd");

}

return 0;

}


OUTPUT:

Enter the number:4
the number is odd

DESCRIPTION:

First of all we ask the user to input the number which should be checked and is divided by 2 and the final result is displayed.

Saturday, 5 September 2020

Program to find the sum of two numbers

  /*program to find the sum of two numbers*/


#include<stdio.h>

int main()

{

float a,b,sum;

printf("Enter two numbers:");

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

sum=a+b;

printf("%.2f",sum);

return 0;

}

OUTPUT:

Enter two numbers: 4.0 123.8
127.80

Description:

Here we ask the user to input two numbers to be added and display the result.

Program to calculate Simple Interest

  /*program to compute the simple interest*/

#include<stdio.h>

int main()

{

float P,T,R,INTEREST;


printf("Enter the principal:");

scanf("%f",&P);

printf("Enter the time:");

scanf("%f",&T);

printf("Enter the rate:");

scanf("%f",&R);

INTEREST=(P*T*R)/100;

printf("The interest amount is %.1f",INTEREST);

return 0;

}

OUTPUT

Enter the principal:1000
Enter the time:2
Enter the rate:2.4
The interest amount is 48.0

Description:

Here we ask the value of paramters to find SI and calculate it using formula.

Program to calculate circumference of the circle

  /*Program to find circumference of the circle*/

#include <stdio.h>

#define PI 3.14

int main()

{

float r,circumference;

printf("Enter the radius of the circle:");

scanf("%f",&r);

circumference=2*PI*r;

printf("The circumference of the circle is %.2f",circumference);

return 0;

}

OUTPUT

Enter the radius of the circle: 2.7

The circumference of the circle is 16.96

Description:

Here we ask the user to give the radius of the circle and the value of PI is defined by define function.

Program to calculate area of the rectangle

  /* Program to calculate area of rectangle*/

#include<stdio.h>

int main()
{
int Area,Length,Breadth;
Length=20;
Breadth=10;
Area=Length*Breadth;
printf("The area is %d", Area);




return 0;
}

Output:

The area is 200

Description:

Here the value of the length and breadth of the rectangle is given directly,so we applied formula and  get the output result.

Program to print Hello World in C

 /* A basic C  program to print"Hello World"*/


#include<stdio.h> 
int main()
{
printf("HELLO WORLD!");
return 0;
}

OUTPUT

Hello World!

Description

Just a simple program which display hello world to the user

Types of error in C

1.Syntax Error

It is the error in programs grammatical rule or in writing format.This type of error is detected by translator during translation so,it is easier to correct.

2.Semantic(Logical error)

It is an error(bug) in the concept or the logic of the program like formula mistakes.

3.Run-time error

It is an error that is generated while the program is being executed.For example: Memory leak.

Advantages and Disadvantages of C

Advantages

-Easier to interact with hardware.
-Easy to learn.
-Compact and efficient.

Dis-advantages

-Doesnot contain runtime checking.
-No strict data type checking.

-As the program extends it is very difficult to fix the bugs. 

What are the features of c?

 1. One of the reasons of C's popularity is its portability.

2.It is faster and more efficient than other languages.
3.It is suitable for the structured programming that means problem can be solved in terms of modules or blocks.
4.C is rich in its library function.

What is C?

 C is a middle level programming language developed and designed by Dennis Ritchie at AT & T's Bell Laboratories of USA in 1970s. It is reliable,simple and easy to use.

Search This Blog

Program to print weather the number is happy or not.

#include <stdio.h> #include <math.h> int main () { int i , j , num , temp , sum = 0 ; printf ( "Enter number\n...