Ex: Write a C program to print 1 3 6 10 15 21 28 36 triangular series. How to write a C program to print 1 3 6 10 15 21 28 36 triangular series. C program to print 1 3 6 10 15 21 28 36 triangular series.
Input from user:
Enter the number: 8
Expected output:
1 3 6 10 15 21 28 36
Program: #include<stdio.h> voidmain() { int i,no,z=1; printf("enter the no\n"); scanf("%d",&no); printf("1 "); for(i=2;i<=no;i++) { z=z+i; printf("%d ",z); } }
Ex: Write a C program to print hollow right triangle number pattern. How to write a C program to print hollow right triangle number pattern. C program to print hollow right triangle number pattern.
Input from user:
Enter the number:
5
Expected output:
1
12
1 3
1 4
12345
Step by step logic of the given program:
1. Accept input number from user declare variable say no.
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> voidmain() { 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:
In C language we have various logical operators. They are....
Logical AND - &&
Logical OR - ||
Logical NOT - !
We will see their examples for better understanding.
Program using logical AND: #include<stdio.h> voidmain() { int i; for(i=0;i<10;i++)
{ if(i>0 && i>5) { printf("%d\n",i); }
} } In AND operator both conditions need to be true then only it will enter inside the if block, otherwise not. OUTPUT:
Program using logical OR: #include<stdio.h> voidmain() { 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 inside the if block otherwise not. OUTPUT:
Program using logical NOT: #include<stdio.h> voidmain() { int i; for(i=0;i<10;i++) if(i!=5) printf("%d ",i); } If we don't want to print any value of the loop then we can use the NOT operator. OUTPUT
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.
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>
voidmain()
{
int s1,s2,s3,total;
float average;
printf("Enter the three subjects marks one by one:\n");
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> intmain() { 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:
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> intmain() { 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:
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> voidmain() { 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);