#include<stdio.h> #include<math.h> int main() { int i,j,num,temp,sum=0; printf("Enter number\n"); scanf("%d",&num); while(sum!=1 && sum!=4) { sum=0; while(num>0) { j=num%10; sum+=(j*j); num=num/10; } num=sum; } if(sum==1) printf("Happy Number\n"); else printf("UnHappy Number\n"); }
PROGRAMS IN C
A platform to learn C in a practical way.
Thursday, 11 February 2021
Program to print weather the number is happy or not.
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:
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:
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.
#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)0>
{
printf("Co-ordinates (%d,%d) lies in 2nd quadrant",x,y);
}
if(x<0 p="" y="">0>
{
printf("Co-ordinates (%d,%d) lies in 3rd quadrant",x,y);
}
if(x>0&&y<0 p="">0>
{
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:
- num: num is used to store the number given by the user.
- x: x is used to store num temporarily after finding modulus of num with 10 and dividing num by 10.
- rev: rev is used to store the reversed value of the given number (final output) whose value is initially 0.
- 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
- Firstly we ask the value of num to the user.
- Then, we store the value of num in dummy just to use it at the last while printing the previous value of num.
- Then, if x value is greater than 0 we go through while loop
- In the first line inside while loop x = num % 10, x stores modulus of num after dividing it to 10.
- 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.
- In the third line inside the while loop num = num/10, it divides num/10 and num stores int value of reminder.
- 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.
Subscribe to:
Comments (Atom)
Search This Blog
Labels
About Me
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...









