Monday, July 8, 2019

c interview questions2

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

int main()
{
    int i=9;
    if(i==9)
    {
        int i=25;
    }
    printf("i=%d",i);
   // printf("Hello world!\n");
    return 0;
}


output:
i=9


explanation:in if block it is again defined but it takes like function inside declaration

c interview questions1

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

void func (void);
void main( )
{
    printf("first print\n");
    goto abc;
}
void func (void)
{
    abc:
    printf("Barilly\n");
}


output:

error:label abc is used but not defined

LCM and HCF of the two numbers in c programming

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


int m, n;
main( )
{
    int x,y;
    printf ("Enter two numbers ");
    scanf ("%d %d·" , &x, &y);
    printf ("RCF of %d and %d is %d\n", x, y, hcf (x, y) );
    m=x;n=y;
    printf ("LCM of %d and %d is %d\n", x, y, lcm (x, y) );
}
int hcf(int a, int b)
{
    if(a==b)
        return(b);
    else if (a<b)
        hcf(a,b-a);
    else
        hcf(a-b,b);
}
int lcm(int a, int b)
{
    if (a==b)
        return(b);
    else if(a<b)
        lcm(a+m,b);
    else
        lcm(a,b+n);
}


output:

Enter two numbers 12
15
RCF of 12 and 15 is 3
LCM of 12 and 15 is 60

program to find the sum of series in c language

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

long intfact (int num);
double power (float x, int n);
double series (float x, int n);
double rseries (float x, int n);

void main()
{

    float x;
    int n;
    printf ("Enter x:");
    scanf("%f",&x);
    printf ("Enter number of terms\n");
    scanf ("%d", &n);
    printf("Iterative %lf\n",series(x,n));
    printf("Recursive %lf\n",series(x,n));
}

long int fact (int num)
{


    int i;
    long int f=1;
    for(i=1;i<=num;i++)
        f=f*i;
    return f;
}
double power (float x, int n)
{
    int i;
    float p=1;
    for(i=1;i<=n;i++)
        p=p*x;
    return p;
}

double series (float x, int n)
{
    int i,j,sign=1;
    float term, sum;
    for(i=1;i<=n;i++)

    sign=(i%2==0)?-1:1;
    j=2*i-1;
    term=sign*power(x,j)/fact(j);
    sum+=term;
    return sum;
}
double rseries( float x, int n)
{
    int sign=1;
    float term, sum;
    if(n==0)
        sum=0;
    else
    {
        sign=(n%2==0)?-1:1;
        term=sign*power(x,2*n-1)/fact(2*n-1);
        sum=term+rseries(x,n-1);
        return sum;
    }
}

decimal to binary,octal and hexadecimal conversion in c language

#include<stdio.h>
#include <string.h>

void convert(int, int);
main ( )
{
    int num, base;
    int choice;
while(1)
{
    printf("l.Binary\n") ;
    printf("2.0ctal\n") ;
    printf("3.Hexadecimal\n") ;
    printf("4.Exit\n") ;
    printf ("Enter your choice\n") ;
    scanf ("%d",&choice);
    switch(choice)
    {
        case 1:
            base=2;
            break;
        case 2:
            base=8;
            break;
        case 3:
            base=16;
            break;
        case 4:
            exit(1) ;
        default:
            printf ("Wrong choice\n");
            continue;

    }
    printf ("Enter the number in decimal: ");
    scanf("%d",&num) ;
    convert(num,base) ;
    printf ("\n") ;
}
}


void convert (int num, int base)
{
    int rem;
    rem=num%base;
    num/=base;
    if (num>0)
        convert (num,base);
    if(rem<10)
        printf("%d",rem);
    else
        printf("%c",rem-10+'A');
}

out put:
l.Binary
2.0ctal
3.Hexadecimal
4.Exii
Enter your choice 1
Enter the number in decimal: 12
1100

l.Binary
2.0ctal
3.Hexadecimal
4.Exii
Enter your choice 2
Enter the number in decimal: 12
14

l.Binary
2.0ctal
3.Hexadecimal
4.Exii
Enter your choice 3
Enter the number in decimal: 12
C

number divisible by 11 or not in the c programming

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

void test (long int x);
main ( )
{
    long int num;
    printf("Enter the numher to be tested ");
    scanf("%ld",&num);
    test(num) ;
}
void test(long int n)
{
    int s1=0, s2=0, k;
    while(n>0)
    {
        s1+=n%10;
        n/=10;
        s2+=n%10;
        n/=10;
    }
    k=s1>s2? (s1-s2) : (s2-s1) ;
    if(k>10)
        test(k);
    else if(k==0)
        printf ("The number is divisible by 11 \n") ;
    else
        printf("The number is not divisibl~ by 11\n");
}

out put ::
Enter the numher to be tested 121
The number is divisible by 11

Enter the numher to be tested 12345
The number is not divisibl~ by 11




reverse of the given number

#include<stdio.h>
#include <string.h>

void reverse (long int n);
void main ( )
{
    long int num;
    printf ("Enter number ");
    scanf("%ld",&num);
    reverse(num);
    printf("\n");
}
void reverse (long int n)
{
    int rem;

    if(n==0)
        return;
    else
    {
        rem=n%10;
    printf("%d",rem);
        n/=10;
    reverse(n) ;
    }

}


output:

Enter number 12345
54321

Saturday, July 6, 2019

Reading and Writing files in python

""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
started date:06-07-2019
latest modified date:06-07-2019
version:1.0
created by:ANIL DURGAM
*****************************************************"""
fred=open("hello","w")
fred.write("hello world")
fred.close()
read_file=open("hello","r")
file_read=read_file.read()
read_file.close()
print(file_read)

list and tuple function usage in python

""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
started date:06-07-2019
latest modified date:06-07-2019
version:1.0
created by:ANIL DURGAM
*****************************************************"""
FoodTuple=("egg","bread","chicken")
print(FoodTuple)
FoodList=list(FoodTuple)
print(FoodList)
newfoodtuple=tuple(FoodList)
print(newfoodtuple)


output:
('egg', 'bread', 'chicken')
['egg', 'bread', 'chicken']
('egg', 'bread', 'chicken')

immutable type in python

firstTuple[0]="EGG"
print(firstTuple)

output::

NameError: name 'firstTuple' is not defined

example 2:

firstTuple="EGG"
print(firstTuple)

out put:
EGG


sub string in python


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
started date:06-07-2019
latest modified date:06-07-2019
version:1.0
created by:ANIL DURGAM

*****************************************************"""


word="pig"
pigLatinword=word[1:]+word[0]+"ay"
print(pigLatinword)
print(word[0])
print(word[1])
print(word[2])
word1="pygame"
print(word1[1:])
print(word1[2:])
print(word1[0:])

python class topic video