Category Archives: Pratice Problem

C program to find area and circumference of a circle

C program to find area and circumference of circle


// C program to find area and circumference of circle
//Author:Sani kamal
//date: 24-08-2017

#include<stdio.h>

int main(){
float pi=3.1416;
float area,ci;
int radius;
printf("Enter radius of circle:");
scanf("%d",&radius);
area=pi*radius*radius;
printf("Area of circle:%f\n",area);
ci=2*pi*radius;
printf("Circumference of circle:%f",ci);
return 0;

}

 

Output:

Enter radius of circle: 2

Area of circle:12.56640

Circumference of circle:12.56640

 

C program to convert temperature degree centigrade to fahrenheit

C program to return absolute value

C program to convert temperature degree centigrade to fahrenheit

This C program convert temperature centigrade  to fahrenheit scale.


// C program to convert temperature centigrade to fahrenheit
//Author: Sani Kamal

#include<stdio.h>

int main(){
float c,f;
printf("Enter Temperature in centigrade:\n");
scanf("%f",&c);
f=9/5.0*c+32; //formula to convert centigrade to fahrenheit
printf("Temperature in fahrenheit is:%f",f);
return 0;

}

 

Output:

Enter Temperature in centigrade:

14

Temperature in fahrenheit is: 57.200001

 

C Program to find sum of two number

C program to return absolute value

C program to return absolute value like abs() function.

The program need one user input.


// C program to return absolute value like abs() function
//Author: Sani Kamal

#include<stdio.h>

int main(){
int user_abs(int);
int n;
printf("Enter a +VE/-VE number:\n");
scanf("%d",&n);
int result=user_abs(n);
printf("Absolute Value is:%d",result);
return 0;
}
// function to finding absolute value
user_abs(int x){
if(x<0)
return (x*-1);
return x;
}

Output:

Enter +VE/-VE number: -19

Absolute value is: 19

Basic C programs (must do practice set)

  1. C program to find area and circumference of circle.
  2. C program to find simple interest.
  3. C program to convert temperature from degree centigrade to fahrenheit .
  4. C program to calculate sum of 5 subjects and find percentage.
  5. C program to swap two number without using third variable.
  6. C program to reverse a given number.
  7. C program to find greatest in three numbers.
  8. C program to show the use of conditional operator.
  9. C program to find that entered year is leap year or not.
  10. C program to find whether given no. is even or odd.
  11. C program to shift inputed data by two bits to the left.
  12. C program to use switch statement. Display Sunday to Saturday.
  13. C program to display arithmetic operator using switch case.
  14. C program to display first 10 natural no. and their sum.
  15. C program to print star.
  16. C program to print star part 2.
  17. C program to print star part 3.
  18. C program to print fibonacci series up to n.
  19. C program to find factorial of a number.
  20. C program to find whether given number is a prime number or not.
  21. C program to display sum of series 1+1/2+1/3+………+1/n.
  22. C program to display series and find sum of 1+3+5+……+n.
  23. C program to use bitwise AND operator between the two integers.
  24. C program to add two number using pointers.
  25. C program to show sum of n elements of array and show the average.
  26. C program to find maximum number in an array.
  27. C program to display a matrix.
  28. C program to find sum of two matrices.
  29. C program to find subtraction of two matrices.
  30. C program to find multiplication of two matrices.
  31. C program to find transpose of a matrix.
  32. C program to find maximum number in an array using pointer.
  33. C program to show input and output of a string.
  34. C program to find square of a number using functions.
  35. C program to swap two numbers using functions.
  36. C program to find factorial of a number using functions.
  37. C program to show table of a number using functions.
  38. C program to show call by value.
  39. C program to show call by reference.
  40. C program to find largest of two number using function.
  41. C program to find factorial of a number using recursion.
  42. C program to find whether a string is palindrome or not.
  43. File operations C program.
  44. Merging one dimensional array -Excluding The Repeating Element.
  45. C program to find number of occurrences of vowels ,consonants,words,spaces and special character in the given statement.
  46. C program to create enumerated datatype for 12 months. Display their values in integer constants.
  47. C program to evaluate  the equation y=xn where n in non negative integer.
  48. C program to print the multiplication table from 1*1 to 16*10.
  49. Program uses a for loop to print the powers of 2 table for the power 0 to 20, both positive and negative.
  50. A class of  n students take an annual examination in m subjects. A program to read the marks obtained by each student in various subjects and to compute and print the total marks obtained by each of them.
  51. C program to illustrates the use of break statement.
  52. C program to evaluate the series 1/1-x=1+x+ x^2+x^3+……+x^n.
  53. C program to illustrates the use of continue statement.
  54. C program to print binomial coefficient table.
  55. C program to draw a histogram.
  56. C program to illustrate minimum cost problem.
  57. C program for sorting the elements of an array in descending order.
  58. C program to finding  the largest number of an array.
  59. C program for removing the duplicate element in an array.
  60. C program for finding the desired  k-th smallest element in an array.
  61. C program to sort a list of numbers and determine median.
  62. C program to calculate standard deviation.
  63. C program to evaluate responses to a multiple choice test.
  64. C program for production  and sales analysis.
  65. C program to read a series of words from a  terminal using scanf function.
  66. C program to read a line of text from terminal.
  67. C program to copy one string into another string and count the number of characters copied.
  68. C program for printing of the alphabet set in decimal and character format.
  69. C program to concatenation of strings.
  70. C program to illustration of string handling functions.
  71. C program to sort a list of names in alphabetical order.
  72. C program for counting of characters ,words and lines in a text.
  73. C program to alphabetize a student list.
  74. C program for functions with no arguments and no return values.
  75. C program for functions with arguments but no return values.
  76. C program for functions with arguments and return values.
  77. Write a function power that computes x raised to the power y for integers x and y and return double type value.
  78. C program to show how user-defined function is called.
  79. C program to define user-defined function. Call them at different places.
  80. C program to show how similar variables names can be used in different functions.
  81. C program to show effect of global variables in different functions.
  82. C program to display message using user-defined function.
  83. C program to return more than one value from user defined function.
  84. C program to pass arguments to user-defined function by value and by reference.
  85. C program to return only absolute value like abs() function.
  86. C program to calculate square and cube of an entered number. Use function as an argument.
  87. C program to assign return value of a function to another variable.
  88. C program to use %(mod) with function.
  89. C program to evaluate the equation s=sqrt(a()+b()) using function.
  90. C program to call user-defined function through if statement.
  91. C program to call user-defined fucntion through switch statement.
  92. C program to call function through the for loop.

 

C++ program to find factorial of a given number

A simple cpp program to find factorial of a given number using foor loop.

Input n=5

output =120

Time Complexity is O(n)

//CPP program to find factorial of a given number
// using for loop
#include
using namespace std;
int main(){
    int n,fact=1,i;
    cout<<"Enter a number:";     cin>>n;
    for(i=1;i<=n;i++){
        fact =fact *i;
        }
    cout<<"Factorial is:"<<fact;
    return 0;
}

output:
Enter a number:5
Factorial is:120

Java program to find Sum of proper divisor of a number

Given a natural number n, calculate sum of all its proper divisors. A proper divisor of a natural number is the divisor that is strictly less than the number.

For example, number 10 has 3 proper divisors: 1, 2, 5 and the divisor summation is: 1 + 2 + 5 = 8.



import java.util.*;
import java.lang.*;
import java.io.*;

class SumOfDivisors {
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
            System.out.println("Enter a number:");
            int n=sc.nextInt();
            int result=1;
            for(int i=2;i<Math.sqrt(n);i++){
                if(n%i==0){
                    if(n/i==0){
                        result +=i;
                    }else{
                        result +=(i+n/i);
                    }
                }
            }
            System.out.println(result);
        
    }
}

All divisor of a natural number(three ways)

Given a number n, print all distinct divisors of it , including 1 and the number itself.

Example:

Input: n=20

output: 1 2 4 5 10 20
input n=125
output: 1 5 25 125
Method 1: A solution would be to iterate all the number from 1 to n, checking if that number divides n and print it.

Method 2: A solution would be to iterate all the number from 1 to square root of n, checking if that number divides n and print the number pair if number pair are equal then print only one. It print the divisors in small-big pair.

