## Jan 28, 2020

### Series programs questions for practice

Below shown the different series programs for practice:

1) 1 + 2 + 3 + 4 + 5 + ... + n

2) (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)

3) (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)

4) 1! + 2! + 3! + 4! + 5! + ... + n!

5) (1^1) + (2^2) + (3^3) + (4^4) + (5^5) + ... + (n^n)

6) (1!/1) + (2!/2) + (3!/3) + (4!/4) + (5!/5) + ... + (n!/n)

7) [(1^1)/1] + [(2^2)/2] + [(3^3)/3] + [(4^4)/4] + [(5^5)/5] + ... + [(n^n)/n]

8) [(1^1)/1!] + [(2^2)/2!] + [(3^3)/3!] + [(4^4)/4!] + [(5^5)/5!] + ... + [(n^n)/n!]

9) 1/2 - 2/3 + 3/4 - 4/5 + 5/6 - ...... n

10) 1 2 3 6 9 18 27 54...

11) 2 15 41 80 132 197 275 366 470 587...

12) 1 3 8 15 27 50 92 169 311...

## Jan 24, 2020

### C program to print following numbers series 1 2 3 6 9 18 27 54 81...

Ex: write a C program to print following series 1 2 3 6 9 18 27 54 81. How to write a C program to print following series 1 2 3 6 9 18 27 54 81.. C program to print following series 1 2 3 6 9 18 27 54 81..

Input from user:
Enter the number: 8

Expected output:
1 2 3 6 9 18 27 54 .....

Program:

#include<stdio.h>
void main()
{
int i,no,z=1,k;
printf("Enter the number:\n");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
if(i<=3)
{
printf("%d ",i);
z=i;
}
else if(z%2==1)
{
k=z;
z=z*2;
printf("%d ",z);
}
else if(z%2==0)
{
z=z+k;
printf("%d ",z);
}
}
}

Above program shows the following output:

## Jan 19, 2020

### Logical Operators in C with examples

Logical Operators:
In C language we have various logical operators.
They are....

1. Logical AND - &&
2. Logical OR - ||
3. Logical NOT - !

We will see there examples for better understanding.

Program using logical AND:

#include<stdio.h>
void main()
{
int i;
for(i=0;i<10;i++)
{
if(i>0 && i>5)
{
printf("%d\n",i);
}
}
}

In AND operator need both conditions are true then it is enters the loop, otherwise not.

OUTPUT:

Program using logical OR:

#include<stdio.h>

void main()
{
int i;
for(i=0;i<10;i++)
{
if(i>0||i>5)
{
printf("%d ",i);
}
}
}

In OR operator if  any one or both conditions are  true then this enters the if block otherwise not.

OUTPUT:

Program using logical NOT:

#include<stdio.h>

void main()
{
int i;
for(i=0;i<10;i++)
if(i!=5)
printf("%d ",i);
}

If we dosn't want to print any value of the loop then we can use the NOT operator.

OUTPUT

### C program to print Strings in ascending order

Ex: Write a C program to print Strings in ascending order. How to write a C program to print Strings in ascending order. C program to print Strings in ascending order.

Input from user:
Enter the number: 5
Enter the strings:
java
css
html
python
ruby

Expected output:
Sorted strings are:
css
html
java
python
ruby

In below program to print strings in ascending order we will use string functions but, first we need to declare <string.h> header file in our program because, these string functions are defined in this <string.h> header file.

Program:

