Posts

Showing posts from 2017

Python Program to Find the Factorial of a Number

Python Program to Find the Factorial of a Number  # Python program to find the factorial of a number provided by the user. # change the value for a different result num = 7 # uncomment to take input from the user #num = int(input("Enter a number: ")) factorial = 1 # check if the number is negative, positive or zero if num < 0:    print("Sorry, factorial does not exist for negative numbers") elif num == 0:    print("The factorial of 0 is 1") else:    for i in range(1,num + 1):        factorial = factorial*i    print("The factorial of",num,"is",factorial)

Python Program to Print all Prime Numbers in an Interval

Python Program to Print all Prime Numbers in an Interval  # Python program to display all the prime numbers within an interval # change the values of lower and upper for a different result lower = 800 upper = 1000 # uncomment the following lines to take input from the user #lower = int(input("Enter lower range: ")) #upper = int(input("Enter upper range: ")) print("Prime numbers between",lower,"and",upper,"are:") for num in range(lower,upper + 1):    # prime numbers are greater than 1    if num > 1:        for i in range(2,num):            if (num % i) == 0:                break        else:            print(num) output: Prime numbers between 800 and 1000 are: 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 ...

Python Program to Check Prime Number

Python Program to Check Prime Number # Python program to check if the input number is prime or not num = 407 # take input from the user # num = int(input("Enter a number: ")) # prime numbers are greater than 1 if num > 1:    # check for factors    for i in range(2,num):        if (num % i) == 0:            print(num,"is not a prime number")            print(i,"times",num//i,"is",num)            break    else:        print(num,"is a prime number")        # if input number is less than # or equal to 1, it is not prime else:    print(num,"is not a prime number") output: 407 is not a prime number 11 times 37 is 407  In this program, variable num is checked if it's prime ...

Add Two Numbers Provided by The User

Add Two Numbers Provided by The User # Store input numbers num1 = input ( 'Enter first number: ' ) num2 = input ( 'Enter second number: ' ) # Add two numbers sum = float ( num1 ) + float ( num2 ) # Display the sum print ( 'The sum of {0} and {1} is {2}' . format ( num1 , num2 , sum ))      output:   Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.8

Python Program to Add Two Numbers

Python Program to Add Two Numbers # This program adds two numbers num1 = 1.5 num2 = 6.3 # Add two numbers sum = float(num1) + float(num2) # Display the sum print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))  output The sum of 1.5 and 6.3 is 7.8

Python Program to Print Hello world!

Python Program to Print Hello world! # This program prints Hello, world! print('Hello, world!')

C Program to Multiply two Matrices by Passing Matrix to a Function

C Program to Multiply two Matrices by Passing Matrix to a Function #include <stdio.h> void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond); void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond); void display(int mult[][10], int rowFirst, int columnSecond); int main() {     int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k;     printf("Enter rows and column for first matrix: ");     scanf("%d %d", &rowFirst, &columnFirst);     printf("Enter rows and column for second matrix: ");     scanf("%d %d", &rowSecond, &columnSecond);     // If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix agai...

C Program to Write a Sentence to a File

C Program to Write a Sentence to a File #include <stdio.h> #include <stdlib.h>  /* For exit() function */ int main() {    char sentence[1000];    FILE *fptr;    fptr = fopen("program.txt", "w");    if(fptr == NULL)    {       printf("Error!");       exit(1);    }       printf("Enter a sentence:\n");    gets(sentence);    fprintf(fptr,"%s", sentence);    fclose(fptr);    return 0; }  

C Program to Display its own Source Code as Output

C Program to Display its own Source Code as Output #include <stdio.h> int main() {     FILE *fp;     char c;     fp = fopen(__FILE__,"r");     do {          c = getc(fp);          putchar(c);     }     while(c != EOF);     fclose(fp);     return 0; }

Program to Display English Alphabets in Uppercase and Lowercase

Program to Display English Alphabets in Uppercase and Lowercase #include <stdio.h> int main() {     char c;     printf("Enter u to display alphabets in uppercase. And enter l to display alphabets in lowercase: ");     scanf("%c", &c);     if(c== 'U' || c== 'u')     {        for(c = 'A'; c <= 'Z'; ++c)          printf("%c ", c);     }     else if (c == 'L' || c == 'l')     {         for(c = 'a'; c <= 'z'; ++c)          printf("%c ", c);     }     else        printf("Error! You entered invalid character.");     return 0; }

C Program to Display Characters from A to Z Using Loop

C Program to Display Characters from A to Z Using Loop  #include <stdio.h> int main() {     char c;     for(c = 'A'; c <= 'Z'; ++c)        printf("%c ", c);        return 0; }

C Program to Find the Frequency of Characters in a String

C Program to Find the Frequency of Characters in a String #include <stdio.h> int main() {    char str[1000], ch;    int i, frequency = 0;    printf("Enter a string: ");    gets(str);    printf("Enter a character to find the frequency: ");    scanf("%c",&ch);    for(i = 0; str[i] != '\0'; ++i)    {        if(ch == str[i])            ++frequency;    }    printf("Frequency of %c = %d", ch, frequency);    return 0; }  Output Enter a string: This website is awesome. Enter a character to find the frequency: e Frequency of e = 4

C Program to Find the Length of a String

C Program to Find the Length of a String #include <stdio.h> int main () { char s [ 1000 ], i ; printf ( "Enter a string: " ); scanf ( "%s" , s ); for ( i = 0 ; s [ i ] != '\0' ; ++ i ); printf ( "Length of string: %d" , i ); return 0 ; }  

C Program to Copy String Without Using strcpy()

C Program to Copy String Without Using strcpy() #include <stdio.h> int main () { char s1 [ 100 ], s2 [ 100 ], i ; printf ( "Enter string s1: " ); scanf ( "%s" , s1 ); for ( i = 0 ; s1 [ i ] != '\0' ; ++ i ) { s2 [ i ] = s1 [ i ]; } s2 [ i ] = '\0' ; printf ( "String s2: %s" , s2 ); return 0 ; }