Showing posts with label number. Show all posts
Count number of sunday on 1st of every month in C/C++ programing code
Problem: Write a code to calculte number of sunday on first of every month from 1st Jan 1901 to 31st Dec 2000 wherease the at 1st Jan 1900 the day is Monday.
The following code demonstrate the working of program. in this program there is use of nested loops 1st for total years thats is 1900 - 2000 inside this each year have 12 months, so inside year loop their is loop from 1 - 12 which is for months. inside this we have if structure which decides which month is this if that months have 31 days it assign z = 31 if that month have 30 days it assign z = 30; if it is February it checks whether it is a leap year or not and then assign respective value to z.
now inside this loop we have a counter day which is controlled through if to have value from 1-7 where 1 represent Monday , 2 Tuesday and so on in this manner 7 represent Sunday.
now we have a variable total Sunday which is initially 0 in final if structure there are 3 condition
1. day ==7 that is the day should be Sunday
2. z == 1 that is the date of month should be 1st
3. the year should be greater than 1900 as we have to count from 1901.
if these 3 condition satisfies then the counter for counting that is total Sunday increased by 1.
and after all loops successfully executed there is a statement to print the value in console .
Hope u have understood it very well for any question or ambiguity plz comment below.
#include<iostream>
#include<conio.h>
using namespace std;
void main(){
int x[3] = {7, 1, 1900};//first sunday
int y[3];
int z;
int day = 0;
int total_sunday = 0;
for (int i = 1900; i <= 2000; i++){
for (int j = 1; j <= 12; j++){
if (j == 1 || j == 3 || j == 5 || j == 7 || j == 8 || j == 12){
z = 31;
}
else if (j == 4 || j == 6 || j == 9 || j == 10 || j == 11) {
z = 30;
}
else if (j==2 && i%4 == 0)
{
z = 29;
}
else{
z = 28;
}
for (int k = 1; k <= z; k++){
y[0] = k;
y[1] = j;
y[2] = i;
day++;
if (day > 7){
day = 1;
}
if (day == 7 && y[0] == 1 && y[2] > 1900){
total_sunday++;
}
}
}
}
cout << "Total sunday on 1st of month during 1 jan 1901 to 31 dec 2000 are: " << total_sunday;
_getch();
}
The following code demonstrate the working of program. in this program there is use of nested loops 1st for total years thats is 1900 - 2000 inside this each year have 12 months, so inside year loop their is loop from 1 - 12 which is for months. inside this we have if structure which decides which month is this if that months have 31 days it assign z = 31 if that month have 30 days it assign z = 30; if it is February it checks whether it is a leap year or not and then assign respective value to z.
now inside this loop we have a counter day which is controlled through if to have value from 1-7 where 1 represent Monday , 2 Tuesday and so on in this manner 7 represent Sunday.
now we have a variable total Sunday which is initially 0 in final if structure there are 3 condition
1. day ==7 that is the day should be Sunday
2. z == 1 that is the date of month should be 1st
3. the year should be greater than 1900 as we have to count from 1901.
if these 3 condition satisfies then the counter for counting that is total Sunday increased by 1.
and after all loops successfully executed there is a statement to print the value in console .
Hope u have understood it very well for any question or ambiguity plz comment below.
#include<iostream>
#include<conio.h>
using namespace std;
void main(){
int x[3] = {7, 1, 1900};//first sunday
int y[3];
int z;
int day = 0;
int total_sunday = 0;
for (int i = 1900; i <= 2000; i++){
for (int j = 1; j <= 12; j++){
if (j == 1 || j == 3 || j == 5 || j == 7 || j == 8 || j == 12){
z = 31;
}
else if (j == 4 || j == 6 || j == 9 || j == 10 || j == 11) {
z = 30;
}
else if (j==2 && i%4 == 0)
{
z = 29;
}
else{
z = 28;
}
for (int k = 1; k <= z; k++){
y[0] = k;
y[1] = j;
y[2] = i;
day++;
if (day > 7){
day = 1;
}
if (day == 7 && y[0] == 1 && y[2] > 1900){
total_sunday++;
}
}
}
}
cout << "Total sunday on 1st of month during 1 jan 1901 to 31 dec 2000 are: " << total_sunday;
_getch();
}
coding for reversing a number and then printing it in c
#include<stdio.h>
#include<conio.h>
void main(){
int a;
int b;
printf("Enter the number You want to reverse: ");
scanf("%d", &a);
printf("\nNumber you Entered is: ");
b = printf("%d", a);
printf("\nIts reverse is: ");
for (int i = 0; i < b; i++){
printf("%d", a%10);
a /= 10;
}
_getch();
}
#include<conio.h>
void main(){
int a;
int b;
printf("Enter the number You want to reverse: ");
scanf("%d", &a);
printf("\nNumber you Entered is: ");
b = printf("%d", a);
printf("\nIts reverse is: ");
for (int i = 0; i < b; i++){
printf("%d", a%10);
a /= 10;
}
_getch();
}
Monday, 22 December 2014
Posted by Unknown
Coding program to print right triangle through odd number in C through asteriks.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
for(int d=1;d<=5;d++){
do {
printf("enter a odd no");
scanf("%d",&a);
}
while(a%2!=1);
int b;
for(b=0;b<a;b++)
{
for(int c=0;c<=b;c++)
printf("*");
printf("\n");
}}
_getch();
}
#include<conio.h>
void main()
{
int a;
for(int d=1;d<=5;d++){
do {
printf("enter a odd no");
scanf("%d",&a);
}
while(a%2!=1);
int b;
for(b=0;b<a;b++)
{
for(int c=0;c<=b;c++)
printf("*");
printf("\n");
}}
_getch();
}
Posted by Unknown
Coing to Generate prime Number in c
#include<stdio.h>;
#include<conio.h>;
//code by waqas javed //main
void main() {
//initializing vars
int i = 1; //prime number founded
int j = 2; //chk start point
int l = 1; //var factor chk var
int m = 0; //number of factor
int number = 0;
//user tell how many prime number to generate
printf("Enter how many prime number you want to generate: ");
scanf("%d", &number);
//printing first 20 prime no.
while (i <= number){
//chk for factors of number
while (j > l){
//if number is factor
if (j%l == 0){
m++;
l++;
}
//if not
else {
l++;
}
}
//chk and print if prime no.
if (j == l) {
//if prime
if (m == 1)
printf("%d.\t%d \n", i++, j);
j++;
l = 1;
m = 0;
}
//if not reinitializing value to chk nxt var
else {
j++;
l = 1;
m = 0;
}
}
_getch();
}
This code work as it starts from 2 and check whether it is a even number or not.. Its checking is designed like the program checks all the number below that number to be checked that whether they are its factor are not... Then the program check how many factor the number has if they are 2 then it take it as prime number and print it..
For example: Program is checking 9 is a prime no or not. it will check all number below it that is 1,2,3,4,5,6,7,8 now the we have 1 and 3 as its factor and 9 itself also so total factor exceeded 2 and the number is not prime...... similarly it will keep checking all number in increasing order starting from 1 until the total prime number too be founded are found.
#include<conio.h>;
//code by waqas javed //main
void main() {
//initializing vars
int i = 1; //prime number founded
int j = 2; //chk start point
int l = 1; //var factor chk var
int m = 0; //number of factor
int number = 0;
//user tell how many prime number to generate
printf("Enter how many prime number you want to generate: ");
scanf("%d", &number);
//printing first 20 prime no.
while (i <= number){
//chk for factors of number
while (j > l){
//if number is factor
if (j%l == 0){
m++;
l++;
}
//if not
else {
l++;
}
}
//chk and print if prime no.
if (j == l) {
//if prime
if (m == 1)
printf("%d.\t%d \n", i++, j);
j++;
l = 1;
m = 0;
}
//if not reinitializing value to chk nxt var
else {
j++;
l = 1;
m = 0;
}
}
_getch();
}
This code work as it starts from 2 and check whether it is a even number or not.. Its checking is designed like the program checks all the number below that number to be checked that whether they are its factor are not... Then the program check how many factor the number has if they are 2 then it take it as prime number and print it..
For example: Program is checking 9 is a prime no or not. it will check all number below it that is 1,2,3,4,5,6,7,8 now the we have 1 and 3 as its factor and 9 itself also so total factor exceeded 2 and the number is not prime...... similarly it will keep checking all number in increasing order starting from 1 until the total prime number too be founded are found.
Posted by Unknown
Diamond with even number in c++
#include<stdio.h>;
#include<conio.h>;
void main(){
int i;
int k;
int j;
int l;
int m;
int n;
int o;
printf("Write an even no: ");
scanf("%d", &i);
while(i%2 == 1) {
printf("\n please write an Even No: ");
scanf("%d", &i);
}
k = i/2 -1;
j = 0;
l = 1;
m = 0;
n = 0;
o = i/2;
while(o > n){
while (k > j){
printf("-");
j++;
}
while(l > m){
printf("*");
m++;
}
if (k == j){
j = 0;
k--;
}
if( l == m){
printf("\n");
n++;
m = 0;
l++;
l++;
}
}
k = j = 0;
l = i-1;
m = 0;
n = 0;
o = i/2;
while(o > n){
while(k > j) {
printf("-");
j++;
}
while(l > m){
printf("*");
m++;
}
if (k == j){
j = 0;
k++;
}
if (l == m) {
printf("\n");
n++;
l--;
l--;
m = 0;
}
}
_getch ();
}
#include<conio.h>;
void main(){
int i;
int k;
int j;
int l;
int m;
int n;
int o;
printf("Write an even no: ");
scanf("%d", &i);
while(i%2 == 1) {
printf("\n please write an Even No: ");
scanf("%d", &i);
}
k = i/2 -1;
j = 0;
l = 1;
m = 0;
n = 0;
o = i/2;
while(o > n){
while (k > j){
printf("-");
j++;
}
while(l > m){
printf("*");
m++;
}
if (k == j){
j = 0;
k--;
}
if( l == m){
printf("\n");
n++;
m = 0;
l++;
l++;
}
}
k = j = 0;
l = i-1;
m = 0;
n = 0;
o = i/2;
while(o > n){
while(k > j) {
printf("-");
j++;
}
while(l > m){
printf("*");
m++;
}
if (k == j){
j = 0;
k++;
}
if (l == m) {
printf("\n");
n++;
l--;
l--;
m = 0;
}
}
_getch ();
}
Sunday, 28 September 2014
Posted by Unknown