#include<stdio.h>
#include<string.h>
void main()
{
int i,j,no;
char a[20][20],temp[20];
/*Accept input number from  user*/
puts("Enter the number:");
scanf("%d",&no);

/*Accept strings using gets() string function and store it in array a*/
puts("Enter strings:");
for(i=0;i<=no;i++)
gets(a[i]);

for(i=0;i<=no;i++)
{
for(j=i+1;j<=no;j++)
{
/*Compare two strings using strcmp() string function*/
if(strcmp(a[i],a[j])>0)
{
/*Copy one string to another string using strcpy() string function*/
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
}
/*Print sorted strings using puts() string function*/
printf("\nSorted Strings are:");
for(i=0;i<=no;i++)
puts(a[i]);

}

Above program shows the following output:

## Jan 12, 2020

### C program to accept students marks and display result

Ex: write a C program to accept students marks and display result. hoe to write a C program to accept students marks and display result. C prgram to accept students marks and display result.

Input from user:
Enter the three subjects marks:
65
77
88

Expected output:
Total marks = 230
Average marks = 76.0000
Result is distinction.

Step by step logic of program to display result:

1. First declare intiger variable's  s1, s2, s3 for three subjects marks and total to store total marks and float type variable  average for average marks.

2. Accept three subjects marks from user using input/output function's.

3. In the logic add three subject marks and store it in variable total (total=s1+s2+s3;).
After that devide total by no. of subjects and store it in variable average (average=total/3;)

4. After that use  given conditions using if-else statement's to print result.

5. if(average>=70)
print result is distinction.

6. if(average>=60)
print result is first class.

7. if(average>=50)
print result is second class.

8. if(average>=40)
print result is pass.

9. Otherwise print result is fail.

Program:

#include<stdio.h>
void main()
{

int s1,s2,s3,total;
float average;

printf("Enter the three subjects marks one by one:\n");
scanf("%d%d%d",&s1,&s2,&s3);

total=s1+s2+s3;
average=total/3;

printf("\nTotal marks= %d",total);
printf("\nAverage marks= %f\n",average);

if(average>=70)
{
printf("Result is distinction\n");
}
else if(average>=60)
{
printf("Result is first class\n");
}
else if(average>=50)
{
printf("Result is second class\n");
}
else if(average>=40 )
{
printf("Result is pass \n");
}
else
{
printf("Result is Fail\n");
}

}

Above program shows the following output:

### C program to check given number is positive, negative or neutral

Ex: write a C program to check given number is positive, negative or neutral. How to write a C program to check given number is positive negative or neutral. C program to check given number is positive, negative or neutral.

Input from user:

Enter the number: -5

Expected Output:

Given number is negative.

Step by step logic of the program:

1. Accept number from user say no.

2. Use if-else statements and relational operators to check given number is positive  or negative.

3. If number is greater than 0 then print given number is positive, otherwise if number is less than 0 then print given number is negative.

4. Otherwise print number is neutral.

5.print output on the output screen.

Program:

#include<stdio.h>

int main()
{
int no;

printf("Enter the number\n");
scanf("%d",&no);

if(no>0)
{
printf("Given number is positive\n");
}
else if(no<0)
{
printf("Given number is negative");
}
else
{
printf("Given number is neutral");
}
return 0;
}

Above program shows the following output:

## Jan 10, 2020

### C Program For Calculate Net Salary of an employee

Ex: write a C Program for calculate the net salary of an employee. How to write a C program for calculate Net salary of an employee. C program for calculate Net salary of an employee.

Input from user:

Enter the basic salary of an employee: 10000

Expected output:

Net salary of an employee is: 14000.0000

Step by step logic of the given program:

1. First accept basic salary of an employee. declare variable say b.

2. After that find ta, da and hra using basic salary as follows:

ta=b*40/100;

da=b*40/100;

hra=b*60/100;

3. To find Net salary store the addition of ta, da and hra into the net variable-(net=ta+da+hra)

4. Print net salary to the output screen.

Program:

#include<stdio.h>
int main()
{
float b,ta,da,hra,net;

printf("Enter the basic salary of an employee:\n");
scanf("%f",&b);

ta=b*40/100;
da=b*40/100;
hra=b*60/100;

net=ta+da+hra;

printf("Net salary of an employee is: %f",net);

return 0;

}

Above program shows the following output:

### C program for swap two numbers

Ex: write a C program for swap two numbers. How to write a C program which swaps values of the two numbers. C program to swap two numbers.

Input from user:

Enter the no1: 10
Enter the no1: 20

Expected Output:

Values after swaping:
no1=20
no2=10

Step by step logic to swap values of two variable's:

1. First we will declare the variables for swap two variable's value.

2. We declare no1, no2 and temp. where no1 for accepting 1st number from user and no2 for accepting 2nd number from user and temp is third varible which will help to swap no1 and no2 values.

3. In the logic :

• we will store no1(which is 10) value in the temp variable (temp=10).

• After that we will store no2(which is 20) value in no1 variable (no1=20).

• In the last we will give temp(which is 10) variable value to the no2 variable (no2=10).

4. After that we will display swaped values of no1 and no2 (no1=20 and no2=10).

Program for accept two numbers and swap there values:

#include<stdio.h>
void main()
{
int no1,no2,temp;

printf("Enter the no1:\n");
scanf("%d",&no1);

printf("Enter the no2:\n");
scanf("%d",&no2);

temp=no1;
no1=no2;
no2=temp;

printf("Values after swaping:\nno1=%d\nno2=%d",no1,no2);

}

Above program shows the following output:

## Jan 9, 2020

### C Program to calculate total & average of three subjects marks

Ex: write a C program to calculate total and average  of three subjects marks. How to write a C program to calculate total and average of three subjects. C Program to calculate total and average of three subjects.

Input from user:

Enter three subjects marks:
75
85
65

Expected output:

Total= 225
Average= 75.0000

Step by step logic of the given program:

1. First declare intiger variable's  S1, S2, S3 for three subjects marks and total for store total marks and float type variable  average for average marks.

2. Accept three subjects marks from user using input/output function's.

3. In the logic add three subject marks and store it in variable total (total=S1+S2+S3;).
After that devide total by no. of subjects and store it in variable average (average=total/3;)

4. print total and average marks on the output screen.

Program:

#include<stdio.h>
int main()
{
int s1,s2,s3,total;
float average;

printf("Enter three subject marks:\n");
scanf("%d%d%d",&s1,&s2,&s3);

total=s1+s2+s3;
average=total/3;

printf("Total= %d\nAverage= %f",total,average);

return 0;
}

Above program shows the following output:

## Jan 8, 2020

### C program to print Factorial Of Given Number

EX: How to print factorial of the given number. What is the logic to print factorial of the given number. C Program to find the factorial of given number.

Input from user:
Enter the number: 7

Expected output:
Factorial is 5040

### Logic to print factorial of the given number:

1. Accept input number from user declare variable say no.

2. make i=no.

3. We all know factorial of 0 is 1 so if no is equal equals to 0 print factorial is 1.

4. Otherwise use while loop from no to (1+1). And inside that while loop use factorial number simple logic as follows:
while(no>1)
{
i=i*(no-1);
no--;
}

5. Last print value of i means factorial of given number.

## C program to print Factorial Of Given Number:

#include<stdio.h>
void main()
{
int no,i;
printf("enter the number\n");
scanf("%d",&no);
i=no;
if(no==0)
{
printf("factorial is 1");
}
else
{
while(no>1)
{
i=i*(no-1);
no--;
}
printf("factorial is %d",i);
}
}

Above program shows the following output: