Monday, June 24, 2019

to find the given number is prime number or not ..c language

#include<stdio.h>
#include<math.h>

void main()
{
    int i,num,flag=1;
    printf("Enter a number \n");
    scanf("%d",&num) ;
    for(i=2;i<= sqrt(num);i++)
    {
        if (num%i==0)
        {
            printf("%d is not prime\n",num);
            flag=0;
            break;
        }
    }
    if (flag==1)
        printf ("%d is prime\n", num);
}

sum of the digits in given number using C and Python


//Program to print the sum of digits -of any number using for loop

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,sum=0,rem;
    printf ("Enter the number ") ;
    scanf ("%d",&n) ;
    for( ;n>0;n/=10)
    {
        rem=n%10; /*taking last digit of number*/
        sum+=rem;
    }
    printf("Sum of digits = %d\n",sum) ;
    //printf("Hello world!\n");
    return 0;
}


program2:
#include<stdio.h>

int sum(int n);
void main()
{
    int num;
    printf ("Enter the number ") ;
    scanf("%d",&num) ;
    printf("Sum of digits of %d is %d\n", num, sum(num));
}
int sum (int n)
{
    int i, sum=0, rem;
    while(n>0)
    {
        rem=n%10;
        sum+=rem;
        n/=10;

    }
    return(sum);
/*rem takes the value of last digit*/
/*skips the last, digi t of number*/
}


output:
enter the number:12
sum of the digits of the 12 is 3
3

PYTHON code:


def main():
sum1=rem=0
n=int(input("enter the number"))
for i in range(n):
if n>0:
rem=n%10
sum1=sum1+rem
n=n//10
print(sum1)
if __name__ == '__main__':
main()


output:
enter the number:264
output:10

fibanocci series in c language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long x, y,z;
    int i,n;
    x=0;
    y=1;

    printf("Enter the number of terms\n") ;
    scanf ("%d",&n);
    //printf("%d",y);
    for(i=1;i<n;i++)
    {
        z=x+y;
        printf("%d",z);
        x=y;
        y=z;
        printf("\n") ;
    }
    return 0;
}

find the program to find the sum of the given series upto n terms

#include <stdio.h>
#include <stdlib.h>


void main()
{
    int i, n, sum=0,term=1;
    printf ("Enter number of terms ") ;
    scanf ("%d" ,&n) ;
    for(i=1;i<=n;i++)
    {
        sum+=term;
        term=term+i;
    }
    printf ("The sum of series upto %d terms is %d\n", n, sum) ;
}

multification of the two numbers with out using " * " operator

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,i;
    int result=0;
    printf("enter two numbers to be multiplied\n");
    scanf("%d%d",&a,&b);
    for(i=1;i<=b;i++)
    {
        result = result+a;
         //printf("%d\n",result);
    }
    printf("%d * %d = %d \n",a,b,result);

    return 0;
}

python class topic video