Method 3: A solution would be to iterate all the number from 1 to square root of n, checking if that number divides n and print the first number and store the second number in a list if number pair are equal then print only one and print the list in reverse order. It print the divisors in sorted order.

Below is a java program for the same.

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sani Kamal on 29-May-17.
 */
//Java program to find all divisors of a natural number including
// 1 and the number itself
public class PrintDivisors {
    //Method to print all divisors
    //Time complexity: O(n)
    //Auxiliary Space= O(1)
    //It prints divisors in sorted order
    private static void printDivisorAsc(int number) {
        for (int i = 1; i <= number; i++) {
            if (number % i == 0) {
                System.out.print(i + " ");
            }

        }
    }

    //Method to print all divisors
    //Time complexity: O(log(n))
    //Auxiliary Space= O(1)
    //it prints divisors in random order
    private static void printDivisorRand(int number) {
        //This loop run till square root
        for (int i = 1; i <= Math.sqrt(number); i++) {
            if (number % i == 0) {
                //If divisors are equal, print only one
                if (number / i == i) {
                    System.out.print(i + " ");

                }
                //otherwise print both
                else {
                    System.out.print(i + " " + number / i + " ");
                }
            }

        }
    }
    //Method to print all divisors,it prints divisors in sorted order
    //Time complexity: O(log(n))
    //Auxiliary Space= O(1)
    private static void printDivisorSorted(int number) {
        List li=new ArrayList<Integer>();
        //This loop run till square root
        for (int i = 1; i <= Math.sqrt(number); i++) {
            if (number % i == 0) {
                //If divisors are equal, print only one
                if (number / i == i) {
                    System.out.print(i + " ");

                }
                else {
                    System.out.print(i + " ");
                    // add the second divisor in list
                    li.add(number/i);
                }
            }

        }
        //The list will print reverse
        for (int i = li.size()-1; i >=0 ; i--) {
            System.out.print(li.get(i)+" ");

        }
    }
        //Driver program to test above method
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a number to find divisors:");
        try {
            int num = Integer.parseInt(br.readLine());
            printDivisorAsc(num);
            System.out.println();
            printDivisorRand(num);
            System.out.println();
            printDivisorSorted(num);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

    }
}




Output

"C:\Program Files\Java\jdk1.8.0_65\bin\java" 
Enter a number to find divisors:
124
1 2 4 31 62 124 
1 124 2 62 4 31 
1 2 4 31 62 124 
Process finished with exit code 0

Weird Numbers

Given an integer as input, can you check the following:

  • If   N is odd then print “Weird”
  • If N is even and, in between range 2 and 5(inclusive), print “Not Weird”
  • If   N is even and, in between range 6 and 20(inclusive), print “Weird”
  • If is even and ,N>20  print “Not Weird”

Java solution for this problem is given below:

 

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        if(n%2 !=0){
            System.out.println("Weird");
        }
        else if((n>=2 &&n<=5)&& n%2==0){
            System.out.println("Not Weird");
        }
        else if(n>=6 && n<=20 && n%2==0){
            System.out.println("Weird");
        }else{
            System.out.println("Not Weird");
        }
    }
}

 

java program that lists all the files in a given directory

java program that lists all the directory and files in a given directory

import java.io.File;

/**
 * java program that lists all the files in a given directory
 * Created by sani kamal on 07-May-17.
 */
public class FileListing {
    public static void main(String[] args) {
        String dirPath = "C:\\Program Files\\Java\\jdk1.8.0_65\\db";
        File basedir = new File(dirPath);
        if (basedir != null && basedir.exists()) {
            File[] files = basedir.listFiles();
            System.out.println("Files in the directory:" + basedir.getAbsolutePath());
            System.out.println("Is directory writable: " + basedir.canWrite());
            System.out.println("Is directory readable: " + basedir.canRead());
            System.out.println("Numbers of file or directory:" + files.length);
            File file;
            for (int i = 0; i < files.length; i++) {
                file = files[i];
                if (file.isDirectory()) {
                    System.out.println("Directory:" + file.getName());
                } else {
                    System.out.println("File:" + file.getName());
                }

            }
        } else {
            System.out.println("Please provide a valid directory path...");
        }
    }
}