Skip to main content

Posts

Showing posts from December, 2014

Convert numbers to roman numerals

Convert numbers to roman numerals       #include <stdio.h> #include <conio.h> void predigits (char c1,char c2); void postdigits (char c,int n); char roman_Number[1000]; int i=0; int main() {     int j;     long int number;        printf( "Enter any natural number: " );     scanf( "%d" ,&number);        if (number <= 0) {          printf("Invalid number");          return 0;     }     while (number != 0){          if (number >= 1000){              postdigits('M',number/1000);              number = number - (number/1000) * 1000;          } ...

1st Day of given year program

  Gregorian Calendar Program According to the Gregorian calendar,it was Monday on the date 01/01/01. If any year is input through the keyboard  write a program to find out what is the day on 1st January of this year. #include <stdio.h> void main()   { int year, x; printf( "Enter the year\n" ); scanf( "%d" , &year); year = year - 1; x = year + year/4 - year/100 + year/400; if (x % 7 == 0) printf( "Monday" ); else if (x % 7 == 1) printf( "Tuesday" ); else if (x % 7 == 2) printf( "Wednesday" ); else if (x % 7 == 3) printf( "Thrusday" );   else if (x % 7 == 4) printf( "Friday" ); else if (x % 7 == 5) printf( "Saturday" ); else if (x % 7 == 6) printf( "Sunday" ); getch(); }

Sum of 5 digits using recursion

Sum of 5 digits using recursion A 5-digit positive integer is entered through the keyboard, write a function to calculate sum of digits of the 5-digit number Using recursion #include <stdio.h> void sum(int m) void main() { int x,s; printf( "\nInput a 5 digit number" ); scanf( "%d" ,&x); s=sum(x); printf( "\nThe sum of the digits is %d" ,s); } void sum(int m) { if (m==0) return 0; else return(m%10+ sum(m/10) ); }  

Sum, Average and Standard deviation in c

 Sum, Average and Standard deviation Write a function that receives 5 integers and returns the sum, average and standard deviation of these numbers. Call this function from main( ) and print the results in main( ). #include <stdio.h> void stat(int a,int b,int c,int d,int e,int *su,float *av,float *std) void main() { clrscr(); float ave,sd; int a,b,c,d,e,sum; printf( "\nInput 5 integers\n" ); scanf( "\n%d\n%d\n%d\n%d\n%d" ,&a,&b,&c,&d,&e);   stat(a,b,c,d,e,&sum,&ave,&sd); printf( "\nThe sum is %d\nThe average is %f\nThe standard deviation is %f" ,sum,ave,sd); } void stat(int a,int b,int c,int d,int e,int *su,float *av,float *std) { * su=a+b+c+d+e; * av= * su/5; * std=sqrt(((((a-*av)*(a-*av))+((b-*av)*(b-*av))+((c-*av)*(c-*av)))/5.0); }

Fibonacci series using and without recursion

c code to print Fibonacci series without recursion #include <stdio.h> void Fibonacci( int ); int main() {     int k,n;     long int i= 0 ,j= 1 ,f;     printf( "Enter the range of the Fibonacci series" );     scanf( "%d %d " ,&n);     printf( "Fibonacci Series:" );     printf( "%d " , 0 );     Fibonacci(n) ;     return 0; }   void Fibonacci ( int n) {      long int first= 0 ,second= 1 ,sum;     while ( n>0 )    {          sum = first + second;          first = second;          second = sum;      ...

Fibonacci series in c

Fibonacci series in c Fibonacci series in c programming: c program for Fibonacci series without and with recursion. Using the code below you can print as many numbers of terms of series as desired. Numbers of Fibonacci sequence are known as Fibonacci numbers. First few numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two terms in sequence every other term is the sum of two previous terms, For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics and Computer Science. Fibonacci series in c using for loop /* Fibonacci Series c language */   #include <stdio.h>   int main ( ) { int n , first = 0 , second = 1 , next , c ;   printf ( "Enter the number of terms \n " ) ; scanf ( "%d" ,& n ) ;   printf ( "First %d ...

To Create Triangle and Structure

 To Create Triangle and Structure   C Program To display the triangle using *, numbers and character Write a C Program to print half pyramid as using * as shown in figure below. * * * * * * * * * * * * * * * #include <stdio.h> int main () { int i , j , rows ; printf ( "Enter the number of rows: " ); scanf ( "%d" ,& rows ); for ( i = 1 ; i <= rows ; ++ i ) { for ( j = 1 ; j <= i ; ++ j ) { printf ( "* " ); } printf ( "\n" ); } return 0 ; }     Write a C Program to print half pyramid as using numbers as shown in figure below. 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5   #include <stdio.h> int main () { int i , j , rows ; printf ( "Enter the number of rows: " ); scanf ( "%d" ,& rows ); for ( i = 1 ; i <= rows ; ++ i ) { for ( j = 1 ...

To Find Factorial of a Number

To Find Factorial of a Number   Factorial of a Number   For any positive number n , its factorial is given by: factorial = 1 * 2 * 3 * 4. ... n If a number is negative, factorial does not exist and factorial of 0 is 1. This program takes an integer from a user. If user enters negative integer, this program will display error message and if user enters non-negative integer, this program will display the factorial of that number. Source Code /* C program to display factorial of an integer if user enters non-negative integer. */ #include <stdio.h> int main () { int n , count ; unsigned long long int factorial = 1 ; /* you can only write int */ printf ( "\nEnter an integer: \n" ); scanf ( "%d" ,& n ); if ( n < 0 ) printf ( "\nError!!! Factorial of negative number doesn't e...

To Check Whether a Number is Even or Odd

C Program to Check Whether a Number is Even or Odd Numbers perfectly divisible by 2 are known even numbers and numbers which are not divisible by 2 are called odd numbers. This program takes an integer from user and checks whether that number is even or odd and displays the result. Source Code /*to check whether a number entered by user is even or odd. */ #include <stdio.h> int main() { int num; printf( "Enter an integer you want to check: " ); scanf( "%d" ,&num); if ((num% 2 )== 0 ) /* Checking whether remainder is 0 or not. */ printf( "%d is even." ,num); else printf( "%d is odd." ,num); return 0 ; } Output 1 Enter an integer you want to check : 25 25 is odd . Output 2 Enter an integer you want to check : 12 12 is even .   In this program, user is asked to enter an integer which is stored in variable num . Then,...

Matchstick Game using C Programming

Matchstick Game using C Programming Write a program for a matchstick game being played between the computer and a user. Your program should ensure that the computer always wins. Rules for the game are as follows: -There are 21 matchsticks. -The computer asks the player to pick 1, 2, 3 or 4 matchsticks. -After the person picks, the computer does its picking. -Whoever is forced to pick up the last matchstick loses the game. -There are 21 matchsticks. -Whoever is forced to pick up the last matchstick loses the game. So the last one is special, so it's all about how to get rid of 20 matches in pairs of turns. -The computer asks the player to pick 1, 2, 3 or 4 matchsticks. So if we reduce the total by 5 each round, the sequence will go 21 16 11 6 1 In effect, whatever number the user picks (n), the computer picks 5-n #include <stdio.h> main ( ) { int matchsticks = 21 , user , computer ; printf ( "Do not enter Invalid Numbers. \...